Skip to content

Commit

Permalink
add basic logging to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Jan 17, 2022
1 parent 92ba0c1 commit c9dc0e8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
36 changes: 28 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -70,8 +71,7 @@ where
pub(crate) struct Response {
accounts: Vec<accounts::Account>,
}
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)
}
Expand All @@ -95,7 +95,7 @@ where
/// # }
/// ```
pub async fn balance(&self, account_id: &str) -> Result<balance::Balance> {
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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -168,7 +168,7 @@ where
source_account_id: &str,
amount: u32,
) -> Result<pots::Pot> {
send_and_resolve_request(
handle_request(
&self.inner_client,
&pots::Deposit::new(pot_id, source_account_id, amount),
)
Expand All @@ -182,7 +182,7 @@ where
destination_account_id: &str,
amount: u32,
) -> Result<pots::Pot> {
send_and_resolve_request(
handle_request(
&self.inner_client,
&pots::Withdraw::new(pot_id, destination_account_id, amount),
)
Expand Down Expand Up @@ -251,16 +251,36 @@ where

/// Return information about the current session
pub async fn who_am_i(&self) -> Result<who_am_i::Response> {
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<R>(client: &dyn Inner, endpoint: &dyn Endpoint) -> Result<R>
#[instrument(skip(client, endpoint), fields(endpoint = endpoint.endpoint()))]
pub async fn handle_request<R>(client: &dyn Inner, endpoint: &dyn Endpoint) -> Result<R>
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<R>(response: reqwest::Response) -> Result<R>
where
R: DeserializeOwned,
{
let status = response.status();

if status.is_success() {
Expand Down
4 changes: 2 additions & 2 deletions src/client/inner/refreshable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Client<Refreshable> {

/// Hit the Monzo auth endpoint and request new access and refresh tokens
async fn get_refresh_tokens(&self) -> Result<auth::RefreshResponse> {
send_and_resolve_request(
handle_request(
&self.inner_client,
&auth::Refresh::new(self.client_id(), self.client_secret(), self.refresh_token()),
)
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/feed_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/transactions/get.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Transaction;
use crate::{
client::{self, send_and_resolve_request},
client::{self, handle_request},
endpoints::Endpoint,
Result,
};
Expand Down Expand Up @@ -53,6 +53,6 @@ impl<'a> Request<'a> {

/// Consume the request and return the [`Transaction`]
pub async fn send(self) -> Result<Transaction> {
send_and_resolve_request(self.client, &self).await
handle_request(self.client, &self).await
}
}
4 changes: 2 additions & 2 deletions src/endpoints/transactions/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<'a> Request<'a> {
transactions: Vec<Transaction>,
}

let response: Response = send_and_resolve_request(self.client, &self).await?;
let response: Response = handle_request(self.client, &self).await?;

Ok(response.transactions)
}
Expand Down

0 comments on commit c9dc0e8

Please sign in to comment.