1use reqwest;
12#[allow(unused_imports)]
13use serde::{de::Error as _};
14use crate::{apis::ResponseContent, models};
15#[allow(unused_imports)]
16use super::{Error, ContentType};
17use dtz_config::Configuration;
18
19fn build_url(config: &Configuration) -> String {
20 if let Some(base_path) = &config.base_path {
21 let base = url::Url::parse(base_path).unwrap();
22 let mut target_url = url::Url::parse(crate::apis::SVC_URL).unwrap();
23 let _ = target_url.set_scheme(base.scheme());
24 let _ = target_url.set_port(base.port());
25 let _ = target_url.set_host(Some(base.host_str().unwrap()));
26 format!("{target_url}")
27 } else {
28 crate::apis::SVC_URL.to_string()
29 }
30}
31
32#[derive(Clone, Debug, Default)]
34pub struct DeleteObjectHeaders {
35 pub x_dtz_realm: Option<String>,
37}
38#[derive(Clone, Debug, Default)]
40pub struct GetObjectHeaders {
41 pub x_dtz_realm: Option<String>,
43}
44#[derive(Clone, Debug, Default)]
46pub struct GetObjectMetadataHeaders {
47 pub x_dtz_realm: Option<String>,
49}
50#[derive(Clone, Debug, Default)]
52pub struct ListObjectsHeaders {
53 pub x_dtz_realm: Option<String>,
55}
56#[derive(Clone, Debug, Default)]
58pub struct PutObjectHeaders {
59 pub x_dtz_expiration: Option<String>,
61 pub x_dtz_expire_in: Option<String>,
63 pub x_dtz_expire_at: Option<String>,
65 pub x_dtz_realm: Option<String>,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
71#[serde(untagged)]
72pub enum DeleteObjectError {
73 Status401(models::ErrorMessage),
74 UnknownValue(serde_json::Value),
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
79#[serde(untagged)]
80pub enum DisableServiceError {
81 Status401(models::ErrorMessage),
82 UnknownValue(serde_json::Value),
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
87#[serde(untagged)]
88pub enum EnableServiceError {
89 Status401(models::ErrorMessage),
90 Status500(models::ErrorMessage),
91 UnknownValue(serde_json::Value),
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(untagged)]
97pub enum GetObjectError {
98 Status404(),
99 Status401(models::ErrorMessage),
100 UnknownValue(serde_json::Value),
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
105#[serde(untagged)]
106pub enum GetObjectMetadataError {
107 Status404(),
108 Status401(models::ErrorMessage),
109 UnknownValue(serde_json::Value),
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
114#[serde(untagged)]
115pub enum ListObjectsError {
116 Status401(models::ErrorMessage),
117 UnknownValue(serde_json::Value),
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
122#[serde(untagged)]
123pub enum PutObjectError {
124 Status400(models::ErrorMessage),
125 Status401(models::ErrorMessage),
126 UnknownValue(serde_json::Value),
127}
128
129#[derive(Debug, Clone, Serialize, Deserialize)]
131#[serde(untagged)]
132pub enum StatsError {
133 Status401(models::ErrorMessage),
134 UnknownValue(serde_json::Value),
135}
136
137
138pub async fn delete_object(configuration: &Configuration, object_path: &str, headers: Option<DeleteObjectHeaders>) -> Result<(), Error<DeleteObjectError>> {
140 let p_path_object_path = object_path;
142
143 let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
144 let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
145
146 if let Some(h) = &headers {
148 if let Some(param_value) = h.x_dtz_realm.as_ref() {
149 req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
150 }
151 }
152
153 if let Some(ref value) = configuration.api_key {
154 req_builder = req_builder.header("X-API-KEY", value);
155 };
156 if let Some(ref token) = configuration.oauth_access_token {
157 req_builder = req_builder.bearer_auth(token.to_owned());
158 };
159
160 let req = req_builder.build()?;
161 let resp = configuration.client.execute(req).await?;
162
163 let status = resp.status();
164
165 if !status.is_client_error() && !status.is_server_error() {
166 Ok(())
167 } else {
168 let content = resp.text().await?;
169 let entity: Option<DeleteObjectError> = serde_json::from_str(&content).ok();
170 Err(Error::ResponseError(ResponseContent { status, content, entity }))
171 }
172}
173
174pub async fn disable_service(configuration: &Configuration) -> Result<(), Error<DisableServiceError>> {
175
176 let uri_str = format!("{}/disable", build_url(configuration));
177 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
178
179
180 if let Some(ref value) = configuration.api_key {
181 req_builder = req_builder.header("X-API-KEY", value);
182 };
183 if let Some(ref token) = configuration.oauth_access_token {
184 req_builder = req_builder.bearer_auth(token.to_owned());
185 };
186
187 let req = req_builder.build()?;
188 let resp = configuration.client.execute(req).await?;
189
190 let status = resp.status();
191
192 if !status.is_client_error() && !status.is_server_error() {
193 Ok(())
194 } else {
195 let content = resp.text().await?;
196 let entity: Option<DisableServiceError> = serde_json::from_str(&content).ok();
197 Err(Error::ResponseError(ResponseContent { status, content, entity }))
198 }
199}
200
201pub async fn enable_service(configuration: &Configuration) -> Result<(), Error<EnableServiceError>> {
202
203 let uri_str = format!("{}/enable", build_url(configuration));
204 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
205
206
207 if let Some(ref value) = configuration.api_key {
208 req_builder = req_builder.header("X-API-KEY", value);
209 };
210 if let Some(ref token) = configuration.oauth_access_token {
211 req_builder = req_builder.bearer_auth(token.to_owned());
212 };
213
214 let req = req_builder.build()?;
215 let resp = configuration.client.execute(req).await?;
216
217 let status = resp.status();
218
219 if !status.is_client_error() && !status.is_server_error() {
220 Ok(())
221 } else {
222 let content = resp.text().await?;
223 let entity: Option<EnableServiceError> = serde_json::from_str(&content).ok();
224 Err(Error::ResponseError(ResponseContent { status, content, entity }))
225 }
226}
227
228pub async fn get_object(configuration: &Configuration, object_path: &str, headers: Option<GetObjectHeaders>) -> Result<reqwest::Response, Error<GetObjectError>> {
229 let p_path_object_path = object_path;
231
232 let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
233 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
234
235 if let Some(h) = &headers {
237 if let Some(param_value) = h.x_dtz_realm.as_ref() {
238 req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
239 }
240 }
241
242 if let Some(ref value) = configuration.api_key {
243 req_builder = req_builder.header("X-API-KEY", value);
244 };
245 if let Some(ref token) = configuration.oauth_access_token {
246 req_builder = req_builder.bearer_auth(token.to_owned());
247 };
248
249 let req = req_builder.build()?;
250 let resp = configuration.client.execute(req).await?;
251
252 let status = resp.status();
253
254 if !status.is_client_error() && !status.is_server_error() {
255 Ok(resp)
256 } else {
257 let content = resp.text().await?;
258 let entity: Option<GetObjectError> = serde_json::from_str(&content).ok();
259 Err(Error::ResponseError(ResponseContent { status, content, entity }))
260 }
261}
262
263pub async fn get_object_metadata(configuration: &Configuration, object_path: &str, headers: Option<GetObjectMetadataHeaders>) -> Result<(), Error<GetObjectMetadataError>> {
264 let p_path_object_path = object_path;
266
267 let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
268 let mut req_builder = configuration.client.request(reqwest::Method::HEAD, &uri_str);
269
270 if let Some(h) = &headers {
272 if let Some(param_value) = h.x_dtz_realm.as_ref() {
273 req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
274 }
275 }
276
277 if let Some(ref value) = configuration.api_key {
278 req_builder = req_builder.header("X-API-KEY", value);
279 };
280 if let Some(ref token) = configuration.oauth_access_token {
281 req_builder = req_builder.bearer_auth(token.to_owned());
282 };
283
284 let req = req_builder.build()?;
285 let resp = configuration.client.execute(req).await?;
286
287 let status = resp.status();
288
289 if !status.is_client_error() && !status.is_server_error() {
290 Ok(())
291 } else {
292 let content = resp.text().await?;
293 let entity: Option<GetObjectMetadataError> = serde_json::from_str(&content).ok();
294 Err(Error::ResponseError(ResponseContent { status, content, entity }))
295 }
296}
297
298pub async fn list_objects(configuration: &Configuration, prefix: Option<&str>, headers: Option<ListObjectsHeaders>) -> Result<Vec<models::ObjectMetadata>, Error<ListObjectsError>> {
299 let p_query_prefix = prefix;
301
302 let uri_str = format!("{}/obj/", build_url(configuration));
303 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
304
305 if let Some(h) = &headers {
307 if let Some(param_value) = h.x_dtz_realm.as_ref() {
308 req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
309 }
310 }
311
312 if let Some(ref param_value) = p_query_prefix {
313 req_builder = req_builder.query(&[("prefix", ¶m_value.to_string())]);
314 }
315 if let Some(ref value) = configuration.api_key {
316 req_builder = req_builder.header("X-API-KEY", value);
317 };
318 if let Some(ref token) = configuration.oauth_access_token {
319 req_builder = req_builder.bearer_auth(token.to_owned());
320 };
321
322 let req = req_builder.build()?;
323 let resp = configuration.client.execute(req).await?;
324
325 let status = resp.status();
326 let content_type = resp
327 .headers()
328 .get("content-type")
329 .and_then(|v| v.to_str().ok())
330 .unwrap_or("application/octet-stream");
331 let content_type = super::ContentType::from(content_type);
332
333 if !status.is_client_error() && !status.is_server_error() {
334 let content = resp.text().await?;
335 match content_type {
336 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
337 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::ObjectMetadata>`"))),
338 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::ObjectMetadata>`")))),
339 }
340 } else {
341 let content = resp.text().await?;
342 let entity: Option<ListObjectsError> = serde_json::from_str(&content).ok();
343 Err(Error::ResponseError(ResponseContent { status, content, entity }))
344 }
345}
346
347pub async fn put_object(configuration: &Configuration, object_path: &str, body: Option<Vec<u8>>, headers: Option<PutObjectHeaders>) -> Result<(), Error<PutObjectError>> {
348 let p_path_object_path = object_path;
350 let p_body_body = body;
351
352 let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
353 let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
354
355 if let Some(h) = &headers {
357 if let Some(param_value) = h.x_dtz_expiration.as_ref() {
358 req_builder = req_builder.header("X-DTZ-EXPIRATION", param_value.to_string());
359 }
360 if let Some(param_value) = h.x_dtz_expire_in.as_ref() {
361 req_builder = req_builder.header("X-DTZ-EXPIRE-IN", param_value.to_string());
362 }
363 if let Some(param_value) = h.x_dtz_expire_at.as_ref() {
364 req_builder = req_builder.header("X-DTZ-EXPIRE-AT", param_value.to_string());
365 }
366 if let Some(param_value) = h.x_dtz_realm.as_ref() {
367 req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
368 }
369 }
370
371 if let Some(ref value) = configuration.api_key {
372 req_builder = req_builder.header("X-API-KEY", value);
373 };
374 if let Some(ref token) = configuration.oauth_access_token {
375 req_builder = req_builder.bearer_auth(token.to_owned());
376 };
377 req_builder = req_builder.body(p_body_body.unwrap_or_default());
378
379 let req = req_builder.build()?;
380 let resp = configuration.client.execute(req).await?;
381
382 let status = resp.status();
383
384 if !status.is_client_error() && !status.is_server_error() {
385 Ok(())
386 } else {
387 let content = resp.text().await?;
388 let entity: Option<PutObjectError> = serde_json::from_str(&content).ok();
389 Err(Error::ResponseError(ResponseContent { status, content, entity }))
390 }
391}
392
393pub async fn stats(configuration: &Configuration) -> Result<models::Stats, Error<StatsError>> {
394
395 let uri_str = format!("{}/stats", build_url(configuration));
396 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
397
398
399 if let Some(ref value) = configuration.api_key {
400 req_builder = req_builder.header("X-API-KEY", value);
401 };
402 if let Some(ref token) = configuration.oauth_access_token {
403 req_builder = req_builder.bearer_auth(token.to_owned());
404 };
405
406 let req = req_builder.build()?;
407 let resp = configuration.client.execute(req).await?;
408
409 let status = resp.status();
410 let content_type = resp
411 .headers()
412 .get("content-type")
413 .and_then(|v| v.to_str().ok())
414 .unwrap_or("application/octet-stream");
415 let content_type = super::ContentType::from(content_type);
416
417 if !status.is_client_error() && !status.is_server_error() {
418 let content = resp.text().await?;
419 match content_type {
420 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
421 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Stats`"))),
422 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Stats`")))),
423 }
424 } else {
425 let content = resp.text().await?;
426 let entity: Option<StatsError> = serde_json::from_str(&content).ok();
427 Err(Error::ResponseError(ResponseContent { status, content, entity }))
428 }
429}
430