Formatting fixes

The result of `cargo fmt`.
This commit is contained in:
2025-11-22 16:23:53 +00:00
parent b8f8d8cdfb
commit e4b36d344c
15 changed files with 701 additions and 373 deletions

View File

@@ -1,9 +1,11 @@
use crate::models::{
Account, EndUserAgreement, PaginatedResponse, Requisition, TokenResponse, TransactionsResponse,
};
use reqwest::Url;
use reqwest_middleware::ClientWithMiddleware;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tracing::{debug, instrument};
use crate::models::{TokenResponse, PaginatedResponse, Requisition, Account, TransactionsResponse, EndUserAgreement};
#[derive(Error, Debug)]
pub enum GoCardlessError {
@@ -39,10 +41,17 @@ impl GoCardlessClient {
Self::with_client(base_url, secret_id, secret_key, None)
}
pub fn with_client(base_url: &str, secret_id: &str, secret_key: &str, client: Option<ClientWithMiddleware>) -> Result<Self, GoCardlessError> {
pub fn with_client(
base_url: &str,
secret_id: &str,
secret_key: &str,
client: Option<ClientWithMiddleware>,
) -> Result<Self, GoCardlessError> {
Ok(Self {
base_url: Url::parse(base_url)?,
client: client.unwrap_or_else(|| reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build()),
client: client.unwrap_or_else(|| {
reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build()
}),
secret_id: secret_id.to_string(),
secret_key: secret_key.to_string(),
access_token: None,
@@ -67,40 +76,47 @@ impl GoCardlessClient {
};
debug!("Requesting new access token");
let response = self.client.post(url)
.json(&body)
.send()
.await?;
let response = self.client.post(url).json(&body).send().await?;
if !response.status().is_success() {
let status = response.status();
let text = response.text().await?;
return Err(GoCardlessError::ApiError(format!("Token request failed {}: {}", status, text)));
return Err(GoCardlessError::ApiError(format!(
"Token request failed {}: {}",
status, text
)));
}
let token_resp: TokenResponse = response.json().await?;
self.access_token = Some(token_resp.access);
self.access_expires_at = Some(chrono::Utc::now() + chrono::Duration::seconds(token_resp.access_expires as i64));
self.access_expires_at =
Some(chrono::Utc::now() + chrono::Duration::seconds(token_resp.access_expires as i64));
debug!("Access token obtained");
Ok(())
}
#[instrument(skip(self))]
pub async fn get_requisitions(&self) -> Result<PaginatedResponse<Requisition>, GoCardlessError> {
pub async fn get_requisitions(
&self,
) -> Result<PaginatedResponse<Requisition>, GoCardlessError> {
let url = self.base_url.join("/api/v2/requisitions/")?;
self.get_authenticated(url).await
}
#[instrument(skip(self))]
pub async fn get_agreements(&self) -> Result<PaginatedResponse<EndUserAgreement>, GoCardlessError> {
pub async fn get_agreements(
&self,
) -> Result<PaginatedResponse<EndUserAgreement>, GoCardlessError> {
let url = self.base_url.join("/api/v2/agreements/enduser/")?;
self.get_authenticated(url).await
}
#[instrument(skip(self))]
pub async fn get_agreement(&self, id: &str) -> Result<EndUserAgreement, GoCardlessError> {
let url = self.base_url.join(&format!("/api/v2/agreements/enduser/{}/", id))?;
let url = self
.base_url
.join(&format!("/api/v2/agreements/enduser/{}/", id))?;
self.get_authenticated(url).await
}
@@ -132,9 +148,16 @@ impl GoCardlessClient {
}
#[instrument(skip(self))]
pub async fn get_transactions(&self, account_id: &str, date_from: Option<&str>, date_to: Option<&str>) -> Result<TransactionsResponse, GoCardlessError> {
let mut url = self.base_url.join(&format!("/api/v2/accounts/{}/transactions/", account_id))?;
pub async fn get_transactions(
&self,
account_id: &str,
date_from: Option<&str>,
date_to: Option<&str>,
) -> Result<TransactionsResponse, GoCardlessError> {
let mut url = self
.base_url
.join(&format!("/api/v2/accounts/{}/transactions/", account_id))?;
{
let mut pairs = url.query_pairs_mut();
if let Some(from) = date_from {
@@ -148,19 +171,29 @@ impl GoCardlessClient {
self.get_authenticated(url).await
}
async fn get_authenticated<T: for<'de> Deserialize<'de>>(&self, url: Url) -> Result<T, GoCardlessError> {
let token = self.access_token.as_ref().ok_or(GoCardlessError::ApiError("No access token available. Call obtain_access_token() first.".into()))?;
async fn get_authenticated<T: for<'de> Deserialize<'de>>(
&self,
url: Url,
) -> Result<T, GoCardlessError> {
let token = self.access_token.as_ref().ok_or(GoCardlessError::ApiError(
"No access token available. Call obtain_access_token() first.".into(),
))?;
let response = self.client.get(url)
let response = self
.client
.get(url)
.bearer_auth(token)
.header("accept", "application/json")
.send()
.await?;
if !response.status().is_success() {
let status = response.status();
let text = response.text().await?;
return Err(GoCardlessError::ApiError(format!("API request failed {}: {}", status, text)));
let status = response.status();
let text = response.text().await?;
return Err(GoCardlessError::ApiError(format!(
"API request failed {}: {}",
status, text
)));
}
let data = response.json().await?;