chore: address Rust 1.77 clippy lints (#4172)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: chikke srujan <121822803+srujanchikke@users.noreply.github.com>
This commit is contained in:
Chethan Rao
2024-03-22 18:22:18 +05:30
committed by GitHub
parent 4c8cdf1475
commit f213c51b3e
16 changed files with 46 additions and 105 deletions

View File

@ -514,14 +514,14 @@ Within the `ConnectorIntegration` trait, you'll find the following methods imple
}
```
- `get_request_body` method calls transformers where hyperswitch payment request data is transformed into connector payment request. For constructing the request body have a function `log_and_get_request_body` that allows generic argument which is the struct that is passed as the body for connector integration, and a function that can be use to encode it into String. We log the request in this function, as the struct will be intact and the masked values will be masked.
- `get_request_body` method calls transformers where hyperswitch payment request data is transformed into connector payment request. If the conversion and construction processes are successful, the function wraps the constructed connector_req in a Box and returns it as `RequestContent::Json`. The `RequestContent` enum defines different types of request content that can be sent. It includes variants for JSON, form-urlencoded, XML, raw bytes, and potentially other formats.
```rust
fn get_request_body(
&self,
req: &types::PaymentsAuthorizeRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_router_data = checkout::CheckoutRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
@ -529,12 +529,7 @@ Within the `ConnectorIntegration` trait, you'll find the following methods imple
req,
))?;
let connector_req = checkout::PaymentsRequest::try_from(&connector_router_data)?;
let checkout_req = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::encode_to_string_of_json,
)
.change_context(errors::ConnectorError::RequestEncodingFailed)?;
Ok(Some(checkout_req))
Ok(RequestContent::Json(Box::new(connector_req)))
}
```

View File

@ -35,9 +35,6 @@ pub mod date_time {
},
OffsetDateTime, PrimitiveDateTime,
};
/// Struct to represent milliseconds in time sensitive data fields
#[derive(Debug)]
pub struct Milliseconds(i32);
/// Enum to represent date formats
#[derive(Debug)]

View File

@ -1,10 +1,6 @@
use masking::{Maskable, Secret};
#[cfg(feature = "logs")]
use router_env::logger;
use serde::{Deserialize, Serialize};
use crate::errors;
pub type Headers = std::collections::HashSet<(String, Maskable<String>)>;
#[derive(
@ -64,6 +60,18 @@ pub enum RequestContent {
RawBytes(Vec<u8>),
}
impl RequestContent {
pub fn get_inner_value(&self) -> Secret<String> {
match self {
Self::Json(i) => serde_json::to_string(&i).unwrap_or_default().into(),
Self::FormUrlEncoded(i) => serde_urlencoded::to_string(i).unwrap_or_default().into(),
Self::Xml(i) => quick_xml::se::to_string(&i).unwrap_or_default().into(),
Self::FormData(_) => String::new().into(),
Self::RawBytes(_) => String::new().into(),
}
}
}
impl Request {
pub fn new(method: Method, url: &str) -> Self {
Self {
@ -176,33 +184,3 @@ impl Default for RequestBuilder {
Self::new()
}
}
#[derive(Clone, Debug)]
pub struct RequestBody(Secret<String>);
impl RequestBody {
pub fn log_and_get_request_body<T, F>(
body: T,
encoder: F,
) -> errors::CustomResult<Self, errors::ParsingError>
where
F: FnOnce(T) -> errors::CustomResult<String, errors::ParsingError>,
T: std::fmt::Debug,
{
#[cfg(feature = "logs")]
logger::info!(connector_request_body=?body);
Ok(Self(Secret::new(encoder(body)?)))
}
pub fn get_inner_value(request_body: RequestContent) -> Secret<String> {
match request_body {
RequestContent::Json(i) => serde_json::to_string(&i).unwrap_or_default().into(),
RequestContent::FormUrlEncoded(i) => {
serde_urlencoded::to_string(&i).unwrap_or_default().into()
}
RequestContent::Xml(i) => quick_xml::se::to_string(&i).unwrap_or_default().into(),
RequestContent::FormData(_) => String::new().into(),
RequestContent::RawBytes(_) => String::new().into(),
}
}
}

View File

@ -190,27 +190,6 @@ impl From<ProcessTrackerUpdate> for ProcessTrackerUpdateInternal {
}
}
#[allow(dead_code)]
pub struct SchedulerOptions {
looper_interval: common_utils::date_time::Milliseconds,
db_name: String,
cache_name: String,
schema_name: String,
cache_expiry: i32,
runners: Vec<String>,
fetch_limit: i32,
fetch_limit_product_factor: i32,
query_order: String,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ProcessData {
db_name: String,
cache_name: String,
process_tracker: ProcessTracker,
}
#[derive(
serde::Serialize,
serde::Deserialize,

View File

@ -140,11 +140,7 @@ where
.chars()
.skip(base_url.len() - 1)
.collect();
let sha256 = self.generate_digest(
types::RequestBody::get_inner_value(boa_req)
.expose()
.as_bytes(),
);
let sha256 = self.generate_digest(boa_req.get_inner_value().expose().as_bytes());
let signature = self.generate_signature(
auth,
host.to_string(),

View File

@ -1345,7 +1345,7 @@ pub fn construct_file_upload_request(
request.file_key,
request
.file_type
.to_string()
.as_ref()
.split('/')
.last()
.unwrap_or_default()

View File

@ -77,8 +77,9 @@ where
| common_utils::request::Method::Put
| common_utils::request::Method::Delete
| common_utils::request::Method::Patch => {
let body =
types::RequestBody::get_inner_value(self.get_request_body(req, connectors)?)
let body = self
.get_request_body(req, connectors)?
.get_inner_value()
.peek()
.to_owned();
let md5_payload = crypto::Md5

View File

@ -241,11 +241,7 @@ where
.chars()
.skip(base_url.len() - 1)
.collect();
let sha256 = self.generate_digest(
types::RequestBody::get_inner_value(cybersource_req)
.expose()
.as_bytes(),
);
let sha256 = self.generate_digest(cybersource_req.get_inner_value().expose().as_bytes());
let http_method = self.get_http_method();
let signature = self.generate_signature(
auth,

View File

@ -68,9 +68,7 @@ where
"{}{}{}",
auth.x_login.peek(),
date,
types::RequestBody::get_inner_value(dlocal_req)
.peek()
.to_owned()
dlocal_req.get_inner_value().peek().to_owned()
);
let authz = crypto::HmacSha256::sign_message(
&crypto::HmacSha256,

View File

@ -78,7 +78,7 @@ where
.generate_authorization_signature(
auth,
&client_request_id,
types::RequestBody::get_inner_value(fiserv_req).peek(),
fiserv_req.get_inner_value().peek(),
timestamp,
)
.change_context(errors::ConnectorError::RequestEncodingFailed)?;

View File

@ -44,8 +44,10 @@ where
connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
let auth = payeezy::PayeezyAuthType::try_from(&req.connector_auth_type)?;
let request_payload =
types::RequestBody::get_inner_value(self.get_request_body(req, connectors)?).expose();
let request_payload = self
.get_request_body(req, connectors)?
.get_inner_value()
.expose();
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.ok()

View File

@ -222,7 +222,7 @@ impl
let auth: rapyd::RapydAuthType = rapyd::RapydAuthType::try_from(&req.connector_auth_type)?;
let body = types::PaymentsAuthorizeType::get_request_body(self, req, connectors)?;
let req_body = types::RequestBody::get_inner_value(body).expose();
let req_body = body.get_inner_value().expose();
let signature =
self.generate_signature(&auth, "post", "/v1/payments", &req_body, &timestamp, &salt)?;
let headers = vec![
@ -555,7 +555,7 @@ impl
req.request.connector_transaction_id
);
let body = types::PaymentsCaptureType::get_request_body(self, req, connectors)?;
let req_body = types::RequestBody::get_inner_value(body).expose();
let req_body = body.get_inner_value().expose();
let signature =
self.generate_signature(&auth, "post", &url_path, &req_body, &timestamp, &salt)?;
let headers = vec![
@ -691,7 +691,7 @@ impl services::ConnectorIntegration<api::Execute, types::RefundsData, types::Ref
let salt = Alphanumeric.sample_string(&mut rand::thread_rng(), 12);
let body = types::RefundExecuteType::get_request_body(self, req, connectors)?;
let req_body = types::RequestBody::get_inner_value(body).expose();
let req_body = body.get_inner_value().expose();
let auth: rapyd::RapydAuthType = rapyd::RapydAuthType::try_from(&req.connector_auth_type)?;
let signature =
self.generate_signature(&auth, "post", "/v1/refunds", &req_body, &timestamp, &salt)?;

View File

@ -63,7 +63,7 @@ where
let riskified_req = self.get_request_body(req, connectors)?;
let binding = types::RequestBody::get_inner_value(riskified_req);
let binding = riskified_req.get_inner_value();
let payload = binding.peek();
let digest = self

View File

@ -400,7 +400,7 @@ where
resp.merchant_id.clone(),
resp.payment_id.clone(),
resp.connector.clone(),
resp.request.get_setup_mandate_details().map(Clone::clone),
resp.request.get_setup_mandate_details().cloned(),
maybe_customer,
pm_id.get_required_value("payment_method_id")?,
mandate_ids,

View File

@ -7,7 +7,6 @@ pub use api_models::enums::Connector;
use api_models::payments::CardToken;
#[cfg(feature = "payouts")]
pub use api_models::{enums::PayoutConnectors, payouts as payout_types};
pub use common_utils::request::RequestBody;
use data_models::payments::{payment_attempt::PaymentAttempt, PaymentIntent};
use diesel_models::enums;

View File

@ -22,7 +22,7 @@ pub use api_models::{enums::Connector, mandates};
#[cfg(feature = "payouts")]
pub use api_models::{enums::PayoutConnectors, payouts as payout_types};
use common_enums::MandateStatus;
pub use common_utils::request::{RequestBody, RequestContent};
pub use common_utils::request::RequestContent;
use common_utils::{pii, pii::Email};
use data_models::mandates::{CustomerAcceptance, MandateData};
use error_stack::{IntoReport, ResultExt};