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 PutObjectHeaders {
35 pub x_dtz_expiration: Option<String>,
37 pub x_dtz_expire_in: Option<String>,
39 pub x_dtz_expire_at: Option<String>,
41 pub x_dtz_realm: Option<String>,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum DeleteObjectError {
49 UnknownValue(serde_json::Value),
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize)]
54#[serde(untagged)]
55pub enum DisableServiceError {
56 UnknownValue(serde_json::Value),
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
61#[serde(untagged)]
62pub enum EnableServiceError {
63 UnknownValue(serde_json::Value),
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
68#[serde(untagged)]
69pub enum GetObjectError {
70 UnknownValue(serde_json::Value),
71}
72
73#[derive(Debug, Clone, Serialize, Deserialize)]
75#[serde(untagged)]
76pub enum GetObjectMetadataError {
77 UnknownValue(serde_json::Value),
78}
79
80#[derive(Debug, Clone, Serialize, Deserialize)]
82#[serde(untagged)]
83pub enum ListObjectsError {
84 UnknownValue(serde_json::Value),
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
89#[serde(untagged)]
90pub enum PutObjectError {
91 UnknownValue(serde_json::Value),
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(untagged)]
97pub enum StatsError {
98 UnknownValue(serde_json::Value),
99}
100
101
102pub async fn delete_object(configuration: &Configuration, object_path: &str) -> Result<(), Error<DeleteObjectError>> {
104 let p_path_object_path = object_path;
106
107 let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
108 let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
109
110
111 if let Some(ref value) = configuration.api_key {
112 req_builder = req_builder.header("X-API-KEY", value);
113 };
114 if let Some(ref token) = configuration.oauth_access_token {
115 req_builder = req_builder.bearer_auth(token.to_owned());
116 };
117
118 let req = req_builder.build()?;
119 let resp = configuration.client.execute(req).await?;
120
121 let status = resp.status();
122
123 if !status.is_client_error() && !status.is_server_error() {
124 Ok(())
125 } else {
126 let content = resp.text().await?;
127 let entity: Option<DeleteObjectError> = serde_json::from_str(&content).ok();
128 Err(Error::ResponseError(ResponseContent { status, content, entity }))
129 }
130}
131
132pub async fn disable_service(configuration: &Configuration) -> Result<(), Error<DisableServiceError>> {
133
134 let uri_str = format!("{}/disable", build_url(configuration));
135 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
136
137
138 if let Some(ref value) = configuration.api_key {
139 req_builder = req_builder.header("X-API-KEY", value);
140 };
141 if let Some(ref token) = configuration.oauth_access_token {
142 req_builder = req_builder.bearer_auth(token.to_owned());
143 };
144
145 let req = req_builder.build()?;
146 let resp = configuration.client.execute(req).await?;
147
148 let status = resp.status();
149
150 if !status.is_client_error() && !status.is_server_error() {
151 Ok(())
152 } else {
153 let content = resp.text().await?;
154 let entity: Option<DisableServiceError> = serde_json::from_str(&content).ok();
155 Err(Error::ResponseError(ResponseContent { status, content, entity }))
156 }
157}
158
159pub async fn enable_service(configuration: &Configuration) -> Result<(), Error<EnableServiceError>> {
160
161 let uri_str = format!("{}/enable", build_url(configuration));
162 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
163
164
165 if let Some(ref value) = configuration.api_key {
166 req_builder = req_builder.header("X-API-KEY", value);
167 };
168 if let Some(ref token) = configuration.oauth_access_token {
169 req_builder = req_builder.bearer_auth(token.to_owned());
170 };
171
172 let req = req_builder.build()?;
173 let resp = configuration.client.execute(req).await?;
174
175 let status = resp.status();
176
177 if !status.is_client_error() && !status.is_server_error() {
178 Ok(())
179 } else {
180 let content = resp.text().await?;
181 let entity: Option<EnableServiceError> = serde_json::from_str(&content).ok();
182 Err(Error::ResponseError(ResponseContent { status, content, entity }))
183 }
184}
185
186pub async fn get_object(configuration: &Configuration, object_path: &str) -> Result<reqwest::Response, Error<GetObjectError>> {
187 let p_path_object_path = object_path;
189
190 let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
191 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
192
193
194 if let Some(ref value) = configuration.api_key {
195 req_builder = req_builder.header("X-API-KEY", value);
196 };
197 if let Some(ref token) = configuration.oauth_access_token {
198 req_builder = req_builder.bearer_auth(token.to_owned());
199 };
200
201 let req = req_builder.build()?;
202 let resp = configuration.client.execute(req).await?;
203
204 let status = resp.status();
205
206 if !status.is_client_error() && !status.is_server_error() {
207 Ok(resp)
208 } else {
209 let content = resp.text().await?;
210 let entity: Option<GetObjectError> = serde_json::from_str(&content).ok();
211 Err(Error::ResponseError(ResponseContent { status, content, entity }))
212 }
213}
214
215pub async fn get_object_metadata(configuration: &Configuration, object_path: &str) -> Result<models::ObjectMetadata, Error<GetObjectMetadataError>> {
216 let p_path_object_path = object_path;
218
219 let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
220 let mut req_builder = configuration.client.request(reqwest::Method::HEAD, &uri_str);
221
222
223 if let Some(ref value) = configuration.api_key {
224 req_builder = req_builder.header("X-API-KEY", value);
225 };
226 if let Some(ref token) = configuration.oauth_access_token {
227 req_builder = req_builder.bearer_auth(token.to_owned());
228 };
229
230 let req = req_builder.build()?;
231 let resp = configuration.client.execute(req).await?;
232
233 let status = resp.status();
234 let content_type = resp
235 .headers()
236 .get("content-type")
237 .and_then(|v| v.to_str().ok())
238 .unwrap_or("application/octet-stream");
239 let content_type = super::ContentType::from(content_type);
240
241 if !status.is_client_error() && !status.is_server_error() {
242 let content = resp.text().await?;
243 match content_type {
244 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
245 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ObjectMetadata`"))),
246 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::ObjectMetadata`")))),
247 }
248 } else {
249 let content = resp.text().await?;
250 let entity: Option<GetObjectMetadataError> = serde_json::from_str(&content).ok();
251 Err(Error::ResponseError(ResponseContent { status, content, entity }))
252 }
253}
254
255pub async fn list_objects(configuration: &Configuration, prefix: Option<&str>) -> Result<Vec<models::ObjectMetadata>, Error<ListObjectsError>> {
256 let p_query_prefix = prefix;
258
259 let uri_str = format!("{}/obj/", build_url(configuration));
260 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
261
262
263 if let Some(ref param_value) = p_query_prefix {
264 req_builder = req_builder.query(&[("prefix", ¶m_value.to_string())]);
265 }
266 if let Some(ref value) = configuration.api_key {
267 req_builder = req_builder.header("X-API-KEY", value);
268 };
269 if let Some(ref token) = configuration.oauth_access_token {
270 req_builder = req_builder.bearer_auth(token.to_owned());
271 };
272
273 let req = req_builder.build()?;
274 let resp = configuration.client.execute(req).await?;
275
276 let status = resp.status();
277 let content_type = resp
278 .headers()
279 .get("content-type")
280 .and_then(|v| v.to_str().ok())
281 .unwrap_or("application/octet-stream");
282 let content_type = super::ContentType::from(content_type);
283
284 if !status.is_client_error() && !status.is_server_error() {
285 let content = resp.text().await?;
286 match content_type {
287 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
288 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::ObjectMetadata>`"))),
289 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>`")))),
290 }
291 } else {
292 let content = resp.text().await?;
293 let entity: Option<ListObjectsError> = serde_json::from_str(&content).ok();
294 Err(Error::ResponseError(ResponseContent { status, content, entity }))
295 }
296}
297
298pub async fn put_object(configuration: &Configuration, object_path: &str, body: Option<Vec<u8>>, headers: Option<PutObjectHeaders>) -> Result<(), Error<PutObjectError>> {
299 let p_path_object_path = object_path;
301 let p_body_body = body;
302
303 let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
304 let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
305
306 if let Some(h) = &headers {
308 if let Some(param_value) = h.x_dtz_expiration.as_ref() {
309 req_builder = req_builder.header("X-DTZ-EXPIRATION", param_value.to_string());
310 }
311 if let Some(param_value) = h.x_dtz_expire_in.as_ref() {
312 req_builder = req_builder.header("X-DTZ-EXPIRE-IN", param_value.to_string());
313 }
314 if let Some(param_value) = h.x_dtz_expire_at.as_ref() {
315 req_builder = req_builder.header("X-DTZ-EXPIRE-AT", param_value.to_string());
316 }
317 if let Some(param_value) = h.x_dtz_realm.as_ref() {
318 req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
319 }
320 }
321
322 if let Some(ref value) = configuration.api_key {
323 req_builder = req_builder.header("X-API-KEY", value);
324 };
325 if let Some(ref token) = configuration.oauth_access_token {
326 req_builder = req_builder.bearer_auth(token.to_owned());
327 };
328 req_builder = req_builder.body(p_body_body.unwrap_or_default());
329
330 let req = req_builder.build()?;
331 let resp = configuration.client.execute(req).await?;
332
333 let status = resp.status();
334
335 if !status.is_client_error() && !status.is_server_error() {
336 Ok(())
337 } else {
338 let content = resp.text().await?;
339 let entity: Option<PutObjectError> = serde_json::from_str(&content).ok();
340 Err(Error::ResponseError(ResponseContent { status, content, entity }))
341 }
342}
343
344pub async fn stats(configuration: &Configuration) -> Result<models::Stats, Error<StatsError>> {
345
346 let uri_str = format!("{}/stats", build_url(configuration));
347 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
348
349
350 if let Some(ref value) = configuration.api_key {
351 req_builder = req_builder.header("X-API-KEY", value);
352 };
353 if let Some(ref token) = configuration.oauth_access_token {
354 req_builder = req_builder.bearer_auth(token.to_owned());
355 };
356
357 let req = req_builder.build()?;
358 let resp = configuration.client.execute(req).await?;
359
360 let status = resp.status();
361 let content_type = resp
362 .headers()
363 .get("content-type")
364 .and_then(|v| v.to_str().ok())
365 .unwrap_or("application/octet-stream");
366 let content_type = super::ContentType::from(content_type);
367
368 if !status.is_client_error() && !status.is_server_error() {
369 let content = resp.text().await?;
370 match content_type {
371 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
372 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Stats`"))),
373 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`")))),
374 }
375 } else {
376 let content = resp.text().await?;
377 let entity: Option<StatsError> = serde_json::from_str(&content).ok();
378 Err(Error::ResponseError(ResponseContent { status, content, entity }))
379 }
380}
381