-
Notifications
You must be signed in to change notification settings - Fork 0
/
external.rs
45 lines (38 loc) · 1.06 KB
/
external.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use serde::Deserialize;
use std::collections::HashMap;
pub struct CMCService {
pub api_key: String,
}
#[derive(Deserialize, Debug)]
struct CMCResponse {
data: Vec<CMCResponseData>,
}
#[derive(Deserialize, Debug)]
struct CMCResponseData {
symbol: String,
quote: HashMap<String, CMCResponseDataPrice>,
}
#[derive(Deserialize, Debug)]
struct CMCResponseDataPrice {
price: f32,
}
impl CMCService {
pub async fn get_btc_price(&self) -> f32 {
let client = reqwest::Client::builder().build().unwrap();
let resp = client
.get("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest")
.header("X-CMC_PRO_API_KEY".to_string(), &self.api_key)
.send()
.await
.unwrap();
let resp = resp.json::<CMCResponse>().await.unwrap();
let price: f32 = resp
.data
.iter()
.filter(|it| it.symbol == "BTC".to_string())
.map(|it| it.quote.get("USD").unwrap().price)
.next()
.unwrap();
price
}
}