Skip to content

Commit

Permalink
refactor inner client
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Nov 28, 2023
1 parent 444046f commit 69eeb49
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 60 deletions.
79 changes: 39 additions & 40 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pub mod inner;

/// A generic trait of any HTTP client which also stores and manages an access
/// token.
#[allow(async_fn_in_trait)]
pub trait Inner: Send + Sync + std::fmt::Debug {
/// Construct end send an HTTP request using the provided Endpoint with
/// bearer token authentication.
#[allow(async_fn_in_trait)]
async fn execute<E>(&self, endpoint: &E) -> reqwest::Result<reqwest::Response>
where
E: Endpoint;
Expand All @@ -28,6 +28,29 @@ pub trait Inner: Send + Sync + std::fmt::Debug {

/// The base URL of the API
fn url(&self) -> &str;

#[instrument(skip(self, endpoint), fields(url = self.url(), endpoint = endpoint.endpoint()))]

Check warning on line 32 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L32

Added line #L32 was not covered by tests
async fn handle_request<E, R>(&self, endpoint: &E) -> Result<R>
where
R: DeserializeOwned,
E: Endpoint,
{
tracing::info!("sending request");

Check warning on line 38 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L38

Added line #L38 was not covered by tests
let response = self.execute(endpoint).await?;
tracing::info!("response received");

Check warning on line 40 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L40

Added line #L40 was not covered by tests

let result = handle_response(response).await;

match &result {
Ok(_) => {
tracing::info!("request successful");

Check warning on line 46 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L46

Added line #L46 was not covered by tests
}
Err(e) => {
tracing::info!("request failed: {}", e);

Check warning on line 49 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L49

Added line #L49 was not covered by tests
}
};
result
}
}

/// A Monzo API client
Expand Down Expand Up @@ -75,7 +98,7 @@ where
pub struct Response {
accounts: Vec<accounts::Account>,
}
let response: Response = handle_request(&self.inner_client, &accounts::List).await?;
let response: Response = self.inner_client.handle_request(&accounts::List).await?;

Check warning on line 101 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L101

Added line #L101 was not covered by tests

Ok(response.accounts)
}
Expand All @@ -99,7 +122,9 @@ where
/// # }
/// ```
pub async fn balance(&self, account_id: &str) -> Result<balance::Balance> {
handle_request(&self.inner_client, &balance::Get::new(account_id)).await
self.inner_client
.handle_request(&balance::Get::new(account_id))
.await

Check warning on line 127 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L125-L127

Added lines #L125 - L127 were not covered by tests
}

/// Return a list of Pots
Expand Down Expand Up @@ -127,8 +152,10 @@ where
pots: Vec<pots::Pot>,
}

let response: Response =
handle_request(&self.inner_client, &pots::List::new(account_id)).await?;
let response: Response = self
.inner_client
.handle_request(&pots::List::new(account_id))
.await?;

Check warning on line 158 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L155-L158

Added lines #L155 - L158 were not covered by tests

Ok(response.pots)
}
Expand Down Expand Up @@ -172,11 +199,9 @@ where
source_account_id: &str,
amount: u32,
) -> Result<pots::Pot> {
handle_request(
&self.inner_client,
&pots::Deposit::new(pot_id, source_account_id, amount),
)
.await
self.inner_client
.handle_request(&pots::Deposit::new(pot_id, source_account_id, amount))
.await

Check warning on line 204 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L202-L204

Added lines #L202 - L204 were not covered by tests
}

/// Withdraw money from a pot
Expand All @@ -186,11 +211,9 @@ where
destination_account_id: &str,
amount: u32,
) -> Result<pots::Pot> {
handle_request(
&self.inner_client,
&pots::Withdraw::new(pot_id, destination_account_id, amount),
)
.await
self.inner_client
.handle_request(&pots::Withdraw::new(pot_id, destination_account_id, amount))
.await

Check warning on line 216 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L214-L216

Added lines #L214 - L216 were not covered by tests
}

/// Get a list of transactions
Expand Down Expand Up @@ -255,34 +278,10 @@ where

/// Return information about the current session
pub async fn who_am_i(&self) -> Result<who_am_i::Response> {
handle_request(&self.inner_client, &who_am_i::Request).await
self.inner_client.handle_request(&who_am_i::Request).await

Check warning on line 281 in src/client.rs

View check run for this annotation

Codecov / codecov/patch

src/client.rs#L281

Added line #L281 was not covered by tests
}
}

#[instrument(skip(client, endpoint), fields(url = client.url(), endpoint = endpoint.endpoint()))]
pub async fn handle_request<R, C, E>(client: &C, endpoint: &E) -> Result<R>
where
R: DeserializeOwned,
C: Inner,
E: Endpoint,
{
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,
Expand Down
14 changes: 8 additions & 6 deletions src/client/inner/refreshable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
client,
client::{handle_request, Client},
client::{Client, Inner},
endpoints::{auth, Endpoint},
Result,
};
Expand Down Expand Up @@ -56,11 +56,13 @@ impl Client<Refreshable> {

/// Hit the Monzo auth endpoint and request new access and refresh tokens
async fn get_refresh_tokens(&self) -> Result<auth::RefreshResponse> {
handle_request(
&self.inner_client,
&auth::Refresh::new(self.client_id(), self.client_secret(), self.refresh_token()),
)
.await
self.inner_client
.handle_request(&auth::Refresh::new(
self.client_id(),
self.client_secret(),
self.refresh_token(),
))
.await

Check warning on line 65 in src/client/inner/refreshable.rs

View check run for this annotation

Codecov / codecov/patch

src/client/inner/refreshable.rs#L59-L65

Added lines #L59 - L65 were not covered by tests
}

/// Refresh the access and refresh tokens for this client
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::handle_request, endpoints::Endpoint, Result};
use crate::{client, endpoints::Endpoint, Result};

/// A request to create a new basic feed item.
///
Expand Down Expand Up @@ -102,7 +102,7 @@ pub(crate) mod basic {

/// Consume and send the [`Request`].
pub async fn send(self) -> Result<()> {
handle_request(self.client, &self).await
self.client.handle_request(&self).await

Check warning on line 105 in src/endpoints/feed_items.rs

View check run for this annotation

Codecov / codecov/patch

src/endpoints/feed_items.rs#L105

Added line #L105 was not covered by tests
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/endpoints/transactions/get.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use super::Transaction;
use crate::{
client::{self, handle_request},
endpoints::Endpoint,
Result,
};
use crate::{client, endpoints::Endpoint, Result};

/// A request to retrieve a list of transactions from the Monzo API
///
Expand Down Expand Up @@ -60,6 +56,6 @@ where

/// Consume the request and return the [`Transaction`]
pub async fn send(self) -> Result<Transaction> {
handle_request(self.client, &self).await
self.client.handle_request(&self).await

Check warning on line 59 in src/endpoints/transactions/get.rs

View check run for this annotation

Codecov / codecov/patch

src/endpoints/transactions/get.rs#L59

Added line #L59 was not covered by tests
}
}
8 changes: 2 additions & 6 deletions src/endpoints/transactions/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use super::{Pagination, Since, Transaction};
use crate::{
client::{self, handle_request},
endpoints::Endpoint,
Result,
};
use crate::{client, endpoints::Endpoint, Result};

/// A request to retrieve a list of transactions from the Monzo API
///
Expand Down Expand Up @@ -90,7 +86,7 @@ where
transactions: Vec<Transaction>,
}

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

Check warning on line 89 in src/endpoints/transactions/list.rs

View check run for this annotation

Codecov / codecov/patch

src/endpoints/transactions/list.rs#L89

Added line #L89 was not covered by tests

Ok(response.transactions)
}
Expand Down

0 comments on commit 69eeb49

Please sign in to comment.