Skip to content

Instantly share code, notes, and snippets.

@sungkim11
Created March 17, 2022 10:11
Show Gist options
  • Save sungkim11/92eb59e7c4ab922b237df942efbcede9 to your computer and use it in GitHub Desktop.
Save sungkim11/92eb59e7c4ab922b237df942efbcede9 to your computer and use it in GitHub Desktop.
extern crate serde;
use serde::{Serialize, Deserialize};
extern crate reqwest;
use reqwest::blocking::Client;
#[derive(Serialize, Deserialize, Debug)]
pub struct CoinPrice {
pub base: String,
pub currency: String,
pub amount: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CoinbasePrice {
pub data: CoinPrice
}
impl CoinPrice {
fn print_coinprice(self) {
println!("SPOT: {base}-{currency}: {amount}",
base=self.base,
currency=self.currency,
amount=self.amount);
}
}
pub fn rust_struct_3() {
let spot_url = format!("https://api.coinbase.com/v2/prices/{currency}-{rates}/spot",
currency = "BTC",
rates = "USD");
let client = Client::new();
let resp_spot_price = client.get(&spot_url)
.send();
match resp_spot_price {
Ok(parsed_spot_price) => {
let coinbaseprice = parsed_spot_price.json::<CoinbasePrice>()
.unwrap();
let spot_price = CoinPrice {
base: coinbaseprice.data.base,
currency: coinbaseprice.data.currency,
amount: coinbaseprice.data.amount
};
spot_price.print_coinprice();
}
Err(e) => println!("Err: {:?}", e),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment