Skip to content

Commit

Permalink
chore: address Rust 1.77 clippy lints (#4172)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people committed Mar 22, 2024
1 parent 4c8cdf1 commit f213c51
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 105 deletions.
33 changes: 14 additions & 19 deletions add_connector.md
Expand Up @@ -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<Option<types::RequestBody>, 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<RequestContent, 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)?;
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.
Expand Down
3 changes: 0 additions & 3 deletions crates/common_utils/src/lib.rs
Expand Up @@ -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)]
Expand Down
46 changes: 12 additions & 34 deletions 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<String>)>;

#[derive(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
}
}
}
21 changes: 0 additions & 21 deletions crates/diesel_models/src/process_tracker.rs
Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions crates/router/src/connector/bankofamerica.rs
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/checkout/transformers.rs
Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions crates/router/src/connector/cryptopay.rs
Expand Up @@ -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)?;
Expand Down
6 changes: 1 addition & 5 deletions crates/router/src/connector/cybersource.rs
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions crates/router/src/connector/dlocal.rs
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/fiserv.rs
Expand Up @@ -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)?;
Expand Down
6 changes: 4 additions & 2 deletions crates/router/src/connector/payeezy.rs
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions crates/router/src/connector/rapyd.rs
Expand Up @@ -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![
Expand Down Expand Up @@ -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![
Expand Down Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/riskified.rs
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/core/mandate.rs
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion crates/router/src/core/payment_methods.rs
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/types.rs
Expand Up @@ -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};
Expand Down

0 comments on commit f213c51

Please sign in to comment.