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