Skip to content

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

2.2 KB
Raw Blame
//! Generic microservice client framework (typestate pipeline + macro helpers).
//!
//! This module is microservice-agnostic. Domain-specific flows and lightweight clients should
//! live in their own crates and implement [`MicroserviceClient`].
//!
//! # Examples
//!
//! ```rust,ignore
//! use common_utils::request::Headers;
//! use hyperswitch_interfaces::{
//!     api_client::ApiClientWrapper,
//!     micro_service::{MicroserviceClient, MicroserviceClientError},
//! };
//! use router_env::RequestIdentifier;
//! use url::Url;
//!
//! struct ExampleClient<'a> {
//!     base_url: &'a Url,
//!     parent_headers: &'a Headers,
//!     trace: &'a RequestIdentifier,
//! }
//!
//! impl<'a> MicroserviceClient for ExampleClient<'a> {
//!     fn base_url(&self) -> &Url { self.base_url }
//!     fn parent_headers(&self) -> &Headers { self.parent_headers }
//!     fn trace(&self) -> &RequestIdentifier { self.trace }
//! }
//!
//! async fn call_flow(
//!     state: &dyn ApiClientWrapper,
//!     client: &ExampleClient<'_>,
//! ) -> Result<(), MicroserviceClientError> {
//!     // ExampleFlow::call(state, client, flow).await?;
//!     Ok(())
//! }
//! ```

mod error;
mod executor;
mod macros;
mod state;

use common_utils::request::Headers;
pub use error::{MicroserviceClientError, MicroserviceClientErrorKind};
pub use executor::execute_microservice_operation;
use router_env::RequestIdentifier;
pub use state::{ClientOperation, Executed, TransformedRequest, TransformedResponse, Validated};
use url::Url;

/// Minimal context required to execute a microservice flow.
///
/// Implement this for lightweight client wrappers that carry base URL, headers, and trace config.
pub trait MicroserviceClient {
    /// Base URL for the microservice.
    fn base_url(&self) -> &Url;
    /// Parent headers to forward to the microservice.
    fn parent_headers(&self) -> &Headers;
    /// Trace identifier configuration.
    fn trace(&self) -> &RequestIdentifier;
    /// Whether the caller's tenant id (`x-tenant-id`) should be forwarded to the
    /// service. Internal microservices rely on it; external third-party services
    /// that do their own tenant resolution (e.g. from the API key) return `false`.
    fn should_forward_tenant_header(&self) -> bool {
        true
    }
}