diff --git a/Cargo.toml b/Cargo.toml index d3cef79..ad7338b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ serde_json = "1.0.68" async-trait = "0.1.51" erased-serde = "0.3.16" thiserror = "1.0.30" +tracing = "0.1.29" [dev-dependencies] clap = { version = "3.0.0", features = ["derive", "env"] } diff --git a/src/client.rs b/src/client.rs index 1ca0225..b2ce9ab 100644 --- a/src/client.rs +++ b/src/client.rs @@ -2,6 +2,7 @@ use async_trait::async_trait; use serde::{de::DeserializeOwned, Deserialize}; +use tracing::instrument; use crate::{ endpoints::{accounts, balance, feed_items, pots, transactions, who_am_i, Endpoint}, @@ -70,8 +71,7 @@ where pub(crate) struct Response { accounts: Vec, } - let response: Response = - send_and_resolve_request(&self.inner_client, &accounts::List).await?; + let response: Response = handle_request(&self.inner_client, &accounts::List).await?; Ok(response.accounts) } @@ -95,7 +95,7 @@ where /// # } /// ``` pub async fn balance(&self, account_id: &str) -> Result { - send_and_resolve_request(&self.inner_client, &balance::Get::new(account_id)).await + handle_request(&self.inner_client, &balance::Get::new(account_id)).await } /// Return a list of Pots @@ -124,7 +124,7 @@ where } let response: Response = - send_and_resolve_request(&self.inner_client, &pots::List::new(account_id)).await?; + handle_request(&self.inner_client, &pots::List::new(account_id)).await?; Ok(response.pots) } @@ -168,7 +168,7 @@ where source_account_id: &str, amount: u32, ) -> Result { - send_and_resolve_request( + handle_request( &self.inner_client, &pots::Deposit::new(pot_id, source_account_id, amount), ) @@ -182,7 +182,7 @@ where destination_account_id: &str, amount: u32, ) -> Result { - send_and_resolve_request( + handle_request( &self.inner_client, &pots::Withdraw::new(pot_id, destination_account_id, amount), ) @@ -251,16 +251,36 @@ where /// Return information about the current session pub async fn who_am_i(&self) -> Result { - send_and_resolve_request(&self.inner_client, &who_am_i::Request).await + handle_request(&self.inner_client, &who_am_i::Request).await } } -pub async fn send_and_resolve_request(client: &dyn Inner, endpoint: &dyn Endpoint) -> Result +#[instrument(skip(client, endpoint), fields(endpoint = endpoint.endpoint()))] +pub async fn handle_request(client: &dyn Inner, endpoint: &dyn Endpoint) -> Result where R: DeserializeOwned, { + tracing::info!("sending request"); let response = client.execute(endpoint).await?; + tracing::info!("response received"); + + let result = handle_response(response).await; + match &result { + Ok(_) => { + tracing::info!("request successful"); + } + Err(e) => { + tracing::info!("request failed: {}", e); + } + }; + result +} + +async fn handle_response(response: reqwest::Response) -> Result +where + R: DeserializeOwned, +{ let status = response.status(); if status.is_success() { diff --git a/src/client/inner/refreshable.rs b/src/client/inner/refreshable.rs index b95fb9e..007741c 100644 --- a/src/client/inner/refreshable.rs +++ b/src/client/inner/refreshable.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use crate::{ client, - client::{send_and_resolve_request, Client}, + client::{handle_request, Client}, endpoints::{auth, Endpoint}, Result, }; @@ -58,7 +58,7 @@ impl Client { /// Hit the Monzo auth endpoint and request new access and refresh tokens async fn get_refresh_tokens(&self) -> Result { - send_and_resolve_request( + handle_request( &self.inner_client, &auth::Refresh::new(self.client_id(), self.client_secret(), self.refresh_token()), ) diff --git a/src/endpoints/feed_items.rs b/src/endpoints/feed_items.rs index 948220b..699b36f 100644 --- a/src/endpoints/feed_items.rs +++ b/src/endpoints/feed_items.rs @@ -5,7 +5,7 @@ pub use basic::Request as Basic; pub(crate) mod basic { use serde::Serialize; - use crate::{client, client::send_and_resolve_request, endpoints::Endpoint, Result}; + use crate::{client, client::handle_request, endpoints::Endpoint, Result}; /// A request to create a new basic feed item. /// @@ -96,7 +96,7 @@ pub(crate) mod basic { /// Consume and send the [`Request`]. pub async fn send(self) -> Result<()> { - send_and_resolve_request(self.client, &self).await + handle_request(self.client, &self).await } } diff --git a/src/endpoints/transactions/get.rs b/src/endpoints/transactions/get.rs index c6a930d..c60bd37 100644 --- a/src/endpoints/transactions/get.rs +++ b/src/endpoints/transactions/get.rs @@ -1,6 +1,6 @@ use super::Transaction; use crate::{ - client::{self, send_and_resolve_request}, + client::{self, handle_request}, endpoints::Endpoint, Result, }; @@ -53,6 +53,6 @@ impl<'a> Request<'a> { /// Consume the request and return the [`Transaction`] pub async fn send(self) -> Result { - send_and_resolve_request(self.client, &self).await + handle_request(self.client, &self).await } } diff --git a/src/endpoints/transactions/list.rs b/src/endpoints/transactions/list.rs index 346db74..83b5dd3 100644 --- a/src/endpoints/transactions/list.rs +++ b/src/endpoints/transactions/list.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use super::{Pagination, Since, Transaction}; use crate::{ - client::{self, send_and_resolve_request}, + client::{self, handle_request}, endpoints::Endpoint, Result, }; @@ -83,7 +83,7 @@ impl<'a> Request<'a> { transactions: Vec, } - let response: Response = send_and_resolve_request(self.client, &self).await?; + let response: Response = handle_request(self.client, &self).await?; Ok(response.transactions) }