use super::common::*;
#[derive(
Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, typed_builder::TypedBuilder,
)]
#[builder(doc)]
pub struct Customer {
#[builder(setter(into, doc = "Sets [`customer_id`](Customer::customer_id)."))]
pub customer_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
doc = "Sets [`personal_details`](Customer::personal_details)."
)
)]
pub personal_details: Option<PersonalDetails>,
}
#[derive(
Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, typed_builder::TypedBuilder,
)]
#[builder(doc)]
pub struct CustomerValidationError {
#[builder(setter(
into,
doc = "Sets [`error_code`](CustomerValidationError::error_code)."
))]
pub error_code: String,
#[builder(setter(
into,
doc = "Sets [`error_message`](CustomerValidationError::error_message)."
))]
pub error_message: String,
#[builder(setter(into, doc = "Sets [`instance`](CustomerValidationError::instance)."))]
pub instance: String,
}
#[derive(
Debug,
Clone,
Default,
PartialEq,
serde::Serialize,
serde::Deserialize,
typed_builder::TypedBuilder,
)]
#[builder(doc)]
pub struct PaymentInstrumentResponse {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
doc = "Sets [`active`](PaymentInstrumentResponse::active)."
)
)]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(strip_option, doc = "Sets [`card`](PaymentInstrumentResponse::card).")
)]
pub card: Option<PaymentInstrumentResponseCard>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
doc = "Sets [`created_at`](PaymentInstrumentResponse::created_at)."
)
)]
pub created_at: Option<crate::datetime::DateTime>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
doc = "Sets [`mandate`](PaymentInstrumentResponse::mandate)."
)
)]
pub mandate: Option<MandateResponse>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
into,
doc = "Sets [`token`](PaymentInstrumentResponse::token)."
)
)]
pub token: Option<String>,
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
doc = "Sets [`r#type`](PaymentInstrumentResponse::r#type)."
)
)]
pub r#type: Option<PaymentInstrumentResponseType>,
}
#[derive(
Debug,
Clone,
Default,
PartialEq,
serde::Serialize,
serde::Deserialize,
typed_builder::TypedBuilder,
)]
#[builder(doc)]
pub struct PaymentInstrumentResponseCard {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
into,
doc = "Sets [`last_4_digits`](PaymentInstrumentResponseCard::last_4_digits)."
)
)]
pub last_4_digits: Option<String>,
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
doc = "Sets [`r#type`](PaymentInstrumentResponseCard::r#type)."
)
)]
pub r#type: Option<CardType>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum PaymentInstrumentResponseType {
#[serde(rename = "card")]
Card,
#[serde(untagged)]
Other(String),
}
#[derive(
Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, typed_builder::TypedBuilder,
)]
#[builder(doc)]
pub struct CreateRequest {
#[builder(setter(into, doc = "Sets [`customer_id`](CreateRequest::customer_id)."))]
pub customer_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
doc = "Sets [`personal_details`](CreateRequest::personal_details)."
)
)]
pub personal_details: Option<PersonalDetails>,
}
#[derive(
Debug,
Clone,
Default,
PartialEq,
serde::Serialize,
serde::Deserialize,
typed_builder::TypedBuilder,
)]
#[builder(doc)]
pub struct UpdateRequest {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(
strip_option,
doc = "Sets [`personal_details`](UpdateRequest::personal_details)."
)
)]
pub personal_details: Option<PersonalDetails>,
}
pub type ListPaymentInstrumentsResponse = Vec<PaymentInstrumentResponse>;
use crate::client::Client;
#[derive(Debug, PartialEq)]
pub enum CreateErrorBody {
BadRequest(crate::error::UnknownApiBody),
Unauthorized(Problem),
Forbidden(ErrorForbidden),
Conflict(Error),
}
impl crate::error::ProblemDetails for CreateErrorBody {
fn problem(&self) -> Option<&crate::resources::common::Problem> {
match self {
Self::BadRequest(body) => crate::error::ProblemDetails::problem(body),
Self::Unauthorized(problem) => Some(problem),
_ => None,
}
}
}
#[derive(Debug, PartialEq)]
pub enum GetErrorBody {
Unauthorized(Problem),
Forbidden(ErrorForbidden),
NotFound(Error),
}
impl crate::error::ProblemDetails for GetErrorBody {
fn problem(&self) -> Option<&crate::resources::common::Problem> {
match self {
Self::Unauthorized(problem) => Some(problem),
_ => None,
}
}
}
#[derive(Debug, PartialEq)]
pub enum UpdateErrorBody {
Unauthorized(Problem),
Forbidden(ErrorForbidden),
NotFound(Error),
}
impl crate::error::ProblemDetails for UpdateErrorBody {
fn problem(&self) -> Option<&crate::resources::common::Problem> {
match self {
Self::Unauthorized(problem) => Some(problem),
_ => None,
}
}
}
#[derive(Debug, PartialEq)]
pub enum ListPaymentInstrumentsErrorBody {
Unauthorized(Problem),
Forbidden(ErrorForbidden),
NotFound(Error),
}
impl crate::error::ProblemDetails for ListPaymentInstrumentsErrorBody {
fn problem(&self) -> Option<&crate::resources::common::Problem> {
match self {
Self::Unauthorized(problem) => Some(problem),
_ => None,
}
}
}
#[derive(Debug, PartialEq)]
pub enum DeactivatePaymentInstrumentErrorBody {
BadRequest(Error),
Unauthorized(Problem),
Forbidden(ErrorForbidden),
NotFound(Error),
}
impl crate::error::ProblemDetails for DeactivatePaymentInstrumentErrorBody {
fn problem(&self) -> Option<&crate::resources::common::Problem> {
match self {
Self::Unauthorized(problem) => Some(problem),
_ => None,
}
}
}
#[derive(Debug)]
pub struct CustomersClient<'a> {
client: &'a Client,
}
impl<'a> CustomersClient<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
pub fn client(&self) -> &Client {
self.client
}
pub async fn create(
&self,
body: CreateRequest,
) -> crate::error::SdkResult<Customer, CreateErrorBody> {
let path = "/v0.1/customers";
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.post(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout())
.json(&body);
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::CREATED => {
let data: Customer = response.json().await?;
Ok(data)
}
reqwest::StatusCode::BAD_REQUEST => {
Err(crate::error::SdkError::from_response(response, |raw| {
Ok(CreateErrorBody::BadRequest(
crate::error::UnknownApiBody::from_bytes(raw),
))
})
.await)
}
reqwest::StatusCode::UNAUTHORIZED => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Problem>(raw).map(CreateErrorBody::Unauthorized)
})
.await)
}
reqwest::StatusCode::FORBIDDEN => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<ErrorForbidden>(raw).map(CreateErrorBody::Forbidden)
})
.await)
}
reqwest::StatusCode::CONFLICT => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Error>(raw).map(CreateErrorBody::Conflict)
})
.await)
}
_ => Err(crate::error::SdkError::unexpected_response(response).await),
}
}
pub async fn get(
&self,
customer_id: impl AsRef<str>,
) -> crate::error::SdkResult<Customer, GetErrorBody> {
let path = format!("/v0.1/customers/{}", customer_id.as_ref());
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.get(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: Customer = response.json().await?;
Ok(data)
}
reqwest::StatusCode::UNAUTHORIZED => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Problem>(raw).map(GetErrorBody::Unauthorized)
})
.await)
}
reqwest::StatusCode::FORBIDDEN => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<ErrorForbidden>(raw).map(GetErrorBody::Forbidden)
})
.await)
}
reqwest::StatusCode::NOT_FOUND => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Error>(raw).map(GetErrorBody::NotFound)
})
.await)
}
_ => Err(crate::error::SdkError::unexpected_response(response).await),
}
}
pub async fn update(
&self,
customer_id: impl AsRef<str>,
body: UpdateRequest,
) -> crate::error::SdkResult<Customer, UpdateErrorBody> {
let path = format!("/v0.1/customers/{}", customer_id.as_ref());
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.put(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout())
.json(&body);
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: Customer = response.json().await?;
Ok(data)
}
reqwest::StatusCode::UNAUTHORIZED => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Problem>(raw).map(UpdateErrorBody::Unauthorized)
})
.await)
}
reqwest::StatusCode::FORBIDDEN => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<ErrorForbidden>(raw).map(UpdateErrorBody::Forbidden)
})
.await)
}
reqwest::StatusCode::NOT_FOUND => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Error>(raw).map(UpdateErrorBody::NotFound)
})
.await)
}
_ => Err(crate::error::SdkError::unexpected_response(response).await),
}
}
pub async fn list_payment_instruments(
&self,
customer_id: impl AsRef<str>,
) -> crate::error::SdkResult<ListPaymentInstrumentsResponse, ListPaymentInstrumentsErrorBody>
{
let path = format!(
"/v0.1/customers/{}/payment-instruments",
customer_id.as_ref()
);
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.get(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: ListPaymentInstrumentsResponse = response.json().await?;
Ok(data)
}
reqwest::StatusCode::UNAUTHORIZED => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Problem>(raw)
.map(ListPaymentInstrumentsErrorBody::Unauthorized)
})
.await)
}
reqwest::StatusCode::FORBIDDEN => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<ErrorForbidden>(raw)
.map(ListPaymentInstrumentsErrorBody::Forbidden)
})
.await)
}
reqwest::StatusCode::NOT_FOUND => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Error>(raw)
.map(ListPaymentInstrumentsErrorBody::NotFound)
})
.await)
}
_ => Err(crate::error::SdkError::unexpected_response(response).await),
}
}
pub async fn deactivate_payment_instrument(
&self,
customer_id: impl AsRef<str>,
token: impl AsRef<str>,
) -> crate::error::SdkResult<(), DeactivatePaymentInstrumentErrorBody> {
let path = format!(
"/v0.1/customers/{}/payment-instruments/{}",
customer_id.as_ref(),
token.as_ref()
);
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.delete(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::NO_CONTENT => Ok(()),
reqwest::StatusCode::BAD_REQUEST => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Error>(raw)
.map(DeactivatePaymentInstrumentErrorBody::BadRequest)
})
.await)
}
reqwest::StatusCode::UNAUTHORIZED => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Problem>(raw)
.map(DeactivatePaymentInstrumentErrorBody::Unauthorized)
})
.await)
}
reqwest::StatusCode::FORBIDDEN => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<ErrorForbidden>(raw)
.map(DeactivatePaymentInstrumentErrorBody::Forbidden)
})
.await)
}
reqwest::StatusCode::NOT_FOUND => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Error>(raw)
.map(DeactivatePaymentInstrumentErrorBody::NotFound)
})
.await)
}
_ => Err(crate::error::SdkError::unexpected_response(response).await),
}
}
}