diff --git a/src/marketstack.rs b/src/marketstack.rs index 694aba4..580ecd5 100644 --- a/src/marketstack.rs +++ b/src/marketstack.rs @@ -299,4 +299,27 @@ impl AsyncMarketstack { }; call().map_err(api::ApiError::client).await } + + /// Create a new AyncMarketstack API representation. + /// + /// The `token` should be a valid [personal access token](https://marketstack.com/documentation). + /// Errors out if `token` is invalid. + pub async fn new(host: H, token: T) -> MarketstackResult + where + H: AsRef, + T: Into, + { + Self::new_impl("https", host.as_ref(), Auth::Token(token.into())).await + } + + /// Create a new non-SSL AsyncMarketstack API representation. + /// + /// A `token` will still be required for insecure access. + pub async fn new_insecure(host: H, token: T) -> MarketstackResult + where + H: AsRef, + T: Into, + { + Self::new_impl("http", host.as_ref(), Auth::Token(token.into())).await + } } diff --git a/src/test/client.rs b/src/test/client.rs index 2a5cefe..25fe11f 100644 --- a/src/test/client.rs +++ b/src/test/client.rs @@ -45,11 +45,6 @@ impl ExpectedUrlBuilder { .extend(pairs.iter().cloned().map(|(k, v)| (k.into(), v.into()))); self } - - pub fn body_str(&mut self, body: &str) -> &mut Self { - self.body = Some(body.bytes().collect()); - self - } } impl ExpectedUrl { @@ -249,19 +244,6 @@ pub struct PagedTestClient { const KEYSET_QUERY_PARAM: &str = "__test_keyset"; const DEFAULT_PAGE_SIZE: usize = 20; -impl PagedTestClient { - pub fn new_raw(expected: ExpectedUrl, data: I) -> Self - where - I: IntoIterator, - { - Self { - expected, - data: data.into_iter().collect(), - auth: Auth::Token("".into()), - } - } -} - impl RestClient for PagedTestClient { type Error = TestClientError; diff --git a/tests/eod_test.rs b/tests/eod_test.rs index 48c2123..6cb21be 100644 --- a/tests/eod_test.rs +++ b/tests/eod_test.rs @@ -1,5 +1,5 @@ -use marketstack::api::{eod, Query}; -use marketstack::{EodData, Marketstack}; +use marketstack::api::{eod, AsyncQuery, Query}; +use marketstack::{AsyncMarketstack, EodData, Marketstack}; mod setup; @@ -35,3 +35,40 @@ fn test_eod_paged() { assert_eq!(eod_result.pagination.limit, 5); assert_eq!(eod_result.data.len(), 5); } + +#[tokio::test] +#[ignore] +async fn test_async_eod() { + let api_key = setup::setup_key(); + let client = AsyncMarketstack::new_insecure("api.marketstack.com", api_key) + .await + .unwrap(); + + let endpoint = eod::Eod::builder().symbol("AAPL").build().unwrap(); + let eod_result: EodData = endpoint.query_async(&client).await.unwrap(); + + assert_eq!(eod_result.pagination.limit, 100); + assert_eq!(eod_result.pagination.offset, 0); + + assert_eq!(eod_result.data.len(), 100); +} + +#[tokio::test] +#[ignore] +async fn test_async_eod_paged() { + let api_key = setup::setup_key(); + let client = AsyncMarketstack::new_insecure("api.marketstack.com", api_key) + .await + .unwrap(); + + let endpoint = eod::Eod::builder() + .symbol("AAPL") + .limit(5) + .unwrap() + .build() + .unwrap(); + let eod_result: EodData = endpoint.query_async(&client).await.unwrap(); + + assert_eq!(eod_result.pagination.limit, 5); + assert_eq!(eod_result.data.len(), 5); +}