Skip to content

Commit

Permalink
fixed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
warittornc committed Feb 23, 2024
1 parent c4f7807 commit 3b58fef
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 169 deletions.
15 changes: 13 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ edition = "2021"
license = "MIT OR Apache-2.0"

[workspace]
members = ["price-adapter-raw", "price-adapter", "bothan-core", "bothan-binance", "bothan-coin-gecko"]
members = ["bothan-binance", "bothan-coingecko", "bothan-core", "price-adapter", "price-adapter-raw"]
resolver = "2"

[workspace.dependencies]
futures = "0.3.30"
async-trait = "0.1.77"
reqwest = { version = "0.11.24", features = ["json"] }
thiserror = "1.0.57"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
serde = { version = "1.0.197", features = ["std", "derive", "alloc"] }
tokio = { version = "1.36.0", features = ["full"] }
tokio-util = "0.7.10"
derive_more = { version = "1.0.0-beta.6", features = ["full"] }

price-adapter = { version = "0.1.8", path = "price-adapter" }
price-adapter-raw = { version = "0.1.8", path = "price-adapter-raw" }
bothan-core = { version = "0.1.0", path = "bothan-core" }
bothan-binance = { version = "0.1.0", path = "bothan-binance" }
bothan-coin-gecko = { version = "0.1.0", path = "bothan-coin-gecko" }
bothan-coingecko = { version = "0.1.0", path = "bothan-coingecko" }
25 changes: 9 additions & 16 deletions bothan-binance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@ 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]
reqwest = { version = "0.11.22", features = ["json"] }
thiserror = "1.0.56"
tracing = { version = "0.1.40", features = [] }
tokio = { version = "1.36.0", features = ["full"] }
tracing-subscriber = "0.3.17"
serde = { version = "1.0.196", features = ["std", "derive", "alloc"] }
serde_json = "1.0.108"
itertools = "0.12.0"
serde_json = "1.0.114"
tokio-tungstenite = { version = "0.21.0", features = ["native-tls"] }
futures-util = "0.3.29"
tokio-util = "0.7.10"
chrono = "0.4.31"
rand = "0.8.4"
dashmap = "5.5.3"
derive_more = { version = "1.0.0-beta.6", features = ["full"] }
futures = "0.3.30"
bothan-core = { path = "../bothan-core" }
async-trait = "0.1.77"

bothan-core = { workspace = true }
async-trait = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
serde = { workspace = true }
tokio = { workspace = true }
21 changes: 0 additions & 21 deletions bothan-coin-gecko/Cargo.toml

This file was deleted.

85 changes: 0 additions & 85 deletions bothan-coin-gecko/src/api/rest.rs

This file was deleted.

20 changes: 20 additions & 0 deletions bothan-coingecko/Cargo.toml
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 }
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use tracing_subscriber::fmt::init;

use bothan_coin_gecko::CoinGeckoServiceBuilder;
use bothan_coingecko::CoinGeckoServiceBuilder;
use bothan_core::service::Service;

#[tokio::main]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@ 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: String,
}

impl CoinGeckoRestAPIBuilder {
pub fn new() -> Self {
CoinGeckoRestAPIBuilder {
url: None,
api_key: None,
user_agent: DEFAULT_USER_AGENT.into(),
}
}

pub fn set_url(&mut self, url: &str) -> &Self {
self.url = Some(url.into());
self
Expand Down Expand Up @@ -61,3 +52,13 @@ impl CoinGeckoRestAPIBuilder {
Ok(CoinGeckoRestAPI::new(parsed_url, client))
}
}

impl Default for CoinGeckoRestAPIBuilder {
fn default() -> Self {
CoinGeckoRestAPIBuilder {
url: None,
api_key: None,
user_agent: DEFAULT_USER_AGENT.into(),
}
}
}
File renamed without changes.
65 changes: 65 additions & 0 deletions bothan-coingecko/src/api/rest.rs
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(&params);
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?)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde::{Deserialize, Serialize};

pub(crate) const MAX_PAGE_SIZE: usize = 250;
pub(crate) const DEFAULT_USER_AGENT: &str = "Bothan";
pub(crate) const DEFAULT_URL: &str = "https://api.coingecko.com/api/v3/";
pub(crate) const DEFAULT_PRO_URL: &str = "https://pro-api.coingecko.com/api/v3/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@ use tokio::time::Duration;
use crate::api::types::DEFAULT_USER_AGENT;
use crate::api::CoinGeckoRestAPIBuilder;
use crate::error::Error;
use crate::types::{DEFAULT_UPDATE_INTERVAL, DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL};
use crate::types::{
DEFAULT_PAGE_SIZE, 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: String,
update_interval: Duration,
update_supported_assets_interval: Duration,
page_size: usize,
page_query_delay: Option<Duration>,
}

impl CoinGeckoServiceBuilder {
pub fn new() -> Self {
CoinGeckoServiceBuilder {
url: None,
api_key: None,
user_agent: DEFAULT_USER_AGENT.into(),
update_interval: DEFAULT_UPDATE_INTERVAL,
update_supported_assets_interval: DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL,
}
}
pub fn set_url(mut self, url: &str) -> Self {
self.url = Some(url.into());
self
Expand Down Expand Up @@ -53,6 +47,16 @@ impl CoinGeckoServiceBuilder {
self
}

pub fn set_page_size(mut self, page_size: usize) -> Self {
self.page_size = page_size;
self
}

pub fn set_page_query_delay(mut self, page_query_delay: Duration) -> Self {
self.page_query_delay = Some(page_query_delay);
self
}

pub async fn build(self) -> Result<CoinGeckoService, Error> {
let mut api_builder = CoinGeckoRestAPIBuilder::default();
if let Some(url) = &self.url {
Expand All @@ -64,11 +68,29 @@ impl CoinGeckoServiceBuilder {
api_builder.set_user_agent(&self.user_agent);
let api = api_builder.build()?;

Ok(CoinGeckoService::new(
let service = CoinGeckoService::new(
api,
self.update_interval,
self.update_supported_assets_interval,
self.page_size,
self.page_query_delay,
)
.await)
.await;

Ok(service)
}
}

impl Default for CoinGeckoServiceBuilder {
fn default() -> Self {
CoinGeckoServiceBuilder {
url: None,
api_key: None,
user_agent: DEFAULT_USER_AGENT.into(),
update_interval: DEFAULT_UPDATE_INTERVAL,
update_supported_assets_interval: DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL,
page_size: DEFAULT_PAGE_SIZE,
page_query_delay: None,
}
}
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 3b58fef

Please sign in to comment.