-
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
00fff8f
commit a702fc4
Showing
2 changed files
with
128 additions
and
0 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,60 @@ | ||
use reqwest::header::{HeaderMap, HeaderValue}; | ||
use reqwest::ClientBuilder; | ||
use url::Url; | ||
|
||
use crate::api::error::Error; | ||
use crate::api::types::{DEFAULT_PRO_URL, DEFAULT_URL, DEFAULT_USER_AGENT}; | ||
use crate::api::CoinGeckoRestAPI; | ||
|
||
#[derive(Default)] | ||
pub struct CoinGeckoRestAPIBuilder { | ||
url: Option<String>, | ||
api_key: Option<String>, | ||
user_agent: Option<String>, | ||
} | ||
|
||
impl CoinGeckoRestAPIBuilder { | ||
pub fn set_url(&mut self, url: &str) -> &Self { | ||
self.url = Some(url.into()); | ||
self | ||
} | ||
|
||
pub fn set_api_key(&mut self, api_key: &str) -> &Self { | ||
self.api_key = Some(api_key.into()); | ||
self | ||
} | ||
|
||
pub fn set_user_agent(&mut self, user_agent: &str) -> &Self { | ||
self.user_agent = Some(user_agent.into()); | ||
self | ||
} | ||
|
||
pub fn build(self) -> Result<CoinGeckoRestAPI, Error> { | ||
let mut headers = HeaderMap::new(); | ||
|
||
let user_agent = match &self.user_agent { | ||
Some(user_agent) => user_agent, | ||
None => DEFAULT_USER_AGENT, | ||
}; | ||
headers.insert("User-Agent", HeaderValue::from_str(user_agent)?); | ||
|
||
if let Some(key) = &self.api_key { | ||
let mut val = HeaderValue::from_str(key)?; | ||
val.set_sensitive(true); | ||
headers.insert("x-cg-pro-api-key", val); | ||
} | ||
|
||
let url = match &self.url { | ||
Some(url) => url, | ||
None => match &self.api_key { | ||
Some(_) => DEFAULT_PRO_URL, | ||
None => DEFAULT_URL, | ||
}, | ||
}; | ||
|
||
let parsed_url = Url::parse(url)?; | ||
let client = ClientBuilder::new().default_headers(headers).build()?; | ||
|
||
Ok(CoinGeckoRestAPI::new(parsed_url, client)) | ||
} | ||
} |
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,68 @@ | ||
use tokio::time::Duration; | ||
|
||
use crate::api::CoinGeckoRestAPIBuilder; | ||
use crate::error::Error; | ||
use crate::types::{DEFAULT_UPDATE_INTERVAL, DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL}; | ||
use crate::CoinGeckoService; | ||
|
||
#[derive(Default)] | ||
pub struct CoinGeckoServiceBuilder { | ||
url: Option<String>, | ||
api_key: Option<String>, | ||
user_agent: Option<String>, | ||
update_interval: Option<Duration>, | ||
update_supported_assets_interval: Option<Duration>, | ||
} | ||
|
||
impl CoinGeckoServiceBuilder { | ||
pub fn set_url(mut self, url: &str) -> Self { | ||
self.url = Some(url.into()); | ||
self | ||
} | ||
|
||
pub fn set_api_key(mut self, api_key: &str) -> Self { | ||
self.api_key = Some(api_key.into()); | ||
self | ||
} | ||
|
||
pub fn set_user_agent(mut self, user_agent: &str) -> Self { | ||
self.user_agent = Some(user_agent.into()); | ||
self | ||
} | ||
|
||
pub fn set_update_interval(mut self, update_interval: Duration) -> Self { | ||
self.update_interval = Some(update_interval); | ||
self | ||
} | ||
|
||
pub fn set_update_supported_assets_interval( | ||
mut self, | ||
update_supported_assets_interval: Duration, | ||
) -> Self { | ||
self.update_supported_assets_interval = Some(update_supported_assets_interval); | ||
self | ||
} | ||
|
||
pub async fn build(self) -> Result<CoinGeckoService, Error> { | ||
let mut api_builder = CoinGeckoRestAPIBuilder::default(); | ||
if let Some(url) = &self.url { | ||
api_builder.set_url(url); | ||
}; | ||
|
||
if let Some(api_key) = &self.api_key { | ||
api_builder.set_api_key(api_key); | ||
}; | ||
|
||
if let Some(user_agent) = &self.user_agent { | ||
api_builder.set_user_agent(user_agent); | ||
}; | ||
|
||
let update_interval = self.update_interval.unwrap_or(DEFAULT_UPDATE_INTERVAL); | ||
let update_supported_assets_interval = self | ||
.update_supported_assets_interval | ||
.unwrap_or(DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL); | ||
|
||
let api = api_builder.build()?; | ||
Ok(CoinGeckoService::new(api, update_interval, update_supported_assets_interval).await) | ||
} | ||
} |