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
104
105
106
use async_trait::async_trait;
use crate::context::Context;
use crate::error::Error;
use crate::model::account_summary::AccountSummary;
use crate::model::error_body::ErrorBody;

pub enum AccountSummaryError {
    Error401(ErrorBody),
    Error500(ErrorBody),
}

#[async_trait]
pub trait AccountSummaryClient {
    async fn get(&self, skip: i32, limit: i32) -> Result<Vec<AccountSummary>, Error<AccountSummaryError>>;
    async fn count_get(&self) -> Result<i64, Error<AccountSummaryError>>;
}

pub struct AccountSummaryClientLive {
    pub context: Context,
}

#[async_trait]
impl AccountSummaryClient for AccountSummaryClientLive {
    async fn get(&self, skip: i32, limit: i32) -> Result<Vec<AccountSummary>, Error<AccountSummaryError>> {
        let mut url = self.context.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v2")
            .push("admin")
            .push("accounts");

        url.query_pairs_mut().append_pair("skip", &skip.to_string());

        url.query_pairs_mut().append_pair("limit", &limit.to_string());

        let mut request = self
            .context
            .client
            .get(url.clone());

        {
            tracing::info!(method="get", endpoint="/v2/admin/accounts", url=url.to_string(), "get");
        }

        if let Some(token) = self.context.bearer_token() {
            request = request.bearer_auth(token);
        }

        let response = request.send().await?;

        let status = response.status().as_u16();
        match status {
            200 => {
                Ok(response.json::<Vec<AccountSummary>>().await?)
            }
            401 => {
                let body = response.json::<ErrorBody>().await?;
                Err(Error::Item(AccountSummaryError::Error401(body)))
            }
            500 => {
                let body = response.json::<ErrorBody>().await?;
                Err(Error::Item(AccountSummaryError::Error500(body)))
            }
            _ => Err(Error::unexpected(status, response.bytes().await?)),
        }
    }

    async fn count_get(&self) -> Result<i64, Error<AccountSummaryError>> {
        let mut url = self.context.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v2")
            .push("admin")
            .push("accounts")
            .push("count");

        let mut request = self
            .context
            .client
            .get(url.clone());

        {
            tracing::info!(method="get", endpoint="/v2/admin/accounts/count", url=url.to_string(), "count_get");
        }

        if let Some(token) = self.context.bearer_token() {
            request = request.bearer_auth(token);
        }

        let response = request.send().await?;

        let status = response.status().as_u16();
        match status {
            200 => {
                Ok(response.json::<i64>().await?)
            }
            401 => {
                let body = response.json::<ErrorBody>().await?;
                Err(Error::Item(AccountSummaryError::Error401(body)))
            }
            500 => {
                let body = response.json::<ErrorBody>().await?;
                Err(Error::Item(AccountSummaryError::Error500(body)))
            }
            _ => Err(Error::unexpected(status, response.bytes().await?)),
        }
    }
}