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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
pub enum GetResourceLimitsError {
    RequestFailure(reqwest::Error),
    InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
    UnexpectedStatus(reqwest::StatusCode),
    Status400 {
        errors: Vec<String>,
    },
    Status403 {
        error: String,
    },
    Status500 {
        error: String,
    },
}

impl From<reqwest::Error> for GetResourceLimitsError {
    fn from(error: reqwest::Error) -> GetResourceLimitsError {
        GetResourceLimitsError::RequestFailure(error)
    }
}

impl From<reqwest::header::InvalidHeaderValue> for GetResourceLimitsError {
    fn from(error: reqwest::header::InvalidHeaderValue) -> GetResourceLimitsError {
        GetResourceLimitsError::InvalidHeaderValue(error)
    }
}

impl GetResourceLimitsError {
    pub fn to_limits_endpoint_error(&self) -> Option<crate::model::LimitsEndpointError> {
        match self {
            GetResourceLimitsError::Status400 { errors } => Some(crate::model::LimitsEndpointError::ArgValidationError { errors: errors.clone() }), 
            GetResourceLimitsError::Status403 { error } => Some(crate::model::LimitsEndpointError::LimitExceeded { error: error.clone() }), 
            GetResourceLimitsError::Status500 { error } => Some(crate::model::LimitsEndpointError::InternalError { error: error.clone() }), 
            _ => None
        }
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct LimitsEndpointErrorArgValidationErrorPayload {
    pub errors: Vec<String>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct LimitsEndpointErrorLimitExceededPayload {
    pub error: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct LimitsEndpointErrorInternalErrorPayload {
    pub error: String,
}

pub enum UpdateResourceLimitsError {
    RequestFailure(reqwest::Error),
    InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
    UnexpectedStatus(reqwest::StatusCode),
    Status400 {
        errors: Vec<String>,
    },
    Status403 {
        error: String,
    },
    Status500 {
        error: String,
    },
}

impl From<reqwest::Error> for UpdateResourceLimitsError {
    fn from(error: reqwest::Error) -> UpdateResourceLimitsError {
        UpdateResourceLimitsError::RequestFailure(error)
    }
}

impl From<reqwest::header::InvalidHeaderValue> for UpdateResourceLimitsError {
    fn from(error: reqwest::header::InvalidHeaderValue) -> UpdateResourceLimitsError {
        UpdateResourceLimitsError::InvalidHeaderValue(error)
    }
}

impl UpdateResourceLimitsError {
    pub fn to_limits_endpoint_error(&self) -> Option<crate::model::LimitsEndpointError> {
        match self {
            UpdateResourceLimitsError::Status400 { errors } => Some(crate::model::LimitsEndpointError::ArgValidationError { errors: errors.clone() }), 
            UpdateResourceLimitsError::Status403 { error } => Some(crate::model::LimitsEndpointError::LimitExceeded { error: error.clone() }), 
            UpdateResourceLimitsError::Status500 { error } => Some(crate::model::LimitsEndpointError::InternalError { error: error.clone() }), 
            _ => None
        }
    }
}

#[async_trait::async_trait]
pub trait Limits {
    async fn get_resource_limits(&self, account_id: &str, authorization: &str) -> Result<crate::model::ResourceLimits, GetResourceLimitsError>;
    async fn update_resource_limits(&self, field0: crate::model::BatchUpdateResourceLimits, authorization: &str) -> Result<(), UpdateResourceLimitsError>;
}

#[derive(Clone, Debug)]
pub struct LimitsLive {
    pub base_url: reqwest::Url,
}

#[async_trait::async_trait]
impl Limits for LimitsLive {
    async fn get_resource_limits(&self, account_id: &str, authorization: &str) -> Result<crate::model::ResourceLimits, GetResourceLimitsError> {
        let mut url = self.base_url.clone();
        url.set_path("v1/resource-limits");
        url.query_pairs_mut().append_pair("account-id", &format!("{account_id}"));
        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let result = reqwest::Client::builder()
            .build()?
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::ResourceLimits>().await?;
                Ok(body)
            }
            400 => {
                let body = result.json::<LimitsEndpointErrorArgValidationErrorPayload>().await?;
                Err(GetResourceLimitsError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LimitsEndpointErrorLimitExceededPayload>().await?;
                Err(GetResourceLimitsError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LimitsEndpointErrorInternalErrorPayload>().await?;
                Err(GetResourceLimitsError::Status500 { error: body.error })
            }
            _ => Err(GetResourceLimitsError::UnexpectedStatus(result.status()))
        }
    }

    async fn update_resource_limits(&self, field0: crate::model::BatchUpdateResourceLimits, authorization: &str) -> Result<(), UpdateResourceLimitsError> {
        let mut url = self.base_url.clone();
        url.set_path("v1/resource-limits");

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let result = reqwest::Client::builder()
            .build()?
            .post(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<()>().await?;
                Ok(body)
            }
            400 => {
                let body = result.json::<LimitsEndpointErrorArgValidationErrorPayload>().await?;
                Err(UpdateResourceLimitsError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LimitsEndpointErrorLimitExceededPayload>().await?;
                Err(UpdateResourceLimitsError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LimitsEndpointErrorInternalErrorPayload>().await?;
                Err(UpdateResourceLimitsError::Status500 { error: body.error })
            }
            _ => Err(UpdateResourceLimitsError::UnexpectedStatus(result.status()))
        }
    }
}