Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(router): add accept language header middleware (#5500)
chikke srujanec5f9de0cbfe
diff --git a/config/deployments/integration_test.toml b/config/deployments/integration_test.tomlindex 69d268e216..be2d22d92a 100644--- a/config/deployments/integration_test.toml+++ b/config/deployments/integration_test.toml@@ -357,7 +357,7 @@ outgoing_enabled = trueconnectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call[unmasked_headers]-keys = "user-agent"+keys = "accept-language,user-agent"[saved_payment_methods]sdk_eligible_payment_methods = "card"diff --git a/config/deployments/production.toml b/config/deployments/production.tomlindex 17f4b2fa99..f265688487 100644--- a/config/deployments/production.toml+++ b/config/deployments/production.toml@@ -370,7 +370,7 @@ outgoing_enabled = trueconnectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call[unmasked_headers]-keys = "user-agent"+keys = "accept-language,user-agent"[saved_payment_methods]sdk_eligible_payment_methods = "card"diff --git a/config/deployments/sandbox.toml b/config/deployments/sandbox.tomlindex 730f782919..146a9bb87f 100644--- a/config/deployments/sandbox.toml+++ b/config/deployments/sandbox.toml@@ -374,7 +374,7 @@ outgoing_enabled = trueconnectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call[unmasked_headers]-keys = "user-agent"+keys = "accept-language,user-agent"[saved_payment_methods]sdk_eligible_payment_methods = "card"diff --git a/config/development.toml b/config/development.tomlindex b380135e19..8888048ccf 100644--- a/config/development.toml+++ b/config/development.toml@@ -682,7 +682,7 @@ enabled = truefile_storage_backend = "file_system"[unmasked_headers]-keys = "user-agent"+keys = "accept-language,user-agent"[opensearch]host = "https://localhost:9200"diff --git a/config/docker_compose.toml b/config/docker_compose.tomlindex 4d8f287a07..fa5ccaac0a 100644--- a/config/docker_compose.toml+++ b/config/docker_compose.toml@@ -506,7 +506,7 @@ source = "logs"file_storage_backend = "file_system"[unmasked_headers]-keys = "user-agent"+keys = "accept-language,user-agent"[opensearch]host = "https://opensearch:9200"diff --git a/crates/router/Cargo.toml b/crates/router/Cargo.tomlindex 3e6f470d80..958b2d286d 100644--- a/crates/router/Cargo.toml+++ b/crates/router/Cargo.toml@@ -18,7 +18,7 @@ keymanager_create = []keymanager_mtls = ["reqwest/rustls-tls", "common_utils/keymanager_mtls"]encryption_service = ["hyperswitch_domain_models/encryption_service", "common_utils/encryption_service"]frm = ["api_models/frm", "hyperswitch_domain_models/frm", "hyperswitch_connectors/frm", "hyperswitch_interfaces/frm"]-stripe = ["dep:serde_qs"]+stripe = []release = ["stripe", "email", "accounts_cache", "kv_store", "vergen", "recon", "external_services/aws_kms", "external_services/aws_s3", "keymanager_mtls", "keymanager_create", "encryption_service"]oltp = ["storage_impl/oltp"]kv_store = ["scheduler/kv_store"]@@ -102,7 +102,7 @@ rustls-pemfile = "2"serde = { version = "1.0.197", features = ["derive"] }serde_json = "1.0.115"serde_path_to_error = "0.1.16"-serde_qs = { version = "0.12.0", optional = true }+serde_qs = "0.12.0"serde_repr = "0.1.19"serde_urlencoded = "0.7.1"serde_with = "3.7.0"diff --git a/crates/router/src/lib.rs b/crates/router/src/lib.rsindex 6581436c25..9691a53662 100644--- a/crates/router/src/lib.rs+++ b/crates/router/src/lib.rs@@ -341,6 +341,7 @@ pub fn get_application_builder(.wrap(cors::cors(cors))// this middleware works only for Http1.1 requests.wrap(middleware::Http400RequestDetailsLogger)+ .wrap(middleware::AddAcceptLanguageHeader).wrap(middleware::LogSpanInitializer).wrap(router_env::tracing_actix_web::TracingLogger::default())}diff --git a/crates/router/src/middleware.rs b/crates/router/src/middleware.rsindex 7b9919d3af..1276db58dc 100644--- a/crates/router/src/middleware.rs+++ b/crates/router/src/middleware.rs@@ -315,3 +315,84 @@ where})}}++/// Middleware for Adding Accept-Language header based on query params+pub struct AddAcceptLanguageHeader;++impl<S: 'static, B> actix_web::dev::Transform<S, actix_web::dev::ServiceRequest>+ for AddAcceptLanguageHeader+where+ S: actix_web::dev::Service<+ actix_web::dev::ServiceRequest,+ Response = actix_web::dev::ServiceResponse<B>,+ Error = actix_web::Error,+ >,+ S::Future: 'static,+ B: 'static,+{+ type Response = actix_web::dev::ServiceResponse<B>;+ type Error = actix_web::Error;+ type Transform = AddAcceptLanguageHeaderMiddleware<S>;+ type InitError = ();+ type Future = std::future::Ready<Result<Self::Transform, Self::InitError>>;++ fn new_transform(&self, service: S) -> Self::Future {+ std::future::ready(Ok(AddAcceptLanguageHeaderMiddleware {+ service: std::rc::Rc::new(service),+ }))+ }+}++pub struct AddAcceptLanguageHeaderMiddleware<S> {+ service: std::rc::Rc<S>,+}++impl<S, B> actix_web::dev::Service<actix_web::dev::ServiceRequest>+ for AddAcceptLanguageHeaderMiddleware<S>+where+ S: actix_web::dev::Service<+ actix_web::dev::ServiceRequest,+ Response = actix_web::dev::ServiceResponse<B>,+ Error = actix_web::Error,+ > + 'static,+ S::Future: 'static,+ B: 'static,+{+ type Response = actix_web::dev::ServiceResponse<B>;+ type Error = actix_web::Error;+ type Future = futures::future::LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;++ actix_web::dev::forward_ready!(service);+ fn call(&self, mut req: actix_web::dev::ServiceRequest) -> Self::Future {+ let svc = self.service.clone();+ Box::pin(async move {+ #[derive(serde::Deserialize)]+ struct LocaleQueryParam {+ locale: Option<String>,+ }+ let query_params = req.query_string();+ let locale_param =+ serde_qs::from_str::<LocaleQueryParam>(query_params).map_err(|error| {+ actix_web::error::ErrorBadRequest(format!(+ "Could not convert query params to locale query parmas: {:?}",+ error+ ))+ })?;+ let accept_language_header = req.headers().get(http::header::ACCEPT_LANGUAGE);+ if let Some(locale) = locale_param.locale {+ req.headers_mut().insert(+ http::header::ACCEPT_LANGUAGE,+ http::HeaderValue::from_str(&locale)?,+ );+ } else if accept_language_header.is_none() {+ req.headers_mut().insert(+ http::header::ACCEPT_LANGUAGE,+ http::HeaderValue::from_static("en"),+ );+ }+ let response_fut = svc.call(req);+ let response = response_fut.await?;+ Ok(response)+ })+ }+}diff --git a/loadtest/config/development.toml b/loadtest/config/development.tomlindex d64d246dff..6bae052394 100644--- a/loadtest/config/development.toml+++ b/loadtest/config/development.toml@@ -344,7 +344,7 @@ client_secret = ""partner_id = ""[unmasked_headers]-keys = "user-agent"+keys = "accept-language,user-agent"[multitenancy]enabled = false