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#[non_exhaustive]
15#[derive(Clone, Default, Debug)]
16pub struct ListActiveMetricsOptionalParams {
17 pub host: Option<String>,
20 pub tag_filter: Option<String>,
23}
24
25impl ListActiveMetricsOptionalParams {
26 pub fn host(mut self, value: String) -> Self {
29 self.host = Some(value);
30 self
31 }
32 pub fn tag_filter(mut self, value: String) -> Self {
35 self.tag_filter = Some(value);
36 self
37 }
38}
39
40#[non_exhaustive]
42#[derive(Clone, Default, Debug)]
43pub struct SubmitDistributionPointsOptionalParams {
44 pub content_encoding: Option<crate::datadogV1::model::DistributionPointsContentEncoding>,
46}
47
48impl SubmitDistributionPointsOptionalParams {
49 pub fn content_encoding(
51 mut self,
52 value: crate::datadogV1::model::DistributionPointsContentEncoding,
53 ) -> Self {
54 self.content_encoding = Some(value);
55 self
56 }
57}
58
59#[non_exhaustive]
61#[derive(Clone, Default, Debug)]
62pub struct SubmitMetricsOptionalParams {
63 pub content_encoding: Option<crate::datadogV1::model::MetricContentEncoding>,
65}
66
67impl SubmitMetricsOptionalParams {
68 pub fn content_encoding(
70 mut self,
71 value: crate::datadogV1::model::MetricContentEncoding,
72 ) -> Self {
73 self.content_encoding = Some(value);
74 self
75 }
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize)]
80#[serde(untagged)]
81pub enum GetMetricMetadataError {
82 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
83 UnknownValue(serde_json::Value),
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
88#[serde(untagged)]
89pub enum ListActiveMetricsError {
90 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
91 UnknownValue(serde_json::Value),
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(untagged)]
97pub enum ListMetricsError {
98 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
99 UnknownValue(serde_json::Value),
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
104#[serde(untagged)]
105pub enum QueryMetricsError {
106 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
107 UnknownValue(serde_json::Value),
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize)]
112#[serde(untagged)]
113pub enum SubmitDistributionPointsError {
114 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
115 UnknownValue(serde_json::Value),
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
120#[serde(untagged)]
121pub enum SubmitMetricsError {
122 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
123 UnknownValue(serde_json::Value),
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
128#[serde(untagged)]
129pub enum UpdateMetricMetadataError {
130 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
131 UnknownValue(serde_json::Value),
132}
133
134#[derive(Debug, Clone)]
150pub struct MetricsAPI {
151 config: datadog::Configuration,
152 client: reqwest_middleware::ClientWithMiddleware,
153}
154
155impl Default for MetricsAPI {
156 fn default() -> Self {
157 Self::with_config(datadog::Configuration::default())
158 }
159}
160
161impl MetricsAPI {
162 pub fn new() -> Self {
163 Self::default()
164 }
165 pub fn with_config(config: datadog::Configuration) -> Self {
166 let mut reqwest_client_builder = reqwest::Client::builder();
167
168 if let Some(proxy_url) = &config.proxy_url {
169 let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
170 reqwest_client_builder = reqwest_client_builder.proxy(proxy);
171 }
172
173 let mut middleware_client_builder =
174 reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
175
176 if config.enable_retry {
177 struct RetryableStatus;
178 impl reqwest_retry::RetryableStrategy for RetryableStatus {
179 fn handle(
180 &self,
181 res: &Result<reqwest::Response, reqwest_middleware::Error>,
182 ) -> Option<reqwest_retry::Retryable> {
183 match res {
184 Ok(success) => reqwest_retry::default_on_request_success(success),
185 Err(_) => None,
186 }
187 }
188 }
189 let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
190 .build_with_max_retries(config.max_retries);
191
192 let retry_middleware =
193 reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
194 backoff_policy,
195 RetryableStatus,
196 );
197
198 middleware_client_builder = middleware_client_builder.with(retry_middleware);
199 }
200
201 let client = middleware_client_builder.build();
202
203 Self { config, client }
204 }
205
206 pub fn with_client_and_config(
207 config: datadog::Configuration,
208 client: reqwest_middleware::ClientWithMiddleware,
209 ) -> Self {
210 Self { config, client }
211 }
212
213 pub async fn get_metric_metadata(
215 &self,
216 metric_name: String,
217 ) -> Result<crate::datadogV1::model::MetricMetadata, datadog::Error<GetMetricMetadataError>>
218 {
219 match self.get_metric_metadata_with_http_info(metric_name).await {
220 Ok(response_content) => {
221 if let Some(e) = response_content.entity {
222 Ok(e)
223 } else {
224 Err(datadog::Error::Serde(serde::de::Error::custom(
225 "response content was None",
226 )))
227 }
228 }
229 Err(err) => Err(err),
230 }
231 }
232
233 pub async fn get_metric_metadata_with_http_info(
235 &self,
236 metric_name: String,
237 ) -> Result<
238 datadog::ResponseContent<crate::datadogV1::model::MetricMetadata>,
239 datadog::Error<GetMetricMetadataError>,
240 > {
241 let local_configuration = &self.config;
242 let operation_id = "v1.get_metric_metadata";
243
244 let local_client = &self.client;
245
246 let local_uri_str = format!(
247 "{}/api/v1/metrics/{metric_name}",
248 local_configuration.get_operation_host(operation_id),
249 metric_name = datadog::urlencode(metric_name)
250 );
251 let mut local_req_builder =
252 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
253
254 let mut headers = HeaderMap::new();
256 headers.insert("Accept", HeaderValue::from_static("application/json"));
257
258 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
260 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
261 Err(e) => {
262 log::warn!("Failed to parse user agent header: {e}, falling back to default");
263 headers.insert(
264 reqwest::header::USER_AGENT,
265 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
266 )
267 }
268 };
269
270 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
272 headers.insert(
273 "DD-API-KEY",
274 HeaderValue::from_str(local_key.key.as_str())
275 .expect("failed to parse DD-API-KEY header"),
276 );
277 };
278 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
279 headers.insert(
280 "DD-APPLICATION-KEY",
281 HeaderValue::from_str(local_key.key.as_str())
282 .expect("failed to parse DD-APPLICATION-KEY header"),
283 );
284 };
285
286 local_req_builder = local_req_builder.headers(headers);
287 let local_req = local_req_builder.build()?;
288 log::debug!("request content: {:?}", local_req.body());
289 let local_resp = local_client.execute(local_req).await?;
290
291 let local_status = local_resp.status();
292 let local_content = local_resp.text().await?;
293 log::debug!("response content: {}", local_content);
294
295 if !local_status.is_client_error() && !local_status.is_server_error() {
296 match serde_json::from_str::<crate::datadogV1::model::MetricMetadata>(&local_content) {
297 Ok(e) => {
298 return Ok(datadog::ResponseContent {
299 status: local_status,
300 content: local_content,
301 entity: Some(e),
302 })
303 }
304 Err(e) => return Err(datadog::Error::Serde(e)),
305 };
306 } else {
307 let local_entity: Option<GetMetricMetadataError> =
308 serde_json::from_str(&local_content).ok();
309 let local_error = datadog::ResponseContent {
310 status: local_status,
311 content: local_content,
312 entity: local_entity,
313 };
314 Err(datadog::Error::ResponseError(local_error))
315 }
316 }
317
318 pub async fn list_active_metrics(
320 &self,
321 from: i64,
322 params: ListActiveMetricsOptionalParams,
323 ) -> Result<crate::datadogV1::model::MetricsListResponse, datadog::Error<ListActiveMetricsError>>
324 {
325 match self.list_active_metrics_with_http_info(from, params).await {
326 Ok(response_content) => {
327 if let Some(e) = response_content.entity {
328 Ok(e)
329 } else {
330 Err(datadog::Error::Serde(serde::de::Error::custom(
331 "response content was None",
332 )))
333 }
334 }
335 Err(err) => Err(err),
336 }
337 }
338
339 pub async fn list_active_metrics_with_http_info(
341 &self,
342 from: i64,
343 params: ListActiveMetricsOptionalParams,
344 ) -> Result<
345 datadog::ResponseContent<crate::datadogV1::model::MetricsListResponse>,
346 datadog::Error<ListActiveMetricsError>,
347 > {
348 let local_configuration = &self.config;
349 let operation_id = "v1.list_active_metrics";
350
351 let host = params.host;
353 let tag_filter = params.tag_filter;
354
355 let local_client = &self.client;
356
357 let local_uri_str = format!(
358 "{}/api/v1/metrics",
359 local_configuration.get_operation_host(operation_id)
360 );
361 let mut local_req_builder =
362 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
363
364 local_req_builder = local_req_builder.query(&[("from", &from.to_string())]);
365 if let Some(ref local_query_param) = host {
366 local_req_builder =
367 local_req_builder.query(&[("host", &local_query_param.to_string())]);
368 };
369 if let Some(ref local_query_param) = tag_filter {
370 local_req_builder =
371 local_req_builder.query(&[("tag_filter", &local_query_param.to_string())]);
372 };
373
374 let mut headers = HeaderMap::new();
376 headers.insert("Accept", HeaderValue::from_static("application/json"));
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 match serde_json::from_str::<crate::datadogV1::model::MetricsListResponse>(
417 &local_content,
418 ) {
419 Ok(e) => {
420 return Ok(datadog::ResponseContent {
421 status: local_status,
422 content: local_content,
423 entity: Some(e),
424 })
425 }
426 Err(e) => return Err(datadog::Error::Serde(e)),
427 };
428 } else {
429 let local_entity: Option<ListActiveMetricsError> =
430 serde_json::from_str(&local_content).ok();
431 let local_error = datadog::ResponseContent {
432 status: local_status,
433 content: local_content,
434 entity: local_entity,
435 };
436 Err(datadog::Error::ResponseError(local_error))
437 }
438 }
439
440 pub async fn list_metrics(
442 &self,
443 q: String,
444 ) -> Result<crate::datadogV1::model::MetricSearchResponse, datadog::Error<ListMetricsError>>
445 {
446 match self.list_metrics_with_http_info(q).await {
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 list_metrics_with_http_info(
462 &self,
463 q: String,
464 ) -> Result<
465 datadog::ResponseContent<crate::datadogV1::model::MetricSearchResponse>,
466 datadog::Error<ListMetricsError>,
467 > {
468 let local_configuration = &self.config;
469 let operation_id = "v1.list_metrics";
470
471 let local_client = &self.client;
472
473 let local_uri_str = format!(
474 "{}/api/v1/search",
475 local_configuration.get_operation_host(operation_id)
476 );
477 let mut local_req_builder =
478 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
479
480 local_req_builder = local_req_builder.query(&[("q", &q.to_string())]);
481
482 let mut headers = HeaderMap::new();
484 headers.insert("Accept", HeaderValue::from_static("application/json"));
485
486 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
488 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
489 Err(e) => {
490 log::warn!("Failed to parse user agent header: {e}, falling back to default");
491 headers.insert(
492 reqwest::header::USER_AGENT,
493 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
494 )
495 }
496 };
497
498 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
500 headers.insert(
501 "DD-API-KEY",
502 HeaderValue::from_str(local_key.key.as_str())
503 .expect("failed to parse DD-API-KEY header"),
504 );
505 };
506 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
507 headers.insert(
508 "DD-APPLICATION-KEY",
509 HeaderValue::from_str(local_key.key.as_str())
510 .expect("failed to parse DD-APPLICATION-KEY header"),
511 );
512 };
513
514 local_req_builder = local_req_builder.headers(headers);
515 let local_req = local_req_builder.build()?;
516 log::debug!("request content: {:?}", local_req.body());
517 let local_resp = local_client.execute(local_req).await?;
518
519 let local_status = local_resp.status();
520 let local_content = local_resp.text().await?;
521 log::debug!("response content: {}", local_content);
522
523 if !local_status.is_client_error() && !local_status.is_server_error() {
524 match serde_json::from_str::<crate::datadogV1::model::MetricSearchResponse>(
525 &local_content,
526 ) {
527 Ok(e) => {
528 return Ok(datadog::ResponseContent {
529 status: local_status,
530 content: local_content,
531 entity: Some(e),
532 })
533 }
534 Err(e) => return Err(datadog::Error::Serde(e)),
535 };
536 } else {
537 let local_entity: Option<ListMetricsError> = serde_json::from_str(&local_content).ok();
538 let local_error = datadog::ResponseContent {
539 status: local_status,
540 content: local_content,
541 entity: local_entity,
542 };
543 Err(datadog::Error::ResponseError(local_error))
544 }
545 }
546
547 pub async fn query_metrics(
549 &self,
550 from: i64,
551 to: i64,
552 query: String,
553 ) -> Result<crate::datadogV1::model::MetricsQueryResponse, datadog::Error<QueryMetricsError>>
554 {
555 match self.query_metrics_with_http_info(from, to, query).await {
556 Ok(response_content) => {
557 if let Some(e) = response_content.entity {
558 Ok(e)
559 } else {
560 Err(datadog::Error::Serde(serde::de::Error::custom(
561 "response content was None",
562 )))
563 }
564 }
565 Err(err) => Err(err),
566 }
567 }
568
569 pub async fn query_metrics_with_http_info(
571 &self,
572 from: i64,
573 to: i64,
574 query: String,
575 ) -> Result<
576 datadog::ResponseContent<crate::datadogV1::model::MetricsQueryResponse>,
577 datadog::Error<QueryMetricsError>,
578 > {
579 let local_configuration = &self.config;
580 let operation_id = "v1.query_metrics";
581
582 let local_client = &self.client;
583
584 let local_uri_str = format!(
585 "{}/api/v1/query",
586 local_configuration.get_operation_host(operation_id)
587 );
588 let mut local_req_builder =
589 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
590
591 local_req_builder = local_req_builder.query(&[("from", &from.to_string())]);
592 local_req_builder = local_req_builder.query(&[("to", &to.to_string())]);
593 local_req_builder = local_req_builder.query(&[("query", &query.to_string())]);
594
595 let mut headers = HeaderMap::new();
597 headers.insert("Accept", HeaderValue::from_static("application/json"));
598
599 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
601 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
602 Err(e) => {
603 log::warn!("Failed to parse user agent header: {e}, falling back to default");
604 headers.insert(
605 reqwest::header::USER_AGENT,
606 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
607 )
608 }
609 };
610
611 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
613 headers.insert(
614 "DD-API-KEY",
615 HeaderValue::from_str(local_key.key.as_str())
616 .expect("failed to parse DD-API-KEY header"),
617 );
618 };
619 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
620 headers.insert(
621 "DD-APPLICATION-KEY",
622 HeaderValue::from_str(local_key.key.as_str())
623 .expect("failed to parse DD-APPLICATION-KEY header"),
624 );
625 };
626
627 local_req_builder = local_req_builder.headers(headers);
628 let local_req = local_req_builder.build()?;
629 log::debug!("request content: {:?}", local_req.body());
630 let local_resp = local_client.execute(local_req).await?;
631
632 let local_status = local_resp.status();
633 let local_content = local_resp.text().await?;
634 log::debug!("response content: {}", local_content);
635
636 if !local_status.is_client_error() && !local_status.is_server_error() {
637 match serde_json::from_str::<crate::datadogV1::model::MetricsQueryResponse>(
638 &local_content,
639 ) {
640 Ok(e) => {
641 return Ok(datadog::ResponseContent {
642 status: local_status,
643 content: local_content,
644 entity: Some(e),
645 })
646 }
647 Err(e) => return Err(datadog::Error::Serde(e)),
648 };
649 } else {
650 let local_entity: Option<QueryMetricsError> = serde_json::from_str(&local_content).ok();
651 let local_error = datadog::ResponseContent {
652 status: local_status,
653 content: local_content,
654 entity: local_entity,
655 };
656 Err(datadog::Error::ResponseError(local_error))
657 }
658 }
659
660 pub async fn submit_distribution_points(
662 &self,
663 body: crate::datadogV1::model::DistributionPointsPayload,
664 params: SubmitDistributionPointsOptionalParams,
665 ) -> Result<
666 crate::datadogV1::model::IntakePayloadAccepted,
667 datadog::Error<SubmitDistributionPointsError>,
668 > {
669 match self
670 .submit_distribution_points_with_http_info(body, params)
671 .await
672 {
673 Ok(response_content) => {
674 if let Some(e) = response_content.entity {
675 Ok(e)
676 } else {
677 Err(datadog::Error::Serde(serde::de::Error::custom(
678 "response content was None",
679 )))
680 }
681 }
682 Err(err) => Err(err),
683 }
684 }
685
686 pub async fn submit_distribution_points_with_http_info(
688 &self,
689 body: crate::datadogV1::model::DistributionPointsPayload,
690 params: SubmitDistributionPointsOptionalParams,
691 ) -> Result<
692 datadog::ResponseContent<crate::datadogV1::model::IntakePayloadAccepted>,
693 datadog::Error<SubmitDistributionPointsError>,
694 > {
695 let local_configuration = &self.config;
696 let operation_id = "v1.submit_distribution_points";
697
698 let content_encoding = params.content_encoding;
700
701 let local_client = &self.client;
702
703 let local_uri_str = format!(
704 "{}/api/v1/distribution_points",
705 local_configuration.get_operation_host(operation_id)
706 );
707 let mut local_req_builder =
708 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
709
710 let mut headers = HeaderMap::new();
712 headers.insert("Content-Type", HeaderValue::from_static("text/json"));
713 headers.insert("Accept", HeaderValue::from_static("application/json"));
714
715 if let Some(ref local) = content_encoding {
716 headers.insert(
717 "Content-Encoding",
718 local
719 .to_string()
720 .parse()
721 .expect("failed to parse Content-Encoding header"),
722 );
723 }
724
725 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
727 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
728 Err(e) => {
729 log::warn!("Failed to parse user agent header: {e}, falling back to default");
730 headers.insert(
731 reqwest::header::USER_AGENT,
732 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
733 )
734 }
735 };
736
737 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
739 headers.insert(
740 "DD-API-KEY",
741 HeaderValue::from_str(local_key.key.as_str())
742 .expect("failed to parse DD-API-KEY header"),
743 );
744 };
745
746 let output = Vec::new();
748 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
749 if body.serialize(&mut ser).is_ok() {
750 if let Some(content_encoding) = headers.get("Content-Encoding") {
751 match content_encoding.to_str().unwrap_or_default() {
752 "gzip" => {
753 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
754 let _ = enc.write_all(ser.into_inner().as_slice());
755 match enc.finish() {
756 Ok(buf) => {
757 local_req_builder = local_req_builder.body(buf);
758 }
759 Err(e) => return Err(datadog::Error::Io(e)),
760 }
761 }
762 "deflate" => {
763 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
764 let _ = enc.write_all(ser.into_inner().as_slice());
765 match enc.finish() {
766 Ok(buf) => {
767 local_req_builder = local_req_builder.body(buf);
768 }
769 Err(e) => return Err(datadog::Error::Io(e)),
770 }
771 }
772 "zstd1" => {
773 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
774 let _ = enc.write_all(ser.into_inner().as_slice());
775 match enc.finish() {
776 Ok(buf) => {
777 local_req_builder = local_req_builder.body(buf);
778 }
779 Err(e) => return Err(datadog::Error::Io(e)),
780 }
781 }
782 _ => {
783 local_req_builder = local_req_builder.body(ser.into_inner());
784 }
785 }
786 } else {
787 local_req_builder = local_req_builder.body(ser.into_inner());
788 }
789 }
790
791 local_req_builder = local_req_builder.headers(headers);
792 let local_req = local_req_builder.build()?;
793 log::debug!("request content: {:?}", local_req.body());
794 let local_resp = local_client.execute(local_req).await?;
795
796 let local_status = local_resp.status();
797 let local_content = local_resp.text().await?;
798 log::debug!("response content: {}", local_content);
799
800 if !local_status.is_client_error() && !local_status.is_server_error() {
801 match serde_json::from_str::<crate::datadogV1::model::IntakePayloadAccepted>(
802 &local_content,
803 ) {
804 Ok(e) => {
805 return Ok(datadog::ResponseContent {
806 status: local_status,
807 content: local_content,
808 entity: Some(e),
809 })
810 }
811 Err(e) => return Err(datadog::Error::Serde(e)),
812 };
813 } else {
814 let local_entity: Option<SubmitDistributionPointsError> =
815 serde_json::from_str(&local_content).ok();
816 let local_error = datadog::ResponseContent {
817 status: local_status,
818 content: local_content,
819 entity: local_entity,
820 };
821 Err(datadog::Error::ResponseError(local_error))
822 }
823 }
824
825 pub async fn submit_metrics(
837 &self,
838 body: crate::datadogV1::model::MetricsPayload,
839 params: SubmitMetricsOptionalParams,
840 ) -> Result<crate::datadogV1::model::IntakePayloadAccepted, datadog::Error<SubmitMetricsError>>
841 {
842 match self.submit_metrics_with_http_info(body, params).await {
843 Ok(response_content) => {
844 if let Some(e) = response_content.entity {
845 Ok(e)
846 } else {
847 Err(datadog::Error::Serde(serde::de::Error::custom(
848 "response content was None",
849 )))
850 }
851 }
852 Err(err) => Err(err),
853 }
854 }
855
856 pub async fn submit_metrics_with_http_info(
868 &self,
869 body: crate::datadogV1::model::MetricsPayload,
870 params: SubmitMetricsOptionalParams,
871 ) -> Result<
872 datadog::ResponseContent<crate::datadogV1::model::IntakePayloadAccepted>,
873 datadog::Error<SubmitMetricsError>,
874 > {
875 let local_configuration = &self.config;
876 let operation_id = "v1.submit_metrics";
877
878 let content_encoding = params.content_encoding;
880
881 let local_client = &self.client;
882
883 let local_uri_str = format!(
884 "{}/api/v1/series",
885 local_configuration.get_operation_host(operation_id)
886 );
887 let mut local_req_builder =
888 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
889
890 let mut headers = HeaderMap::new();
892 headers.insert("Content-Type", HeaderValue::from_static("text/json"));
893 headers.insert("Accept", HeaderValue::from_static("application/json"));
894
895 if let Some(ref local) = content_encoding {
896 headers.insert(
897 "Content-Encoding",
898 local
899 .to_string()
900 .parse()
901 .expect("failed to parse Content-Encoding header"),
902 );
903 }
904
905 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
907 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
908 Err(e) => {
909 log::warn!("Failed to parse user agent header: {e}, falling back to default");
910 headers.insert(
911 reqwest::header::USER_AGENT,
912 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
913 )
914 }
915 };
916
917 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
919 headers.insert(
920 "DD-API-KEY",
921 HeaderValue::from_str(local_key.key.as_str())
922 .expect("failed to parse DD-API-KEY header"),
923 );
924 };
925
926 let output = Vec::new();
928 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
929 if body.serialize(&mut ser).is_ok() {
930 if let Some(content_encoding) = headers.get("Content-Encoding") {
931 match content_encoding.to_str().unwrap_or_default() {
932 "gzip" => {
933 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
934 let _ = enc.write_all(ser.into_inner().as_slice());
935 match enc.finish() {
936 Ok(buf) => {
937 local_req_builder = local_req_builder.body(buf);
938 }
939 Err(e) => return Err(datadog::Error::Io(e)),
940 }
941 }
942 "deflate" => {
943 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
944 let _ = enc.write_all(ser.into_inner().as_slice());
945 match enc.finish() {
946 Ok(buf) => {
947 local_req_builder = local_req_builder.body(buf);
948 }
949 Err(e) => return Err(datadog::Error::Io(e)),
950 }
951 }
952 "zstd1" => {
953 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
954 let _ = enc.write_all(ser.into_inner().as_slice());
955 match enc.finish() {
956 Ok(buf) => {
957 local_req_builder = local_req_builder.body(buf);
958 }
959 Err(e) => return Err(datadog::Error::Io(e)),
960 }
961 }
962 _ => {
963 local_req_builder = local_req_builder.body(ser.into_inner());
964 }
965 }
966 } else {
967 local_req_builder = local_req_builder.body(ser.into_inner());
968 }
969 }
970
971 local_req_builder = local_req_builder.headers(headers);
972 let local_req = local_req_builder.build()?;
973 log::debug!("request content: {:?}", local_req.body());
974 let local_resp = local_client.execute(local_req).await?;
975
976 let local_status = local_resp.status();
977 let local_content = local_resp.text().await?;
978 log::debug!("response content: {}", local_content);
979
980 if !local_status.is_client_error() && !local_status.is_server_error() {
981 match serde_json::from_str::<crate::datadogV1::model::IntakePayloadAccepted>(
982 &local_content,
983 ) {
984 Ok(e) => {
985 return Ok(datadog::ResponseContent {
986 status: local_status,
987 content: local_content,
988 entity: Some(e),
989 })
990 }
991 Err(e) => return Err(datadog::Error::Serde(e)),
992 };
993 } else {
994 let local_entity: Option<SubmitMetricsError> =
995 serde_json::from_str(&local_content).ok();
996 let local_error = datadog::ResponseContent {
997 status: local_status,
998 content: local_content,
999 entity: local_entity,
1000 };
1001 Err(datadog::Error::ResponseError(local_error))
1002 }
1003 }
1004
1005 pub async fn update_metric_metadata(
1007 &self,
1008 metric_name: String,
1009 body: crate::datadogV1::model::MetricMetadata,
1010 ) -> Result<crate::datadogV1::model::MetricMetadata, datadog::Error<UpdateMetricMetadataError>>
1011 {
1012 match self
1013 .update_metric_metadata_with_http_info(metric_name, body)
1014 .await
1015 {
1016 Ok(response_content) => {
1017 if let Some(e) = response_content.entity {
1018 Ok(e)
1019 } else {
1020 Err(datadog::Error::Serde(serde::de::Error::custom(
1021 "response content was None",
1022 )))
1023 }
1024 }
1025 Err(err) => Err(err),
1026 }
1027 }
1028
1029 pub async fn update_metric_metadata_with_http_info(
1031 &self,
1032 metric_name: String,
1033 body: crate::datadogV1::model::MetricMetadata,
1034 ) -> Result<
1035 datadog::ResponseContent<crate::datadogV1::model::MetricMetadata>,
1036 datadog::Error<UpdateMetricMetadataError>,
1037 > {
1038 let local_configuration = &self.config;
1039 let operation_id = "v1.update_metric_metadata";
1040
1041 let local_client = &self.client;
1042
1043 let local_uri_str = format!(
1044 "{}/api/v1/metrics/{metric_name}",
1045 local_configuration.get_operation_host(operation_id),
1046 metric_name = datadog::urlencode(metric_name)
1047 );
1048 let mut local_req_builder =
1049 local_client.request(reqwest::Method::PUT, local_uri_str.as_str());
1050
1051 let mut headers = HeaderMap::new();
1053 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
1054 headers.insert("Accept", HeaderValue::from_static("application/json"));
1055
1056 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
1058 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
1059 Err(e) => {
1060 log::warn!("Failed to parse user agent header: {e}, falling back to default");
1061 headers.insert(
1062 reqwest::header::USER_AGENT,
1063 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
1064 )
1065 }
1066 };
1067
1068 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
1070 headers.insert(
1071 "DD-API-KEY",
1072 HeaderValue::from_str(local_key.key.as_str())
1073 .expect("failed to parse DD-API-KEY header"),
1074 );
1075 };
1076 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
1077 headers.insert(
1078 "DD-APPLICATION-KEY",
1079 HeaderValue::from_str(local_key.key.as_str())
1080 .expect("failed to parse DD-APPLICATION-KEY header"),
1081 );
1082 };
1083
1084 let output = Vec::new();
1086 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
1087 if body.serialize(&mut ser).is_ok() {
1088 if let Some(content_encoding) = headers.get("Content-Encoding") {
1089 match content_encoding.to_str().unwrap_or_default() {
1090 "gzip" => {
1091 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
1092 let _ = enc.write_all(ser.into_inner().as_slice());
1093 match enc.finish() {
1094 Ok(buf) => {
1095 local_req_builder = local_req_builder.body(buf);
1096 }
1097 Err(e) => return Err(datadog::Error::Io(e)),
1098 }
1099 }
1100 "deflate" => {
1101 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
1102 let _ = enc.write_all(ser.into_inner().as_slice());
1103 match enc.finish() {
1104 Ok(buf) => {
1105 local_req_builder = local_req_builder.body(buf);
1106 }
1107 Err(e) => return Err(datadog::Error::Io(e)),
1108 }
1109 }
1110 "zstd1" => {
1111 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
1112 let _ = enc.write_all(ser.into_inner().as_slice());
1113 match enc.finish() {
1114 Ok(buf) => {
1115 local_req_builder = local_req_builder.body(buf);
1116 }
1117 Err(e) => return Err(datadog::Error::Io(e)),
1118 }
1119 }
1120 _ => {
1121 local_req_builder = local_req_builder.body(ser.into_inner());
1122 }
1123 }
1124 } else {
1125 local_req_builder = local_req_builder.body(ser.into_inner());
1126 }
1127 }
1128
1129 local_req_builder = local_req_builder.headers(headers);
1130 let local_req = local_req_builder.build()?;
1131 log::debug!("request content: {:?}", local_req.body());
1132 let local_resp = local_client.execute(local_req).await?;
1133
1134 let local_status = local_resp.status();
1135 let local_content = local_resp.text().await?;
1136 log::debug!("response content: {}", local_content);
1137
1138 if !local_status.is_client_error() && !local_status.is_server_error() {
1139 match serde_json::from_str::<crate::datadogV1::model::MetricMetadata>(&local_content) {
1140 Ok(e) => {
1141 return Ok(datadog::ResponseContent {
1142 status: local_status,
1143 content: local_content,
1144 entity: Some(e),
1145 })
1146 }
1147 Err(e) => return Err(datadog::Error::Serde(e)),
1148 };
1149 } else {
1150 let local_entity: Option<UpdateMetricMetadataError> =
1151 serde_json::from_str(&local_content).ok();
1152 let local_error = datadog::ResponseContent {
1153 status: local_status,
1154 content: local_content,
1155 entity: local_entity,
1156 };
1157 Err(datadog::Error::ResponseError(local_error))
1158 }
1159 }
1160}