From f213c51b3e5c4f0b3546b35bac4dde9698818e01 Mon Sep 17 00:00:00 2001 From: Chethan Rao <70657455+Chethan-rao@users.noreply.github.com> Date: Fri, 22 Mar 2024 18:22:18 +0530 Subject: [PATCH] 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> --- add_connector.md | 33 ++++++------- crates/common_utils/src/lib.rs | 3 -- crates/common_utils/src/request.rs | 46 +++++-------------- crates/diesel_models/src/process_tracker.rs | 21 --------- crates/router/src/connector/bankofamerica.rs | 6 +-- .../src/connector/checkout/transformers.rs | 2 +- crates/router/src/connector/cryptopay.rs | 9 ++-- crates/router/src/connector/cybersource.rs | 6 +-- crates/router/src/connector/dlocal.rs | 4 +- crates/router/src/connector/fiserv.rs | 2 +- crates/router/src/connector/payeezy.rs | 6 ++- crates/router/src/connector/rapyd.rs | 6 +-- crates/router/src/connector/riskified.rs | 2 +- crates/router/src/core/mandate.rs | 2 +- crates/router/src/core/payment_methods.rs | 1 - crates/router/src/types.rs | 2 +- 16 files changed, 46 insertions(+), 105 deletions(-) diff --git a/add_connector.md b/add_connector.md index ac9d3f8d847c..96724cb1b01a 100644 --- a/add_connector.md +++ b/add_connector.md @@ -514,28 +514,23 @@ 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, errors::ConnectorError> { - let connector_router_data = checkout::CheckoutRouterData::try_from(( - &self.get_currency_unit(), - req.request.currency, - req.request.amount, - 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)) - } + &self, + req: &types::PaymentsAuthorizeRouterData, + _connectors: &settings::Connectors, + ) -> CustomResult { + let connector_router_data = checkout::CheckoutRouterData::try_from(( + &self.get_currency_unit(), + req.request.currency, + req.request.amount, + req, + ))?; + let connector_req = checkout::PaymentsRequest::try_from(&connector_router_data)?; + Ok(RequestContent::Json(Box::new(connector_req))) + } ``` - `build_request` method assembles the API request by providing the method, URL, headers, and request body as parameters. diff --git a/crates/common_utils/src/lib.rs b/crates/common_utils/src/lib.rs index 3dbab96408c9..b1a708f7a083 100644 --- a/crates/common_utils/src/lib.rs +++ b/crates/common_utils/src/lib.rs @@ -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)] diff --git a/crates/common_utils/src/request.rs b/crates/common_utils/src/request.rs index dfd890df795b..47f280bc577c 100644 --- a/crates/common_utils/src/request.rs +++ b/crates/common_utils/src/request.rs @@ -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)>; #[derive( @@ -64,6 +60,18 @@ pub enum RequestContent { RawBytes(Vec), } +impl RequestContent { + pub fn get_inner_value(&self) -> Secret { + 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); - -impl RequestBody { - pub fn log_and_get_request_body( - body: T, - encoder: F, - ) -> errors::CustomResult - where - F: FnOnce(T) -> errors::CustomResult, - 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 { - 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(), - } - } -} diff --git a/crates/diesel_models/src/process_tracker.rs b/crates/diesel_models/src/process_tracker.rs index 74fa75a27512..0c6aaa5922f8 100644 --- a/crates/diesel_models/src/process_tracker.rs +++ b/crates/diesel_models/src/process_tracker.rs @@ -190,27 +190,6 @@ impl From 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, - 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, diff --git a/crates/router/src/connector/bankofamerica.rs b/crates/router/src/connector/bankofamerica.rs index fefc80f806c3..aac441201eb0 100644 --- a/crates/router/src/connector/bankofamerica.rs +++ b/crates/router/src/connector/bankofamerica.rs @@ -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(), diff --git a/crates/router/src/connector/checkout/transformers.rs b/crates/router/src/connector/checkout/transformers.rs index 01ce57d10e1d..8004b3b5a12a 100644 --- a/crates/router/src/connector/checkout/transformers.rs +++ b/crates/router/src/connector/checkout/transformers.rs @@ -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() diff --git a/crates/router/src/connector/cryptopay.rs b/crates/router/src/connector/cryptopay.rs index af33ae21d768..a3335b2e228a 100644 --- a/crates/router/src/connector/cryptopay.rs +++ b/crates/router/src/connector/cryptopay.rs @@ -77,10 +77,11 @@ 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)?) - .peek() - .to_owned(); + let body = self + .get_request_body(req, connectors)? + .get_inner_value() + .peek() + .to_owned(); let md5_payload = crypto::Md5 .generate_digest(body.as_bytes()) .change_context(errors::ConnectorError::RequestEncodingFailed)?; diff --git a/crates/router/src/connector/cybersource.rs b/crates/router/src/connector/cybersource.rs index 2b9b45cdda68..e89810e79cf4 100644 --- a/crates/router/src/connector/cybersource.rs +++ b/crates/router/src/connector/cybersource.rs @@ -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, diff --git a/crates/router/src/connector/dlocal.rs b/crates/router/src/connector/dlocal.rs index 9fdb36ea1dc7..b158f239fddd 100644 --- a/crates/router/src/connector/dlocal.rs +++ b/crates/router/src/connector/dlocal.rs @@ -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, diff --git a/crates/router/src/connector/fiserv.rs b/crates/router/src/connector/fiserv.rs index 7db63f5eee50..fdd904ee737e 100644 --- a/crates/router/src/connector/fiserv.rs +++ b/crates/router/src/connector/fiserv.rs @@ -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)?; diff --git a/crates/router/src/connector/payeezy.rs b/crates/router/src/connector/payeezy.rs index 04f180efd080..d24a42085a4a 100644 --- a/crates/router/src/connector/payeezy.rs +++ b/crates/router/src/connector/payeezy.rs @@ -44,8 +44,10 @@ where connectors: &settings::Connectors, ) -> CustomResult)>, 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() diff --git a/crates/router/src/connector/rapyd.rs b/crates/router/src/connector/rapyd.rs index b487740d07de..fd6de253047a 100644 --- a/crates/router/src/connector/rapyd.rs +++ b/crates/router/src/connector/rapyd.rs @@ -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, ×tamp, &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, ×tamp, &salt)?; let headers = vec![ @@ -691,7 +691,7 @@ impl services::ConnectorIntegration