use reqwest::Client; use serde::{Serialize, Deserialize}; use serde_json::{Value}; #[tokio::main] async fn main() { let btc: String = "BTC".to_string(); let btc_price: Value = get_price(btc).await.unwrap(); println!("{}", btc_price); let eth: String = "ETH".to_string(); let eth_price: Value = get_price(eth).await.unwrap(); println!("{}", eth_price); } async fn get_price(code: String) -> Result<Value, Box<dyn std::error::Error>> { let mut url:String = "https://api.coinbase.com/v2/exchange-rates?currency=".to_string(); url.push_str(&code); let contents = reqwest::get(url).await?.text().await?; let res: Value = serde_json::from_str(&contents).unwrap(); println!("{:?}", res["data"]["rates"]["USD"]); Ok((res["data"]["rates"]["USD"]).clone()) }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.97s
Running `target/debug/wallet`
String(“94359.295”)
“94359.295”
String(“3246.82”)
“3246.82”
処理速度が遅いが、取得はできる。
よし、さて、PoSでもやるか!