1use crate::datadog;
5use flate2::{
6 write::{GzEncoder, ZlibEncoder},
7 Compression,
8};
9use reqwest::header::{HeaderMap, HeaderValue};
10use serde::{Deserialize, Serialize};
11use std::io::Write;
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
15#[serde(untagged)]
16pub enum CreateUserError {
17 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
18 UnknownValue(serde_json::Value),
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
23#[serde(untagged)]
24pub enum DisableUserError {
25 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
26 UnknownValue(serde_json::Value),
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
31#[serde(untagged)]
32pub enum GetUserError {
33 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
34 UnknownValue(serde_json::Value),
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum ListUsersError {
41 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
42 UnknownValue(serde_json::Value),
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum UpdateUserError {
49 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
50 UnknownValue(serde_json::Value),
51}
52
53#[derive(Debug, Clone)]
55pub struct UsersAPI {
56 config: datadog::Configuration,
57 client: reqwest_middleware::ClientWithMiddleware,
58}
59
60impl Default for UsersAPI {
61 fn default() -> Self {
62 Self::with_config(datadog::Configuration::default())
63 }
64}
65
66impl UsersAPI {
67 pub fn new() -> Self {
68 Self::default()
69 }
70 pub fn with_config(config: datadog::Configuration) -> Self {
71 let mut reqwest_client_builder = reqwest::Client::builder();
72
73 if let Some(proxy_url) = &config.proxy_url {
74 let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
75 reqwest_client_builder = reqwest_client_builder.proxy(proxy);
76 }
77
78 let mut middleware_client_builder =
79 reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
80
81 if config.enable_retry {
82 struct RetryableStatus;
83 impl reqwest_retry::RetryableStrategy for RetryableStatus {
84 fn handle(
85 &self,
86 res: &Result<reqwest::Response, reqwest_middleware::Error>,
87 ) -> Option<reqwest_retry::Retryable> {
88 match res {
89 Ok(success) => reqwest_retry::default_on_request_success(success),
90 Err(_) => None,
91 }
92 }
93 }
94 let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
95 .build_with_max_retries(config.max_retries);
96
97 let retry_middleware =
98 reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
99 backoff_policy,
100 RetryableStatus,
101 );
102
103 middleware_client_builder = middleware_client_builder.with(retry_middleware);
104 }
105
106 let client = middleware_client_builder.build();
107
108 Self { config, client }
109 }
110
111 pub fn with_client_and_config(
112 config: datadog::Configuration,
113 client: reqwest_middleware::ClientWithMiddleware,
114 ) -> Self {
115 Self { config, client }
116 }
117
118 pub async fn create_user(
123 &self,
124 body: crate::datadogV1::model::User,
125 ) -> Result<crate::datadogV1::model::UserResponse, datadog::Error<CreateUserError>> {
126 match self.create_user_with_http_info(body).await {
127 Ok(response_content) => {
128 if let Some(e) = response_content.entity {
129 Ok(e)
130 } else {
131 Err(datadog::Error::Serde(serde::de::Error::custom(
132 "response content was None",
133 )))
134 }
135 }
136 Err(err) => Err(err),
137 }
138 }
139
140 pub async fn create_user_with_http_info(
145 &self,
146 body: crate::datadogV1::model::User,
147 ) -> Result<
148 datadog::ResponseContent<crate::datadogV1::model::UserResponse>,
149 datadog::Error<CreateUserError>,
150 > {
151 let local_configuration = &self.config;
152 let operation_id = "v1.create_user";
153
154 let local_client = &self.client;
155
156 let local_uri_str = format!(
157 "{}/api/v1/user",
158 local_configuration.get_operation_host(operation_id)
159 );
160 let mut local_req_builder =
161 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
162
163 let mut headers = HeaderMap::new();
165 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
166 headers.insert("Accept", HeaderValue::from_static("application/json"));
167
168 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
170 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
171 Err(e) => {
172 log::warn!("Failed to parse user agent header: {e}, falling back to default");
173 headers.insert(
174 reqwest::header::USER_AGENT,
175 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
176 )
177 }
178 };
179
180 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
182 headers.insert(
183 "DD-API-KEY",
184 HeaderValue::from_str(local_key.key.as_str())
185 .expect("failed to parse DD-API-KEY header"),
186 );
187 };
188 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
189 headers.insert(
190 "DD-APPLICATION-KEY",
191 HeaderValue::from_str(local_key.key.as_str())
192 .expect("failed to parse DD-APPLICATION-KEY header"),
193 );
194 };
195
196 let output = Vec::new();
198 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
199 if body.serialize(&mut ser).is_ok() {
200 if let Some(content_encoding) = headers.get("Content-Encoding") {
201 match content_encoding.to_str().unwrap_or_default() {
202 "gzip" => {
203 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
204 let _ = enc.write_all(ser.into_inner().as_slice());
205 match enc.finish() {
206 Ok(buf) => {
207 local_req_builder = local_req_builder.body(buf);
208 }
209 Err(e) => return Err(datadog::Error::Io(e)),
210 }
211 }
212 "deflate" => {
213 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
214 let _ = enc.write_all(ser.into_inner().as_slice());
215 match enc.finish() {
216 Ok(buf) => {
217 local_req_builder = local_req_builder.body(buf);
218 }
219 Err(e) => return Err(datadog::Error::Io(e)),
220 }
221 }
222 "zstd1" => {
223 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
224 let _ = enc.write_all(ser.into_inner().as_slice());
225 match enc.finish() {
226 Ok(buf) => {
227 local_req_builder = local_req_builder.body(buf);
228 }
229 Err(e) => return Err(datadog::Error::Io(e)),
230 }
231 }
232 _ => {
233 local_req_builder = local_req_builder.body(ser.into_inner());
234 }
235 }
236 } else {
237 local_req_builder = local_req_builder.body(ser.into_inner());
238 }
239 }
240
241 local_req_builder = local_req_builder.headers(headers);
242 let local_req = local_req_builder.build()?;
243 log::debug!("request content: {:?}", local_req.body());
244 let local_resp = local_client.execute(local_req).await?;
245
246 let local_status = local_resp.status();
247 let local_content = local_resp.text().await?;
248 log::debug!("response content: {}", local_content);
249
250 if !local_status.is_client_error() && !local_status.is_server_error() {
251 match serde_json::from_str::<crate::datadogV1::model::UserResponse>(&local_content) {
252 Ok(e) => {
253 return Ok(datadog::ResponseContent {
254 status: local_status,
255 content: local_content,
256 entity: Some(e),
257 })
258 }
259 Err(e) => return Err(datadog::Error::Serde(e)),
260 };
261 } else {
262 let local_entity: Option<CreateUserError> = serde_json::from_str(&local_content).ok();
263 let local_error = datadog::ResponseContent {
264 status: local_status,
265 content: local_content,
266 entity: local_entity,
267 };
268 Err(datadog::Error::ResponseError(local_error))
269 }
270 }
271
272 pub async fn disable_user(
277 &self,
278 user_handle: String,
279 ) -> Result<crate::datadogV1::model::UserDisableResponse, datadog::Error<DisableUserError>>
280 {
281 match self.disable_user_with_http_info(user_handle).await {
282 Ok(response_content) => {
283 if let Some(e) = response_content.entity {
284 Ok(e)
285 } else {
286 Err(datadog::Error::Serde(serde::de::Error::custom(
287 "response content was None",
288 )))
289 }
290 }
291 Err(err) => Err(err),
292 }
293 }
294
295 pub async fn disable_user_with_http_info(
300 &self,
301 user_handle: String,
302 ) -> Result<
303 datadog::ResponseContent<crate::datadogV1::model::UserDisableResponse>,
304 datadog::Error<DisableUserError>,
305 > {
306 let local_configuration = &self.config;
307 let operation_id = "v1.disable_user";
308
309 let local_client = &self.client;
310
311 let local_uri_str = format!(
312 "{}/api/v1/user/{user_handle}",
313 local_configuration.get_operation_host(operation_id),
314 user_handle = datadog::urlencode(user_handle)
315 );
316 let mut local_req_builder =
317 local_client.request(reqwest::Method::DELETE, local_uri_str.as_str());
318
319 let mut headers = HeaderMap::new();
321 headers.insert("Accept", HeaderValue::from_static("application/json"));
322
323 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
325 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
326 Err(e) => {
327 log::warn!("Failed to parse user agent header: {e}, falling back to default");
328 headers.insert(
329 reqwest::header::USER_AGENT,
330 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
331 )
332 }
333 };
334
335 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
337 headers.insert(
338 "DD-API-KEY",
339 HeaderValue::from_str(local_key.key.as_str())
340 .expect("failed to parse DD-API-KEY header"),
341 );
342 };
343 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
344 headers.insert(
345 "DD-APPLICATION-KEY",
346 HeaderValue::from_str(local_key.key.as_str())
347 .expect("failed to parse DD-APPLICATION-KEY header"),
348 );
349 };
350
351 local_req_builder = local_req_builder.headers(headers);
352 let local_req = local_req_builder.build()?;
353 log::debug!("request content: {:?}", local_req.body());
354 let local_resp = local_client.execute(local_req).await?;
355
356 let local_status = local_resp.status();
357 let local_content = local_resp.text().await?;
358 log::debug!("response content: {}", local_content);
359
360 if !local_status.is_client_error() && !local_status.is_server_error() {
361 match serde_json::from_str::<crate::datadogV1::model::UserDisableResponse>(
362 &local_content,
363 ) {
364 Ok(e) => {
365 return Ok(datadog::ResponseContent {
366 status: local_status,
367 content: local_content,
368 entity: Some(e),
369 })
370 }
371 Err(e) => return Err(datadog::Error::Serde(e)),
372 };
373 } else {
374 let local_entity: Option<DisableUserError> = serde_json::from_str(&local_content).ok();
375 let local_error = datadog::ResponseContent {
376 status: local_status,
377 content: local_content,
378 entity: local_entity,
379 };
380 Err(datadog::Error::ResponseError(local_error))
381 }
382 }
383
384 pub async fn get_user(
386 &self,
387 user_handle: String,
388 ) -> Result<crate::datadogV1::model::UserResponse, datadog::Error<GetUserError>> {
389 match self.get_user_with_http_info(user_handle).await {
390 Ok(response_content) => {
391 if let Some(e) = response_content.entity {
392 Ok(e)
393 } else {
394 Err(datadog::Error::Serde(serde::de::Error::custom(
395 "response content was None",
396 )))
397 }
398 }
399 Err(err) => Err(err),
400 }
401 }
402
403 pub async fn get_user_with_http_info(
405 &self,
406 user_handle: String,
407 ) -> Result<
408 datadog::ResponseContent<crate::datadogV1::model::UserResponse>,
409 datadog::Error<GetUserError>,
410 > {
411 let local_configuration = &self.config;
412 let operation_id = "v1.get_user";
413
414 let local_client = &self.client;
415
416 let local_uri_str = format!(
417 "{}/api/v1/user/{user_handle}",
418 local_configuration.get_operation_host(operation_id),
419 user_handle = datadog::urlencode(user_handle)
420 );
421 let mut local_req_builder =
422 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
423
424 let mut headers = HeaderMap::new();
426 headers.insert("Accept", HeaderValue::from_static("application/json"));
427
428 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
430 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
431 Err(e) => {
432 log::warn!("Failed to parse user agent header: {e}, falling back to default");
433 headers.insert(
434 reqwest::header::USER_AGENT,
435 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
436 )
437 }
438 };
439
440 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
442 headers.insert(
443 "DD-API-KEY",
444 HeaderValue::from_str(local_key.key.as_str())
445 .expect("failed to parse DD-API-KEY header"),
446 );
447 };
448 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
449 headers.insert(
450 "DD-APPLICATION-KEY",
451 HeaderValue::from_str(local_key.key.as_str())
452 .expect("failed to parse DD-APPLICATION-KEY header"),
453 );
454 };
455
456 local_req_builder = local_req_builder.headers(headers);
457 let local_req = local_req_builder.build()?;
458 log::debug!("request content: {:?}", local_req.body());
459 let local_resp = local_client.execute(local_req).await?;
460
461 let local_status = local_resp.status();
462 let local_content = local_resp.text().await?;
463 log::debug!("response content: {}", local_content);
464
465 if !local_status.is_client_error() && !local_status.is_server_error() {
466 match serde_json::from_str::<crate::datadogV1::model::UserResponse>(&local_content) {
467 Ok(e) => {
468 return Ok(datadog::ResponseContent {
469 status: local_status,
470 content: local_content,
471 entity: Some(e),
472 })
473 }
474 Err(e) => return Err(datadog::Error::Serde(e)),
475 };
476 } else {
477 let local_entity: Option<GetUserError> = serde_json::from_str(&local_content).ok();
478 let local_error = datadog::ResponseContent {
479 status: local_status,
480 content: local_content,
481 entity: local_entity,
482 };
483 Err(datadog::Error::ResponseError(local_error))
484 }
485 }
486
487 pub async fn list_users(
489 &self,
490 ) -> Result<crate::datadogV1::model::UserListResponse, datadog::Error<ListUsersError>> {
491 match self.list_users_with_http_info().await {
492 Ok(response_content) => {
493 if let Some(e) = response_content.entity {
494 Ok(e)
495 } else {
496 Err(datadog::Error::Serde(serde::de::Error::custom(
497 "response content was None",
498 )))
499 }
500 }
501 Err(err) => Err(err),
502 }
503 }
504
505 pub async fn list_users_with_http_info(
507 &self,
508 ) -> Result<
509 datadog::ResponseContent<crate::datadogV1::model::UserListResponse>,
510 datadog::Error<ListUsersError>,
511 > {
512 let local_configuration = &self.config;
513 let operation_id = "v1.list_users";
514
515 let local_client = &self.client;
516
517 let local_uri_str = format!(
518 "{}/api/v1/user",
519 local_configuration.get_operation_host(operation_id)
520 );
521 let mut local_req_builder =
522 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
523
524 let mut headers = HeaderMap::new();
526 headers.insert("Accept", HeaderValue::from_static("application/json"));
527
528 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
530 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
531 Err(e) => {
532 log::warn!("Failed to parse user agent header: {e}, falling back to default");
533 headers.insert(
534 reqwest::header::USER_AGENT,
535 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
536 )
537 }
538 };
539
540 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
542 headers.insert(
543 "DD-API-KEY",
544 HeaderValue::from_str(local_key.key.as_str())
545 .expect("failed to parse DD-API-KEY header"),
546 );
547 };
548 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
549 headers.insert(
550 "DD-APPLICATION-KEY",
551 HeaderValue::from_str(local_key.key.as_str())
552 .expect("failed to parse DD-APPLICATION-KEY header"),
553 );
554 };
555
556 local_req_builder = local_req_builder.headers(headers);
557 let local_req = local_req_builder.build()?;
558 log::debug!("request content: {:?}", local_req.body());
559 let local_resp = local_client.execute(local_req).await?;
560
561 let local_status = local_resp.status();
562 let local_content = local_resp.text().await?;
563 log::debug!("response content: {}", local_content);
564
565 if !local_status.is_client_error() && !local_status.is_server_error() {
566 match serde_json::from_str::<crate::datadogV1::model::UserListResponse>(&local_content)
567 {
568 Ok(e) => {
569 return Ok(datadog::ResponseContent {
570 status: local_status,
571 content: local_content,
572 entity: Some(e),
573 })
574 }
575 Err(e) => return Err(datadog::Error::Serde(e)),
576 };
577 } else {
578 let local_entity: Option<ListUsersError> = serde_json::from_str(&local_content).ok();
579 let local_error = datadog::ResponseContent {
580 status: local_status,
581 content: local_content,
582 entity: local_entity,
583 };
584 Err(datadog::Error::ResponseError(local_error))
585 }
586 }
587
588 pub async fn update_user(
592 &self,
593 user_handle: String,
594 body: crate::datadogV1::model::User,
595 ) -> Result<crate::datadogV1::model::UserResponse, datadog::Error<UpdateUserError>> {
596 match self.update_user_with_http_info(user_handle, body).await {
597 Ok(response_content) => {
598 if let Some(e) = response_content.entity {
599 Ok(e)
600 } else {
601 Err(datadog::Error::Serde(serde::de::Error::custom(
602 "response content was None",
603 )))
604 }
605 }
606 Err(err) => Err(err),
607 }
608 }
609
610 pub async fn update_user_with_http_info(
614 &self,
615 user_handle: String,
616 body: crate::datadogV1::model::User,
617 ) -> Result<
618 datadog::ResponseContent<crate::datadogV1::model::UserResponse>,
619 datadog::Error<UpdateUserError>,
620 > {
621 let local_configuration = &self.config;
622 let operation_id = "v1.update_user";
623
624 let local_client = &self.client;
625
626 let local_uri_str = format!(
627 "{}/api/v1/user/{user_handle}",
628 local_configuration.get_operation_host(operation_id),
629 user_handle = datadog::urlencode(user_handle)
630 );
631 let mut local_req_builder =
632 local_client.request(reqwest::Method::PUT, local_uri_str.as_str());
633
634 let mut headers = HeaderMap::new();
636 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
637 headers.insert("Accept", HeaderValue::from_static("application/json"));
638
639 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
641 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
642 Err(e) => {
643 log::warn!("Failed to parse user agent header: {e}, falling back to default");
644 headers.insert(
645 reqwest::header::USER_AGENT,
646 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
647 )
648 }
649 };
650
651 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
653 headers.insert(
654 "DD-API-KEY",
655 HeaderValue::from_str(local_key.key.as_str())
656 .expect("failed to parse DD-API-KEY header"),
657 );
658 };
659 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
660 headers.insert(
661 "DD-APPLICATION-KEY",
662 HeaderValue::from_str(local_key.key.as_str())
663 .expect("failed to parse DD-APPLICATION-KEY header"),
664 );
665 };
666
667 let output = Vec::new();
669 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
670 if body.serialize(&mut ser).is_ok() {
671 if let Some(content_encoding) = headers.get("Content-Encoding") {
672 match content_encoding.to_str().unwrap_or_default() {
673 "gzip" => {
674 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
675 let _ = enc.write_all(ser.into_inner().as_slice());
676 match enc.finish() {
677 Ok(buf) => {
678 local_req_builder = local_req_builder.body(buf);
679 }
680 Err(e) => return Err(datadog::Error::Io(e)),
681 }
682 }
683 "deflate" => {
684 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
685 let _ = enc.write_all(ser.into_inner().as_slice());
686 match enc.finish() {
687 Ok(buf) => {
688 local_req_builder = local_req_builder.body(buf);
689 }
690 Err(e) => return Err(datadog::Error::Io(e)),
691 }
692 }
693 "zstd1" => {
694 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
695 let _ = enc.write_all(ser.into_inner().as_slice());
696 match enc.finish() {
697 Ok(buf) => {
698 local_req_builder = local_req_builder.body(buf);
699 }
700 Err(e) => return Err(datadog::Error::Io(e)),
701 }
702 }
703 _ => {
704 local_req_builder = local_req_builder.body(ser.into_inner());
705 }
706 }
707 } else {
708 local_req_builder = local_req_builder.body(ser.into_inner());
709 }
710 }
711
712 local_req_builder = local_req_builder.headers(headers);
713 let local_req = local_req_builder.build()?;
714 log::debug!("request content: {:?}", local_req.body());
715 let local_resp = local_client.execute(local_req).await?;
716
717 let local_status = local_resp.status();
718 let local_content = local_resp.text().await?;
719 log::debug!("response content: {}", local_content);
720
721 if !local_status.is_client_error() && !local_status.is_server_error() {
722 match serde_json::from_str::<crate::datadogV1::model::UserResponse>(&local_content) {
723 Ok(e) => {
724 return Ok(datadog::ResponseContent {
725 status: local_status,
726 content: local_content,
727 entity: Some(e),
728 })
729 }
730 Err(e) => return Err(datadog::Error::Serde(e)),
731 };
732 } else {
733 let local_entity: Option<UpdateUserError> = serde_json::from_str(&local_content).ok();
734 let local_error = datadog::ResponseContent {
735 status: local_status,
736 content: local_content,
737 entity: local_entity,
738 };
739 Err(datadog::Error::ResponseError(local_error))
740 }
741 }
742}