square_rust/api/models/response/create_customer/
mod.rs

1//! This module contains all version of response models for the create_customer endpoint.
2
3pub mod versions;
4
5use std::any::Any;
6
7use reqwest::Error;
8use reqwest::Response;
9
10use crate::api::models::enums::api_version::SquareApiVersion;
11use crate::api::models::{
12    objects::api_error::SquareApiError, objects::error_response::ErrorResponse,
13    response::create_customer::versions::v20230925::CreateCustomerResponseV20230925,
14};
15
16/// This trait represents the response from the create_customer endpoint
17pub trait CreateCustomerResponse {
18    fn as_any(&self) -> &dyn std::any::Any;
19}
20
21impl CreateCustomerResponse for CreateCustomerResponseV20230925 {
22    fn as_any(&self) -> &dyn Any {
23        self
24    }
25}
26
27/// Convert the response from the create_customer endpoint into the appropriate response type
28pub async fn from_response_to_create_customer_response(
29    response: Response,
30    api_version: SquareApiVersion,
31) -> Result<Box<dyn CreateCustomerResponse>, SquareApiError> {
32    if response.status().is_success() {
33        let parsed = match api_version {
34            SquareApiVersion::V20230925 => {
35                let parsed: Result<CreateCustomerResponseV20230925, Error> =
36                    response.json::<CreateCustomerResponseV20230925>().await;
37                parsed
38            }
39        };
40
41        match parsed {
42            Ok(val) => Ok(Box::new(val) as Box<dyn CreateCustomerResponse>),
43            Err(_) => Err(SquareApiError::new("Deserialization error")),
44        }
45    } else {
46        let err_response_res: Result<ErrorResponse, Error> = response.json().await;
47        match err_response_res {
48            Ok(error_response) => {
49                let api_error = SquareApiError::with_response_errors("Error response", &error_response.errors);
50                Err(api_error)
51            }
52            Err(_) => Err(SquareApiError::new("Deserialization error")),
53        }
54    }
55}