(feat) added loggind(tracing)
This commit is contained in:
parent
e4cfc5eee5
commit
131ff952ca
10 changed files with 79 additions and 43 deletions
|
|
@ -1,13 +1,38 @@
|
|||
// src/api/consents.rs
|
||||
// Consent request and retrieval logic
|
||||
// Consent request and retrieval logic - Functional style
|
||||
|
||||
use super::{client::{BankClient, BankingError}, models::{ConsentRequestBody, ConsentResponse}};
|
||||
use tracing::{info, debug, error};
|
||||
|
||||
impl BankClient {
|
||||
pub async fn request_consent(&self, client_id: &str) -> Result<ConsentResponse, BankingError> {
|
||||
info!("🔐 Requesting consent for client_id: {}", client_id);
|
||||
|
||||
let body = self.build_consent_request(client_id);
|
||||
let token = self.get_token().await?;
|
||||
|
||||
let body = ConsentRequestBody {
|
||||
debug!("✅ Got bank token");
|
||||
debug!("📤 Sending consent request to bank");
|
||||
|
||||
let response = self.http_client
|
||||
.post(self.base_url.join("/account-consents/request")?)
|
||||
.bearer_auth(token)
|
||||
.header("x-requesting-bank", self.client_id.as_str())
|
||||
.json(&body)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
debug!("📥 Bank response status: {}", response.status());
|
||||
|
||||
response
|
||||
.text()
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
.and_then(|text| self.parse_consent_response(&text))
|
||||
}
|
||||
|
||||
fn build_consent_request(&self, client_id: &str) -> ConsentRequestBody {
|
||||
ConsentRequestBody {
|
||||
client_id: client_id.to_string(),
|
||||
permissions: vec![
|
||||
"ReadAccountsDetail".to_string(),
|
||||
|
|
@ -17,22 +42,24 @@ impl BankClient {
|
|||
reason: "Account aggregation for Multiberry app".to_string(),
|
||||
requesting_bank: self.client_id.clone(),
|
||||
requesting_bank_name: "Multiberry Backend".to_string(),
|
||||
};
|
||||
|
||||
let response = self.http_client
|
||||
.post(self.base_url.join("/account-consents/request")?)
|
||||
.bearer_auth(token)
|
||||
.header("x-requesting-bank", self.client_id.as_str())
|
||||
.json(&body)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
match response.status().is_success() {
|
||||
true => response.json().await.map_err(Into::into),
|
||||
false => Err(BankingError::ApiError {
|
||||
status: response.status().as_u16(),
|
||||
body: response.text().await.unwrap_or_default(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_consent_response(&self, text: &str) -> Result<ConsentResponse, BankingError> {
|
||||
info!("✅ Bank consent response: {}", text);
|
||||
|
||||
serde_json::from_str::<ConsentResponse>(text)
|
||||
.map_err(|e| {
|
||||
error!("❌ Failed to deserialize ConsentResponse: {}", e);
|
||||
error!("Raw response was: {}", text);
|
||||
BankingError::ApiError {
|
||||
status: 500,
|
||||
body: format!("Deserialization error: {}", e),
|
||||
}
|
||||
})
|
||||
.map(|consent| {
|
||||
info!("✅ Successfully parsed consent: {}", consent.consent_id);
|
||||
consent
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ pub struct Meta {
|
|||
|
||||
// --- Consent Models ---
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub struct ConsentRequestBody {
|
||||
pub client_id: String,
|
||||
pub permissions: Vec<String>,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue