1use crate::datadog;
5use flate2::{
6 write::{GzEncoder, ZlibEncoder},
7 Compression,
8};
9use log::warn;
10use reqwest::header::{HeaderMap, HeaderValue};
11use serde::{Deserialize, Serialize};
12use std::io::Write;
13
14#[non_exhaustive]
16#[derive(Clone, Default, Debug)]
17pub struct GetIncidentServiceOptionalParams {
18 pub include: Option<crate::datadogV2::model::IncidentRelatedObject>,
20}
21
22impl GetIncidentServiceOptionalParams {
23 pub fn include(mut self, value: crate::datadogV2::model::IncidentRelatedObject) -> Self {
25 self.include = Some(value);
26 self
27 }
28}
29
30#[non_exhaustive]
32#[derive(Clone, Default, Debug)]
33pub struct ListIncidentServicesOptionalParams {
34 pub include: Option<crate::datadogV2::model::IncidentRelatedObject>,
36 pub page_size: Option<i64>,
38 pub page_offset: Option<i64>,
40 pub filter: Option<String>,
42}
43
44impl ListIncidentServicesOptionalParams {
45 pub fn include(mut self, value: crate::datadogV2::model::IncidentRelatedObject) -> Self {
47 self.include = Some(value);
48 self
49 }
50 pub fn page_size(mut self, value: i64) -> Self {
52 self.page_size = Some(value);
53 self
54 }
55 pub fn page_offset(mut self, value: i64) -> Self {
57 self.page_offset = Some(value);
58 self
59 }
60 pub fn filter(mut self, value: String) -> Self {
62 self.filter = Some(value);
63 self
64 }
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
69#[serde(untagged)]
70pub enum CreateIncidentServiceError {
71 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
72 UnknownValue(serde_json::Value),
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
77#[serde(untagged)]
78pub enum DeleteIncidentServiceError {
79 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
80 UnknownValue(serde_json::Value),
81}
82
83#[derive(Debug, Clone, Serialize, Deserialize)]
85#[serde(untagged)]
86pub enum GetIncidentServiceError {
87 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
88 UnknownValue(serde_json::Value),
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
93#[serde(untagged)]
94pub enum ListIncidentServicesError {
95 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
96 UnknownValue(serde_json::Value),
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize)]
101#[serde(untagged)]
102pub enum UpdateIncidentServiceError {
103 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
104 UnknownValue(serde_json::Value),
105}
106
107#[derive(Debug, Clone)]
109pub struct IncidentServicesAPI {
110 config: datadog::Configuration,
111 client: reqwest_middleware::ClientWithMiddleware,
112}
113
114impl Default for IncidentServicesAPI {
115 fn default() -> Self {
116 Self::with_config(datadog::Configuration::default())
117 }
118}
119
120impl IncidentServicesAPI {
121 pub fn new() -> Self {
122 Self::default()
123 }
124 pub fn with_config(config: datadog::Configuration) -> Self {
125 let mut reqwest_client_builder = reqwest::Client::builder();
126
127 if let Some(proxy_url) = &config.proxy_url {
128 let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
129 reqwest_client_builder = reqwest_client_builder.proxy(proxy);
130 }
131
132 let mut middleware_client_builder =
133 reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
134
135 if config.enable_retry {
136 struct RetryableStatus;
137 impl reqwest_retry::RetryableStrategy for RetryableStatus {
138 fn handle(
139 &self,
140 res: &Result<reqwest::Response, reqwest_middleware::Error>,
141 ) -> Option<reqwest_retry::Retryable> {
142 match res {
143 Ok(success) => reqwest_retry::default_on_request_success(success),
144 Err(_) => None,
145 }
146 }
147 }
148 let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
149 .build_with_max_retries(config.max_retries);
150
151 let retry_middleware =
152 reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
153 backoff_policy,
154 RetryableStatus,
155 );
156
157 middleware_client_builder = middleware_client_builder.with(retry_middleware);
158 }
159
160 let client = middleware_client_builder.build();
161
162 Self { config, client }
163 }
164
165 pub fn with_client_and_config(
166 config: datadog::Configuration,
167 client: reqwest_middleware::ClientWithMiddleware,
168 ) -> Self {
169 Self { config, client }
170 }
171
172 pub async fn create_incident_service(
174 &self,
175 body: crate::datadogV2::model::IncidentServiceCreateRequest,
176 ) -> Result<
177 crate::datadogV2::model::IncidentServiceResponse,
178 datadog::Error<CreateIncidentServiceError>,
179 > {
180 match self.create_incident_service_with_http_info(body).await {
181 Ok(response_content) => {
182 if let Some(e) = response_content.entity {
183 Ok(e)
184 } else {
185 Err(datadog::Error::Serde(serde::de::Error::custom(
186 "response content was None",
187 )))
188 }
189 }
190 Err(err) => Err(err),
191 }
192 }
193
194 pub async fn create_incident_service_with_http_info(
196 &self,
197 body: crate::datadogV2::model::IncidentServiceCreateRequest,
198 ) -> Result<
199 datadog::ResponseContent<crate::datadogV2::model::IncidentServiceResponse>,
200 datadog::Error<CreateIncidentServiceError>,
201 > {
202 let local_configuration = &self.config;
203 let operation_id = "v2.create_incident_service";
204 if local_configuration.is_unstable_operation_enabled(operation_id) {
205 warn!("Using unstable operation {operation_id}");
206 } else {
207 let local_error = datadog::UnstableOperationDisabledError {
208 msg: "Operation 'v2.create_incident_service' is not enabled".to_string(),
209 };
210 return Err(datadog::Error::UnstableOperationDisabledError(local_error));
211 }
212
213 let local_client = &self.client;
214
215 let local_uri_str = format!(
216 "{}/api/v2/services",
217 local_configuration.get_operation_host(operation_id)
218 );
219 let mut local_req_builder =
220 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
221
222 let mut headers = HeaderMap::new();
224 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
225 headers.insert("Accept", HeaderValue::from_static("application/json"));
226
227 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
229 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
230 Err(e) => {
231 log::warn!("Failed to parse user agent header: {e}, falling back to default");
232 headers.insert(
233 reqwest::header::USER_AGENT,
234 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
235 )
236 }
237 };
238
239 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
241 headers.insert(
242 "DD-API-KEY",
243 HeaderValue::from_str(local_key.key.as_str())
244 .expect("failed to parse DD-API-KEY header"),
245 );
246 };
247 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
248 headers.insert(
249 "DD-APPLICATION-KEY",
250 HeaderValue::from_str(local_key.key.as_str())
251 .expect("failed to parse DD-APPLICATION-KEY header"),
252 );
253 };
254
255 let output = Vec::new();
257 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
258 if body.serialize(&mut ser).is_ok() {
259 if let Some(content_encoding) = headers.get("Content-Encoding") {
260 match content_encoding.to_str().unwrap_or_default() {
261 "gzip" => {
262 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
263 let _ = enc.write_all(ser.into_inner().as_slice());
264 match enc.finish() {
265 Ok(buf) => {
266 local_req_builder = local_req_builder.body(buf);
267 }
268 Err(e) => return Err(datadog::Error::Io(e)),
269 }
270 }
271 "deflate" => {
272 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
273 let _ = enc.write_all(ser.into_inner().as_slice());
274 match enc.finish() {
275 Ok(buf) => {
276 local_req_builder = local_req_builder.body(buf);
277 }
278 Err(e) => return Err(datadog::Error::Io(e)),
279 }
280 }
281 "zstd1" => {
282 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
283 let _ = enc.write_all(ser.into_inner().as_slice());
284 match enc.finish() {
285 Ok(buf) => {
286 local_req_builder = local_req_builder.body(buf);
287 }
288 Err(e) => return Err(datadog::Error::Io(e)),
289 }
290 }
291 _ => {
292 local_req_builder = local_req_builder.body(ser.into_inner());
293 }
294 }
295 } else {
296 local_req_builder = local_req_builder.body(ser.into_inner());
297 }
298 }
299
300 local_req_builder = local_req_builder.headers(headers);
301 let local_req = local_req_builder.build()?;
302 log::debug!("request content: {:?}", local_req.body());
303 let local_resp = local_client.execute(local_req).await?;
304
305 let local_status = local_resp.status();
306 let local_content = local_resp.text().await?;
307 log::debug!("response content: {}", local_content);
308
309 if !local_status.is_client_error() && !local_status.is_server_error() {
310 match serde_json::from_str::<crate::datadogV2::model::IncidentServiceResponse>(
311 &local_content,
312 ) {
313 Ok(e) => {
314 return Ok(datadog::ResponseContent {
315 status: local_status,
316 content: local_content,
317 entity: Some(e),
318 })
319 }
320 Err(e) => return Err(datadog::Error::Serde(e)),
321 };
322 } else {
323 let local_entity: Option<CreateIncidentServiceError> =
324 serde_json::from_str(&local_content).ok();
325 let local_error = datadog::ResponseContent {
326 status: local_status,
327 content: local_content,
328 entity: local_entity,
329 };
330 Err(datadog::Error::ResponseError(local_error))
331 }
332 }
333
334 pub async fn delete_incident_service(
336 &self,
337 service_id: String,
338 ) -> Result<(), datadog::Error<DeleteIncidentServiceError>> {
339 match self
340 .delete_incident_service_with_http_info(service_id)
341 .await
342 {
343 Ok(_) => Ok(()),
344 Err(err) => Err(err),
345 }
346 }
347
348 pub async fn delete_incident_service_with_http_info(
350 &self,
351 service_id: String,
352 ) -> Result<datadog::ResponseContent<()>, datadog::Error<DeleteIncidentServiceError>> {
353 let local_configuration = &self.config;
354 let operation_id = "v2.delete_incident_service";
355 if local_configuration.is_unstable_operation_enabled(operation_id) {
356 warn!("Using unstable operation {operation_id}");
357 } else {
358 let local_error = datadog::UnstableOperationDisabledError {
359 msg: "Operation 'v2.delete_incident_service' is not enabled".to_string(),
360 };
361 return Err(datadog::Error::UnstableOperationDisabledError(local_error));
362 }
363
364 let local_client = &self.client;
365
366 let local_uri_str = format!(
367 "{}/api/v2/services/{service_id}",
368 local_configuration.get_operation_host(operation_id),
369 service_id = datadog::urlencode(service_id)
370 );
371 let mut local_req_builder =
372 local_client.request(reqwest::Method::DELETE, local_uri_str.as_str());
373
374 let mut headers = HeaderMap::new();
376 headers.insert("Accept", HeaderValue::from_static("*/*"));
377
378 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
380 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
381 Err(e) => {
382 log::warn!("Failed to parse user agent header: {e}, falling back to default");
383 headers.insert(
384 reqwest::header::USER_AGENT,
385 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
386 )
387 }
388 };
389
390 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
392 headers.insert(
393 "DD-API-KEY",
394 HeaderValue::from_str(local_key.key.as_str())
395 .expect("failed to parse DD-API-KEY header"),
396 );
397 };
398 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
399 headers.insert(
400 "DD-APPLICATION-KEY",
401 HeaderValue::from_str(local_key.key.as_str())
402 .expect("failed to parse DD-APPLICATION-KEY header"),
403 );
404 };
405
406 local_req_builder = local_req_builder.headers(headers);
407 let local_req = local_req_builder.build()?;
408 log::debug!("request content: {:?}", local_req.body());
409 let local_resp = local_client.execute(local_req).await?;
410
411 let local_status = local_resp.status();
412 let local_content = local_resp.text().await?;
413 log::debug!("response content: {}", local_content);
414
415 if !local_status.is_client_error() && !local_status.is_server_error() {
416 Ok(datadog::ResponseContent {
417 status: local_status,
418 content: local_content,
419 entity: None,
420 })
421 } else {
422 let local_entity: Option<DeleteIncidentServiceError> =
423 serde_json::from_str(&local_content).ok();
424 let local_error = datadog::ResponseContent {
425 status: local_status,
426 content: local_content,
427 entity: local_entity,
428 };
429 Err(datadog::Error::ResponseError(local_error))
430 }
431 }
432
433 pub async fn get_incident_service(
436 &self,
437 service_id: String,
438 params: GetIncidentServiceOptionalParams,
439 ) -> Result<
440 crate::datadogV2::model::IncidentServiceResponse,
441 datadog::Error<GetIncidentServiceError>,
442 > {
443 match self
444 .get_incident_service_with_http_info(service_id, params)
445 .await
446 {
447 Ok(response_content) => {
448 if let Some(e) = response_content.entity {
449 Ok(e)
450 } else {
451 Err(datadog::Error::Serde(serde::de::Error::custom(
452 "response content was None",
453 )))
454 }
455 }
456 Err(err) => Err(err),
457 }
458 }
459
460 pub async fn get_incident_service_with_http_info(
463 &self,
464 service_id: String,
465 params: GetIncidentServiceOptionalParams,
466 ) -> Result<
467 datadog::ResponseContent<crate::datadogV2::model::IncidentServiceResponse>,
468 datadog::Error<GetIncidentServiceError>,
469 > {
470 let local_configuration = &self.config;
471 let operation_id = "v2.get_incident_service";
472 if local_configuration.is_unstable_operation_enabled(operation_id) {
473 warn!("Using unstable operation {operation_id}");
474 } else {
475 let local_error = datadog::UnstableOperationDisabledError {
476 msg: "Operation 'v2.get_incident_service' is not enabled".to_string(),
477 };
478 return Err(datadog::Error::UnstableOperationDisabledError(local_error));
479 }
480
481 let include = params.include;
483
484 let local_client = &self.client;
485
486 let local_uri_str = format!(
487 "{}/api/v2/services/{service_id}",
488 local_configuration.get_operation_host(operation_id),
489 service_id = datadog::urlencode(service_id)
490 );
491 let mut local_req_builder =
492 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
493
494 if let Some(ref local_query_param) = include {
495 local_req_builder =
496 local_req_builder.query(&[("include", &local_query_param.to_string())]);
497 };
498
499 let mut headers = HeaderMap::new();
501 headers.insert("Accept", HeaderValue::from_static("application/json"));
502
503 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
505 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
506 Err(e) => {
507 log::warn!("Failed to parse user agent header: {e}, falling back to default");
508 headers.insert(
509 reqwest::header::USER_AGENT,
510 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
511 )
512 }
513 };
514
515 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
517 headers.insert(
518 "DD-API-KEY",
519 HeaderValue::from_str(local_key.key.as_str())
520 .expect("failed to parse DD-API-KEY header"),
521 );
522 };
523 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
524 headers.insert(
525 "DD-APPLICATION-KEY",
526 HeaderValue::from_str(local_key.key.as_str())
527 .expect("failed to parse DD-APPLICATION-KEY header"),
528 );
529 };
530
531 local_req_builder = local_req_builder.headers(headers);
532 let local_req = local_req_builder.build()?;
533 log::debug!("request content: {:?}", local_req.body());
534 let local_resp = local_client.execute(local_req).await?;
535
536 let local_status = local_resp.status();
537 let local_content = local_resp.text().await?;
538 log::debug!("response content: {}", local_content);
539
540 if !local_status.is_client_error() && !local_status.is_server_error() {
541 match serde_json::from_str::<crate::datadogV2::model::IncidentServiceResponse>(
542 &local_content,
543 ) {
544 Ok(e) => {
545 return Ok(datadog::ResponseContent {
546 status: local_status,
547 content: local_content,
548 entity: Some(e),
549 })
550 }
551 Err(e) => return Err(datadog::Error::Serde(e)),
552 };
553 } else {
554 let local_entity: Option<GetIncidentServiceError> =
555 serde_json::from_str(&local_content).ok();
556 let local_error = datadog::ResponseContent {
557 status: local_status,
558 content: local_content,
559 entity: local_entity,
560 };
561 Err(datadog::Error::ResponseError(local_error))
562 }
563 }
564
565 pub async fn list_incident_services(
567 &self,
568 params: ListIncidentServicesOptionalParams,
569 ) -> Result<
570 crate::datadogV2::model::IncidentServicesResponse,
571 datadog::Error<ListIncidentServicesError>,
572 > {
573 match self.list_incident_services_with_http_info(params).await {
574 Ok(response_content) => {
575 if let Some(e) = response_content.entity {
576 Ok(e)
577 } else {
578 Err(datadog::Error::Serde(serde::de::Error::custom(
579 "response content was None",
580 )))
581 }
582 }
583 Err(err) => Err(err),
584 }
585 }
586
587 pub async fn list_incident_services_with_http_info(
589 &self,
590 params: ListIncidentServicesOptionalParams,
591 ) -> Result<
592 datadog::ResponseContent<crate::datadogV2::model::IncidentServicesResponse>,
593 datadog::Error<ListIncidentServicesError>,
594 > {
595 let local_configuration = &self.config;
596 let operation_id = "v2.list_incident_services";
597 if local_configuration.is_unstable_operation_enabled(operation_id) {
598 warn!("Using unstable operation {operation_id}");
599 } else {
600 let local_error = datadog::UnstableOperationDisabledError {
601 msg: "Operation 'v2.list_incident_services' is not enabled".to_string(),
602 };
603 return Err(datadog::Error::UnstableOperationDisabledError(local_error));
604 }
605
606 let include = params.include;
608 let page_size = params.page_size;
609 let page_offset = params.page_offset;
610 let filter = params.filter;
611
612 let local_client = &self.client;
613
614 let local_uri_str = format!(
615 "{}/api/v2/services",
616 local_configuration.get_operation_host(operation_id)
617 );
618 let mut local_req_builder =
619 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
620
621 if let Some(ref local_query_param) = include {
622 local_req_builder =
623 local_req_builder.query(&[("include", &local_query_param.to_string())]);
624 };
625 if let Some(ref local_query_param) = page_size {
626 local_req_builder =
627 local_req_builder.query(&[("page[size]", &local_query_param.to_string())]);
628 };
629 if let Some(ref local_query_param) = page_offset {
630 local_req_builder =
631 local_req_builder.query(&[("page[offset]", &local_query_param.to_string())]);
632 };
633 if let Some(ref local_query_param) = filter {
634 local_req_builder =
635 local_req_builder.query(&[("filter", &local_query_param.to_string())]);
636 };
637
638 let mut headers = HeaderMap::new();
640 headers.insert("Accept", HeaderValue::from_static("application/json"));
641
642 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
644 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
645 Err(e) => {
646 log::warn!("Failed to parse user agent header: {e}, falling back to default");
647 headers.insert(
648 reqwest::header::USER_AGENT,
649 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
650 )
651 }
652 };
653
654 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
656 headers.insert(
657 "DD-API-KEY",
658 HeaderValue::from_str(local_key.key.as_str())
659 .expect("failed to parse DD-API-KEY header"),
660 );
661 };
662 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
663 headers.insert(
664 "DD-APPLICATION-KEY",
665 HeaderValue::from_str(local_key.key.as_str())
666 .expect("failed to parse DD-APPLICATION-KEY header"),
667 );
668 };
669
670 local_req_builder = local_req_builder.headers(headers);
671 let local_req = local_req_builder.build()?;
672 log::debug!("request content: {:?}", local_req.body());
673 let local_resp = local_client.execute(local_req).await?;
674
675 let local_status = local_resp.status();
676 let local_content = local_resp.text().await?;
677 log::debug!("response content: {}", local_content);
678
679 if !local_status.is_client_error() && !local_status.is_server_error() {
680 match serde_json::from_str::<crate::datadogV2::model::IncidentServicesResponse>(
681 &local_content,
682 ) {
683 Ok(e) => {
684 return Ok(datadog::ResponseContent {
685 status: local_status,
686 content: local_content,
687 entity: Some(e),
688 })
689 }
690 Err(e) => return Err(datadog::Error::Serde(e)),
691 };
692 } else {
693 let local_entity: Option<ListIncidentServicesError> =
694 serde_json::from_str(&local_content).ok();
695 let local_error = datadog::ResponseContent {
696 status: local_status,
697 content: local_content,
698 entity: local_entity,
699 };
700 Err(datadog::Error::ResponseError(local_error))
701 }
702 }
703
704 pub async fn update_incident_service(
706 &self,
707 service_id: String,
708 body: crate::datadogV2::model::IncidentServiceUpdateRequest,
709 ) -> Result<
710 crate::datadogV2::model::IncidentServiceResponse,
711 datadog::Error<UpdateIncidentServiceError>,
712 > {
713 match self
714 .update_incident_service_with_http_info(service_id, body)
715 .await
716 {
717 Ok(response_content) => {
718 if let Some(e) = response_content.entity {
719 Ok(e)
720 } else {
721 Err(datadog::Error::Serde(serde::de::Error::custom(
722 "response content was None",
723 )))
724 }
725 }
726 Err(err) => Err(err),
727 }
728 }
729
730 pub async fn update_incident_service_with_http_info(
732 &self,
733 service_id: String,
734 body: crate::datadogV2::model::IncidentServiceUpdateRequest,
735 ) -> Result<
736 datadog::ResponseContent<crate::datadogV2::model::IncidentServiceResponse>,
737 datadog::Error<UpdateIncidentServiceError>,
738 > {
739 let local_configuration = &self.config;
740 let operation_id = "v2.update_incident_service";
741 if local_configuration.is_unstable_operation_enabled(operation_id) {
742 warn!("Using unstable operation {operation_id}");
743 } else {
744 let local_error = datadog::UnstableOperationDisabledError {
745 msg: "Operation 'v2.update_incident_service' is not enabled".to_string(),
746 };
747 return Err(datadog::Error::UnstableOperationDisabledError(local_error));
748 }
749
750 let local_client = &self.client;
751
752 let local_uri_str = format!(
753 "{}/api/v2/services/{service_id}",
754 local_configuration.get_operation_host(operation_id),
755 service_id = datadog::urlencode(service_id)
756 );
757 let mut local_req_builder =
758 local_client.request(reqwest::Method::PATCH, local_uri_str.as_str());
759
760 let mut headers = HeaderMap::new();
762 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
763 headers.insert("Accept", HeaderValue::from_static("application/json"));
764
765 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
767 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
768 Err(e) => {
769 log::warn!("Failed to parse user agent header: {e}, falling back to default");
770 headers.insert(
771 reqwest::header::USER_AGENT,
772 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
773 )
774 }
775 };
776
777 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
779 headers.insert(
780 "DD-API-KEY",
781 HeaderValue::from_str(local_key.key.as_str())
782 .expect("failed to parse DD-API-KEY header"),
783 );
784 };
785 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
786 headers.insert(
787 "DD-APPLICATION-KEY",
788 HeaderValue::from_str(local_key.key.as_str())
789 .expect("failed to parse DD-APPLICATION-KEY header"),
790 );
791 };
792
793 let output = Vec::new();
795 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
796 if body.serialize(&mut ser).is_ok() {
797 if let Some(content_encoding) = headers.get("Content-Encoding") {
798 match content_encoding.to_str().unwrap_or_default() {
799 "gzip" => {
800 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
801 let _ = enc.write_all(ser.into_inner().as_slice());
802 match enc.finish() {
803 Ok(buf) => {
804 local_req_builder = local_req_builder.body(buf);
805 }
806 Err(e) => return Err(datadog::Error::Io(e)),
807 }
808 }
809 "deflate" => {
810 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
811 let _ = enc.write_all(ser.into_inner().as_slice());
812 match enc.finish() {
813 Ok(buf) => {
814 local_req_builder = local_req_builder.body(buf);
815 }
816 Err(e) => return Err(datadog::Error::Io(e)),
817 }
818 }
819 "zstd1" => {
820 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
821 let _ = enc.write_all(ser.into_inner().as_slice());
822 match enc.finish() {
823 Ok(buf) => {
824 local_req_builder = local_req_builder.body(buf);
825 }
826 Err(e) => return Err(datadog::Error::Io(e)),
827 }
828 }
829 _ => {
830 local_req_builder = local_req_builder.body(ser.into_inner());
831 }
832 }
833 } else {
834 local_req_builder = local_req_builder.body(ser.into_inner());
835 }
836 }
837
838 local_req_builder = local_req_builder.headers(headers);
839 let local_req = local_req_builder.build()?;
840 log::debug!("request content: {:?}", local_req.body());
841 let local_resp = local_client.execute(local_req).await?;
842
843 let local_status = local_resp.status();
844 let local_content = local_resp.text().await?;
845 log::debug!("response content: {}", local_content);
846
847 if !local_status.is_client_error() && !local_status.is_server_error() {
848 match serde_json::from_str::<crate::datadogV2::model::IncidentServiceResponse>(
849 &local_content,
850 ) {
851 Ok(e) => {
852 return Ok(datadog::ResponseContent {
853 status: local_status,
854 content: local_content,
855 entity: Some(e),
856 })
857 }
858 Err(e) => return Err(datadog::Error::Serde(e)),
859 };
860 } else {
861 let local_entity: Option<UpdateIncidentServiceError> =
862 serde_json::from_str(&local_content).ok();
863 let local_error = datadog::ResponseContent {
864 status: local_status,
865 content: local_content,
866 entity: local_entity,
867 };
868 Err(datadog::Error::ResponseError(local_error))
869 }
870 }
871}