1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! CustomerApi module

use crate::http::client::http_client::SquareHttpClient;
use crate::api::models::api_error::SquareApiError;
use crate::config::SquareApiConfig;
use crate::api::models::api_version::SquareApiVersion;
use crate::api::models::request::create_customer::CreateCustomerRequest;
use crate::api::models::request::create_customer::versions::v20230925::CreateCustomerRequestV20230925;
use crate::api::models::response::create_customer::CreateCustomerResponse;
use super::models::response::create_customer::from_response_to_create_customer_response;

const DEFAULT_API_URL : &str = "/customers";

/// CustomerApi module
pub struct CustomersApi {
    config: SquareApiConfig,
    client: SquareHttpClient,
}

impl CustomersApi {
    /// Create a new CustomersApi
    pub fn new(config: SquareApiConfig, client: SquareHttpClient) -> Self {
        CustomersApi {
            config,
            client,
        }
    }

    /// Use a create_customer endpoint
    pub async fn create_customer<R: CreateCustomerRequest>(
        &self,
        request: R
    ) -> Result<Box<dyn CreateCustomerResponse>, SquareApiError> {
        self.process_request(request).await
    }

    /// Process the request and deserialize the response
    async fn process_request<R: CreateCustomerRequest>(&self, request: R) -> Result<Box<dyn CreateCustomerResponse>, SquareApiError> {

        let body = match self.config.api_version {
            SquareApiVersion::V20230925 => {
                let data = request.as_any().downcast_ref::<CreateCustomerRequestV20230925>();
                match data {
                    Some(data) => data.to_body_string(),
                    None => return Err(SquareApiError::new("Error casting CreateCustomerRequestV20230925"))
                }
            },
        };

        // request to square api
        let response = self.client.post(&self.url(), &body).await?;
        let data = from_response_to_create_customer_response(response, self.config.api_version.clone()).await;
        data
    }

    /// Get the url for the request
    fn url(&self) -> String {
        format!("{}{}{}", self.config.get_dns(), self.config.base_url, DEFAULT_API_URL)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio;
    use crate::api::models::request::create_customer::versions::v20230925::CreateCustomerRequestV20230925;

    #[tokio::test]
    async fn test_create_customer() {

        let idempotency_key = None;
        let given_name = Some("given_name".to_string());
        let family_name = Some("family_name".to_string());
        let company_name = None;
        let nickname = None;
        let email_address = None;
        let address = None;
        let phone_number = None;
        let reference_id = None;
        let note = None;
        let birthday = None;
        let tax_ids = None;

        let config = SquareApiConfig::builder().build();
        let http_client = SquareHttpClient::try_new(&config.http_client_config).unwrap();
        let client = CustomersApi::new(config, http_client);
        let request = CreateCustomerRequestV20230925::new(
            idempotency_key,
            given_name,
            family_name,
            company_name,
            nickname,
            email_address,
            address,
            phone_number,
            reference_id,
            note,
            birthday,
            tax_ids
        );
        let _ = client.create_customer(request).await.unwrap();
    }
}