1use super::common::*;
4#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
6pub struct Role {
7 pub id: String,
9 pub name: String,
11 #[serde(skip_serializing_if = "Option::is_none")]
13 pub description: Option<String>,
14 pub permissions: Vec<String>,
19 pub is_predefined: bool,
21 #[serde(skip_serializing_if = "Option::is_none")]
22 pub metadata: Option<Metadata>,
23 pub created_at: crate::datetime::DateTime,
25 pub updated_at: crate::datetime::DateTime,
27}
28#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
30pub struct ListResponse {
31 pub items: Vec<Role>,
32}
33#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
34pub struct CreateBody {
35 pub name: String,
37 pub permissions: Vec<String>,
42 #[serde(skip_serializing_if = "Option::is_none")]
43 pub metadata: Option<Metadata>,
44 #[serde(skip_serializing_if = "Option::is_none")]
46 pub description: Option<String>,
47}
48#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
49pub struct UpdateBody {
50 #[serde(skip_serializing_if = "Option::is_none")]
52 pub name: Option<String>,
53 #[serde(skip_serializing_if = "Option::is_none")]
58 pub permissions: Option<Vec<String>>,
59 #[serde(skip_serializing_if = "Option::is_none")]
61 pub description: Option<String>,
62}
63use crate::client::Client;
64#[derive(Debug)]
65pub enum ListErrorBody {
66 NotFound(Problem),
67}
68#[derive(Debug)]
69pub enum CreateErrorBody {
70 BadRequest(Problem),
71 NotFound(Problem),
72}
73#[derive(Debug)]
74pub enum DeleteErrorBody {
75 BadRequest(Problem),
76 NotFound(Problem),
77}
78#[derive(Debug)]
79pub enum GetErrorBody {
80 NotFound(Problem),
81}
82#[derive(Debug)]
83pub enum UpdateErrorBody {
84 BadRequest(Problem),
85 NotFound(Problem),
86}
87#[derive(Debug)]
89pub struct RolesClient<'a> {
90 client: &'a Client,
91}
92impl<'a> RolesClient<'a> {
93 pub(crate) fn new(client: &'a Client) -> Self {
94 Self { client }
95 }
96 pub fn client(&self) -> &Client {
98 self.client
99 }
100 pub async fn list(
104 &self,
105 merchant_code: impl Into<String>,
106 ) -> crate::error::SdkResult<ListResponse, ListErrorBody> {
107 let path = format!("/v0.1/merchants/{}/roles", merchant_code.into());
108 let url = format!("{}{}", self.client.base_url(), path);
109 let mut request = self
110 .client
111 .http_client()
112 .get(&url)
113 .header("User-Agent", crate::version::user_agent())
114 .timeout(self.client.timeout());
115 if let Some(authorization) = self.client.authorization() {
116 request = request.header("Authorization", format!("Bearer {}", authorization));
117 }
118 for (header_name, header_value) in self.client.runtime_headers() {
119 request = request.header(*header_name, header_value);
120 }
121 let response = request.send().await?;
122 let status = response.status();
123 match status {
124 reqwest::StatusCode::OK => {
125 let data: ListResponse = response.json().await?;
126 Ok(data)
127 }
128 reqwest::StatusCode::NOT_FOUND => {
129 let body: Problem = response.json().await?;
130 Err(crate::error::SdkError::api(ListErrorBody::NotFound(body)))
131 }
132 _ => {
133 let body_bytes = response.bytes().await?;
134 let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
135 Err(crate::error::SdkError::unexpected(status, body))
136 }
137 }
138 }
139 pub async fn create(
143 &self,
144 merchant_code: impl Into<String>,
145 body: CreateBody,
146 ) -> crate::error::SdkResult<Role, CreateErrorBody> {
147 let path = format!("/v0.1/merchants/{}/roles", merchant_code.into());
148 let url = format!("{}{}", self.client.base_url(), path);
149 let mut request = self
150 .client
151 .http_client()
152 .post(&url)
153 .header("User-Agent", crate::version::user_agent())
154 .timeout(self.client.timeout())
155 .json(&body);
156 if let Some(authorization) = self.client.authorization() {
157 request = request.header("Authorization", format!("Bearer {}", authorization));
158 }
159 for (header_name, header_value) in self.client.runtime_headers() {
160 request = request.header(*header_name, header_value);
161 }
162 let response = request.send().await?;
163 let status = response.status();
164 match status {
165 reqwest::StatusCode::CREATED => {
166 let data: Role = response.json().await?;
167 Ok(data)
168 }
169 reqwest::StatusCode::BAD_REQUEST => {
170 let body: Problem = response.json().await?;
171 Err(crate::error::SdkError::api(CreateErrorBody::BadRequest(
172 body,
173 )))
174 }
175 reqwest::StatusCode::NOT_FOUND => {
176 let body: Problem = response.json().await?;
177 Err(crate::error::SdkError::api(CreateErrorBody::NotFound(body)))
178 }
179 _ => {
180 let body_bytes = response.bytes().await?;
181 let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
182 Err(crate::error::SdkError::unexpected(status, body))
183 }
184 }
185 }
186 pub async fn delete(
190 &self,
191 merchant_code: impl Into<String>,
192 role_id: impl Into<String>,
193 ) -> crate::error::SdkResult<(), DeleteErrorBody> {
194 let path = format!(
195 "/v0.1/merchants/{}/roles/{}",
196 merchant_code.into(),
197 role_id.into()
198 );
199 let url = format!("{}{}", self.client.base_url(), path);
200 let mut request = self
201 .client
202 .http_client()
203 .delete(&url)
204 .header("User-Agent", crate::version::user_agent())
205 .timeout(self.client.timeout());
206 if let Some(authorization) = self.client.authorization() {
207 request = request.header("Authorization", format!("Bearer {}", authorization));
208 }
209 for (header_name, header_value) in self.client.runtime_headers() {
210 request = request.header(*header_name, header_value);
211 }
212 let response = request.send().await?;
213 let status = response.status();
214 match status {
215 reqwest::StatusCode::OK => Ok(()),
216 reqwest::StatusCode::BAD_REQUEST => {
217 let body: Problem = response.json().await?;
218 Err(crate::error::SdkError::api(DeleteErrorBody::BadRequest(
219 body,
220 )))
221 }
222 reqwest::StatusCode::NOT_FOUND => {
223 let body: Problem = response.json().await?;
224 Err(crate::error::SdkError::api(DeleteErrorBody::NotFound(body)))
225 }
226 _ => {
227 let body_bytes = response.bytes().await?;
228 let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
229 Err(crate::error::SdkError::unexpected(status, body))
230 }
231 }
232 }
233 pub async fn get(
237 &self,
238 merchant_code: impl Into<String>,
239 role_id: impl Into<String>,
240 ) -> crate::error::SdkResult<Role, GetErrorBody> {
241 let path = format!(
242 "/v0.1/merchants/{}/roles/{}",
243 merchant_code.into(),
244 role_id.into()
245 );
246 let url = format!("{}{}", self.client.base_url(), path);
247 let mut request = self
248 .client
249 .http_client()
250 .get(&url)
251 .header("User-Agent", crate::version::user_agent())
252 .timeout(self.client.timeout());
253 if let Some(authorization) = self.client.authorization() {
254 request = request.header("Authorization", format!("Bearer {}", authorization));
255 }
256 for (header_name, header_value) in self.client.runtime_headers() {
257 request = request.header(*header_name, header_value);
258 }
259 let response = request.send().await?;
260 let status = response.status();
261 match status {
262 reqwest::StatusCode::OK => {
263 let data: Role = response.json().await?;
264 Ok(data)
265 }
266 reqwest::StatusCode::NOT_FOUND => {
267 let body: Problem = response.json().await?;
268 Err(crate::error::SdkError::api(GetErrorBody::NotFound(body)))
269 }
270 _ => {
271 let body_bytes = response.bytes().await?;
272 let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
273 Err(crate::error::SdkError::unexpected(status, body))
274 }
275 }
276 }
277 pub async fn update(
281 &self,
282 merchant_code: impl Into<String>,
283 role_id: impl Into<String>,
284 body: UpdateBody,
285 ) -> crate::error::SdkResult<Role, UpdateErrorBody> {
286 let path = format!(
287 "/v0.1/merchants/{}/roles/{}",
288 merchant_code.into(),
289 role_id.into()
290 );
291 let url = format!("{}{}", self.client.base_url(), path);
292 let mut request = self
293 .client
294 .http_client()
295 .patch(&url)
296 .header("User-Agent", crate::version::user_agent())
297 .timeout(self.client.timeout())
298 .json(&body);
299 if let Some(authorization) = self.client.authorization() {
300 request = request.header("Authorization", format!("Bearer {}", authorization));
301 }
302 for (header_name, header_value) in self.client.runtime_headers() {
303 request = request.header(*header_name, header_value);
304 }
305 let response = request.send().await?;
306 let status = response.status();
307 match status {
308 reqwest::StatusCode::OK => {
309 let data: Role = response.json().await?;
310 Ok(data)
311 }
312 reqwest::StatusCode::BAD_REQUEST => {
313 let body: Problem = response.json().await?;
314 Err(crate::error::SdkError::api(UpdateErrorBody::BadRequest(
315 body,
316 )))
317 }
318 reqwest::StatusCode::NOT_FOUND => {
319 let body: Problem = response.json().await?;
320 Err(crate::error::SdkError::api(UpdateErrorBody::NotFound(body)))
321 }
322 _ => {
323 let body_bytes = response.bytes().await?;
324 let body = crate::error::UnknownApiBody::from_bytes(body_bytes.as_ref());
325 Err(crate::error::SdkError::unexpected(status, body))
326 }
327 }
328 }
329}