Skip to content

Commit

Permalink
fix from comments
Browse files Browse the repository at this point in the history
  • Loading branch information
colmazia committed Oct 1, 2024
1 parent e3ebd61 commit 62ef731
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 32 deletions.
10 changes: 5 additions & 5 deletions bothan-coinmarketcap/src/api/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl CoinMarketCapRestAPI {
/// Equivalent to the `/v2/cryptocurrency/quotes/latest` endpoint.
pub async fn get_latest_quotes(
&self,
ids: &[&str],
ids: &[usize],
) -> Result<Vec<Option<Quote>>, RestAPIError> {
let url = format!("{}v2/cryptocurrency/quotes/latest", self.url);
let ids_string = ids.iter().map(|id| id.to_string()).join(",");
Expand Down Expand Up @@ -158,7 +158,7 @@ pub(crate) mod test {
let quotes = vec![mock_quote()];
let mock = server.set_successful_quotes(&["1"], &quotes);

let result = client.get_latest_quotes(&["1"]).await;
let result = client.get_latest_quotes(&[1]).await;
let expected_result = quotes.into_iter().map(Some).collect();
mock.assert();
assert_eq!(result, Ok(expected_result));
Expand All @@ -172,7 +172,7 @@ pub(crate) mod test {

let mock = server.set_successful_quotes(&["1", "0"], &quotes);

let result = client.get_latest_quotes(&["1", "0"]).await;
let result = client.get_latest_quotes(&[1, 0]).await;

mock.assert();
let expected_result = vec![Some(quotes[0].clone()), None];
Expand All @@ -185,7 +185,7 @@ pub(crate) mod test {

let mock = server.set_arbitrary_quotes(&["1"], "abc");

let result = client.get_latest_quotes(&["1"]).await;
let result = client.get_latest_quotes(&[1]).await;

mock.assert();

Expand All @@ -198,7 +198,7 @@ pub(crate) mod test {
let (mut server, client) = setup().await;
let mock = server.set_failed_quotes(&["1"]);

let result = client.get_latest_quotes(&["1"]).await;
let result = client.get_latest_quotes(&[1]).await;
mock.assert();
assert!(result.is_err());
}
Expand Down
23 changes: 13 additions & 10 deletions bothan-coinmarketcap/src/worker/asset_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ async fn update_asset_info<T: AsRef<str>>(
ids: &[T],
) {
// Convert ids to a slice of &str
let ids_str: Vec<&str> = ids.iter().map(|id| id.as_ref()).collect();
// let ids_str: Vec<&str> = ids.iter().map(|id| id.as_ref()).collect();
let parsed_ids = ids
.iter()
.filter_map(|s| s.as_ref().parse().ok())
.collect::<Vec<usize>>();

match api.get_latest_quotes(&ids_str).await {
match api.get_latest_quotes(parsed_ids.as_slice()).await {
Ok(quotes) => {
let mut to_set = Vec::new();

for (id, quote) in ids_str.iter().zip(quotes.iter()) {
for (id, quote) in parsed_ids.iter().zip(quotes.iter()) {
let quote = match quote {
Some(price) => price,
None => {
Expand All @@ -70,7 +74,7 @@ async fn update_asset_info<T: AsRef<str>>(
}
};

match parse_quote(id, quote) {
match parse_quote(quote) {
Ok(asset_info) => to_set.push((id.to_string(), asset_info)),
Err(_) => warn!("failed to parse price data for id: {}", id),
}
Expand All @@ -85,7 +89,8 @@ async fn update_asset_info<T: AsRef<str>>(
}
}

pub(crate) fn parse_quote(id: &str, quote: &Quote) -> Result<AssetInfo, ParseError> {
pub(crate) fn parse_quote(quote: &Quote) -> Result<AssetInfo, ParseError> {
let id = quote.id.to_string();
let price = quote
.price_quotes
.usd
Expand Down Expand Up @@ -114,7 +119,6 @@ mod test {

#[test]
fn test_parse_quote() {
let id = "1";
let price = 8426.69;
let timestamp = "2021-01-01T00:00:00.000Z";

Expand All @@ -140,15 +144,14 @@ mod test {
},
};

let asset_info = parse_quote(id, &quote).unwrap();
assert_eq!(asset_info.id, id);
let asset_info = parse_quote(&quote).unwrap();
assert_eq!(asset_info.id, quote.id.to_string());
assert_eq!(asset_info.price, Decimal::from_str("8426.69").unwrap());
assert_eq!(asset_info.timestamp, 1609459200);
}

#[test]
fn test_parse_quote_with_failure() {
let id = "1";
let price = f64::INFINITY;
let timestamp = "2021-01-01T00:00:00.000Z";

Expand All @@ -174,6 +177,6 @@ mod test {
},
};

assert!(parse_quote(id, &quote).is_err());
assert!(parse_quote(&quote).is_err());
}
}
7 changes: 0 additions & 7 deletions bothan-coinmarketcap/src/worker/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ impl CoinMarketCapWorkerBuilder {
self
}

/// Sets the update supported assets interval for the `CoinMarketCapWorker`.
/// The default interval is `DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL`.
pub fn with_update_supported_assets_interval(mut self, update_interval: Duration) -> Self {
self.opts.update_supported_assets_interval = update_interval;
self
}

/// Sets the store for the `CoinMarketCapWorker`.
/// If not set, the store is created and owned by the worker.
pub fn with_store(mut self, store: WorkerStore) -> Self {
Expand Down
10 changes: 1 addition & 9 deletions bothan-coinmarketcap/src/worker/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;
use serde::{Deserialize, Serialize};

use crate::api::types::DEFAULT_URL;
use crate::worker::types::{DEFAULT_UPDATE_INTERVAL, DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL};
use crate::worker::types::DEFAULT_UPDATE_INTERVAL;

/// Options for configuring the `CoinMarketCapWorkerBuilder`.
///
Expand All @@ -19,9 +19,6 @@ pub struct CoinMarketCapWorkerBuilderOpts {
#[serde(default = "default_update_interval")]
#[serde(with = "humantime_serde")]
pub update_interval: Duration,
#[serde(default = "default_update_supported_assets_interval")]
#[serde(with = "humantime_serde")]
pub update_supported_assets_interval: Duration,
}

fn default_url() -> String {
Expand All @@ -32,17 +29,12 @@ fn default_update_interval() -> Duration {
DEFAULT_UPDATE_INTERVAL
}

fn default_update_supported_assets_interval() -> Duration {
DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL
}

impl Default for CoinMarketCapWorkerBuilderOpts {
fn default() -> Self {
Self {
url: default_url(),
api_key: None,
update_interval: default_update_interval(),
update_supported_assets_interval: default_update_supported_assets_interval(),
}
}
}
1 change: 0 additions & 1 deletion bothan-coinmarketcap/src/worker/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use tokio::time::Duration;

pub(crate) const DEFAULT_UPDATE_INTERVAL: Duration = Duration::from_secs(60);
pub(crate) const DEFAULT_UPDATE_SUPPORTED_ASSETS_INTERVAL: Duration = Duration::from_secs(86400);

0 comments on commit 62ef731

Please sign in to comment.