-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4f7807
commit 3b58fef
Showing
17 changed files
with
220 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "bothan-coingecko" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
license.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
url = "2.5.0" | ||
chrono = "0.4.34" | ||
|
||
bothan-core = { workspace = true } | ||
async-trait = { workspace = true } | ||
reqwest = { workspace = true } | ||
thiserror = { workspace = true } | ||
tracing = { workspace = true } | ||
tracing-subscriber = { workspace = true } | ||
serde ={ workspace = true } | ||
tokio = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::collections::HashMap; | ||
|
||
use reqwest::{Client, RequestBuilder, Response, Url}; | ||
|
||
use crate::api::error::Error; | ||
use crate::api::types::{Coin, Market}; | ||
|
||
pub struct CoinGeckoRestAPI { | ||
url: Url, | ||
client: Client, | ||
} | ||
|
||
impl CoinGeckoRestAPI { | ||
pub fn new(url: Url, client: Client) -> Self { | ||
Self { url, client } | ||
} | ||
|
||
pub async fn get_coins_list(&self) -> Result<Vec<Coin>, Error> { | ||
let url = format!("{}coins/list", self.url); | ||
let builder = self.client.get(url); | ||
let response = send_request(builder).await?; | ||
|
||
Ok(response.json::<Vec<Coin>>().await?) | ||
} | ||
|
||
pub async fn get_coins_market( | ||
&self, | ||
ids: &[&str], | ||
page_size: usize, | ||
page: usize, | ||
) -> Result<Vec<Option<Market>>, Error> { | ||
let url = format!("{}coins/markets", self.url); | ||
let params = vec![ | ||
("vs_currency", "usd".to_string()), | ||
("per_page", page_size.to_string()), | ||
("ids", ids.join(",")), | ||
("page", page.to_string()), | ||
]; | ||
|
||
let builder_with_query = self.client.get(&url).query(¶ms); | ||
let response = send_request(builder_with_query).await?; | ||
let market_data = parse_response::<Vec<Market>>(response).await?; | ||
let mut market_data_map: HashMap<String, Market> = | ||
HashMap::from_iter(market_data.into_iter().map(|m| (m.id.clone(), m))); | ||
Ok(ids | ||
.iter() | ||
.map(|id| market_data_map.remove(&id.to_string())) | ||
.collect()) | ||
} | ||
} | ||
|
||
async fn send_request(request_builder: RequestBuilder) -> Result<Response, Error> { | ||
let response = request_builder.send().await?; | ||
|
||
let status = response.status(); | ||
if status.is_client_error() || status.is_server_error() { | ||
return Err(Error::Http(status)); | ||
} | ||
|
||
Ok(response) | ||
} | ||
|
||
async fn parse_response<T: serde::de::DeserializeOwned>(response: Response) -> Result<T, Error> { | ||
Ok(response.json::<T>().await?) | ||
} |
1 change: 0 additions & 1 deletion
1
bothan-coin-gecko/src/api/types.rs → bothan-coingecko/src/api/types.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.