-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add CoinGecko API integration for fetching coin mappings
- Loading branch information
Showing
4 changed files
with
74 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Coin { | ||
id: String, | ||
symbol: String, | ||
} | ||
|
||
fn to_usd_pair(symbol: String) -> String { | ||
format!("{}/USD", symbol) | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum CoinGeckoError { | ||
#[error("Empty response from CoinGecko API")] | ||
EmptyResponse, | ||
#[error("Coingecko Coins API request failed with status: {0}")] | ||
RequestFailed(String), | ||
#[error("Coingecko Coins Failed to parse response: {0}")] | ||
ParseError(#[from] reqwest::Error), | ||
} | ||
|
||
pub async fn get_coingecko_mappings() -> Result<HashMap<String, String>, CoinGeckoError> { | ||
let client = reqwest::Client::new(); | ||
let mut mappings = HashMap::new(); | ||
let mut page = 1; | ||
|
||
loop { | ||
let url = format!( | ||
"https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page={}", | ||
page | ||
); | ||
|
||
let response = client | ||
.get(&url) | ||
.header("User-Agent", "Crypto Data Fetcher") | ||
.send() | ||
.await | ||
.map_err(CoinGeckoError::ParseError)?; | ||
|
||
if !response.status().is_success() { | ||
return Err(CoinGeckoError::RequestFailed(response.status().to_string())); | ||
} | ||
|
||
let coins: Vec<Coin> = response.json().await?; | ||
if coins.is_empty() { | ||
if page == 1 { | ||
return Err(CoinGeckoError::EmptyResponse); | ||
} | ||
break; | ||
} | ||
|
||
coins.into_iter().for_each(|coin| { | ||
mappings.insert(to_usd_pair(coin.symbol.to_uppercase()), coin.id); | ||
}); | ||
|
||
page += 1; | ||
} | ||
|
||
Ok(mappings) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod coingecko; | ||
pub(crate) mod config; | ||
pub mod constants; | ||
pub mod models; | ||
|
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 |
---|---|---|
|
@@ -21,6 +21,8 @@ mod constants; | |
mod types; | ||
// Utils | ||
mod utils; | ||
// coingecko | ||
mod coingecko; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|