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 CreateRumMetricError {
17 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
18 UnknownValue(serde_json::Value),
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
23#[serde(untagged)]
24pub enum DeleteRumMetricError {
25 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
26 UnknownValue(serde_json::Value),
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
31#[serde(untagged)]
32pub enum GetRumMetricError {
33 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
34 UnknownValue(serde_json::Value),
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum ListRumMetricsError {
41 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
42 UnknownValue(serde_json::Value),
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum UpdateRumMetricError {
49 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
50 UnknownValue(serde_json::Value),
51}
52
53#[derive(Debug, Clone)]
55pub struct RumMetricsAPI {
56 config: datadog::Configuration,
57 client: reqwest_middleware::ClientWithMiddleware,
58}
59
60impl Default for RumMetricsAPI {
61 fn default() -> Self {
62 Self::with_config(datadog::Configuration::default())
63 }
64}
65
66impl RumMetricsAPI {
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_rum_metric(
121 &self,
122 body: crate::datadogV2::model::RumMetricCreateRequest,
123 ) -> Result<crate::datadogV2::model::RumMetricResponse, datadog::Error<CreateRumMetricError>>
124 {
125 match self.create_rum_metric_with_http_info(body).await {
126 Ok(response_content) => {
127 if let Some(e) = response_content.entity {
128 Ok(e)
129 } else {
130 Err(datadog::Error::Serde(serde::de::Error::custom(
131 "response content was None",
132 )))
133 }
134 }
135 Err(err) => Err(err),
136 }
137 }
138
139 pub async fn create_rum_metric_with_http_info(
142 &self,
143 body: crate::datadogV2::model::RumMetricCreateRequest,
144 ) -> Result<
145 datadog::ResponseContent<crate::datadogV2::model::RumMetricResponse>,
146 datadog::Error<CreateRumMetricError>,
147 > {
148 let local_configuration = &self.config;
149 let operation_id = "v2.create_rum_metric";
150
151 let local_client = &self.client;
152
153 let local_uri_str = format!(
154 "{}/api/v2/rum/config/metrics",
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::datadogV2::model::RumMetricResponse>(&local_content)
249 {
250 Ok(e) => {
251 return Ok(datadog::ResponseContent {
252 status: local_status,
253 content: local_content,
254 entity: Some(e),
255 })
256 }
257 Err(e) => return Err(datadog::Error::Serde(e)),
258 };
259 } else {
260 let local_entity: Option<CreateRumMetricError> =
261 serde_json::from_str(&local_content).ok();
262 let local_error = datadog::ResponseContent {
263 status: local_status,
264 content: local_content,
265 entity: local_entity,
266 };
267 Err(datadog::Error::ResponseError(local_error))
268 }
269 }
270
271 pub async fn delete_rum_metric(
273 &self,
274 metric_id: String,
275 ) -> Result<(), datadog::Error<DeleteRumMetricError>> {
276 match self.delete_rum_metric_with_http_info(metric_id).await {
277 Ok(_) => Ok(()),
278 Err(err) => Err(err),
279 }
280 }
281
282 pub async fn delete_rum_metric_with_http_info(
284 &self,
285 metric_id: String,
286 ) -> Result<datadog::ResponseContent<()>, datadog::Error<DeleteRumMetricError>> {
287 let local_configuration = &self.config;
288 let operation_id = "v2.delete_rum_metric";
289
290 let local_client = &self.client;
291
292 let local_uri_str = format!(
293 "{}/api/v2/rum/config/metrics/{metric_id}",
294 local_configuration.get_operation_host(operation_id),
295 metric_id = datadog::urlencode(metric_id)
296 );
297 let mut local_req_builder =
298 local_client.request(reqwest::Method::DELETE, local_uri_str.as_str());
299
300 let mut headers = HeaderMap::new();
302 headers.insert("Accept", HeaderValue::from_static("*/*"));
303
304 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
306 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
307 Err(e) => {
308 log::warn!("Failed to parse user agent header: {e}, falling back to default");
309 headers.insert(
310 reqwest::header::USER_AGENT,
311 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
312 )
313 }
314 };
315
316 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
318 headers.insert(
319 "DD-API-KEY",
320 HeaderValue::from_str(local_key.key.as_str())
321 .expect("failed to parse DD-API-KEY header"),
322 );
323 };
324 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
325 headers.insert(
326 "DD-APPLICATION-KEY",
327 HeaderValue::from_str(local_key.key.as_str())
328 .expect("failed to parse DD-APPLICATION-KEY header"),
329 );
330 };
331
332 local_req_builder = local_req_builder.headers(headers);
333 let local_req = local_req_builder.build()?;
334 log::debug!("request content: {:?}", local_req.body());
335 let local_resp = local_client.execute(local_req).await?;
336
337 let local_status = local_resp.status();
338 let local_content = local_resp.text().await?;
339 log::debug!("response content: {}", local_content);
340
341 if !local_status.is_client_error() && !local_status.is_server_error() {
342 Ok(datadog::ResponseContent {
343 status: local_status,
344 content: local_content,
345 entity: None,
346 })
347 } else {
348 let local_entity: Option<DeleteRumMetricError> =
349 serde_json::from_str(&local_content).ok();
350 let local_error = datadog::ResponseContent {
351 status: local_status,
352 content: local_content,
353 entity: local_entity,
354 };
355 Err(datadog::Error::ResponseError(local_error))
356 }
357 }
358
359 pub async fn get_rum_metric(
361 &self,
362 metric_id: String,
363 ) -> Result<crate::datadogV2::model::RumMetricResponse, datadog::Error<GetRumMetricError>> {
364 match self.get_rum_metric_with_http_info(metric_id).await {
365 Ok(response_content) => {
366 if let Some(e) = response_content.entity {
367 Ok(e)
368 } else {
369 Err(datadog::Error::Serde(serde::de::Error::custom(
370 "response content was None",
371 )))
372 }
373 }
374 Err(err) => Err(err),
375 }
376 }
377
378 pub async fn get_rum_metric_with_http_info(
380 &self,
381 metric_id: String,
382 ) -> Result<
383 datadog::ResponseContent<crate::datadogV2::model::RumMetricResponse>,
384 datadog::Error<GetRumMetricError>,
385 > {
386 let local_configuration = &self.config;
387 let operation_id = "v2.get_rum_metric";
388
389 let local_client = &self.client;
390
391 let local_uri_str = format!(
392 "{}/api/v2/rum/config/metrics/{metric_id}",
393 local_configuration.get_operation_host(operation_id),
394 metric_id = datadog::urlencode(metric_id)
395 );
396 let mut local_req_builder =
397 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
398
399 let mut headers = HeaderMap::new();
401 headers.insert("Accept", HeaderValue::from_static("application/json"));
402
403 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
405 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
406 Err(e) => {
407 log::warn!("Failed to parse user agent header: {e}, falling back to default");
408 headers.insert(
409 reqwest::header::USER_AGENT,
410 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
411 )
412 }
413 };
414
415 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
417 headers.insert(
418 "DD-API-KEY",
419 HeaderValue::from_str(local_key.key.as_str())
420 .expect("failed to parse DD-API-KEY header"),
421 );
422 };
423 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
424 headers.insert(
425 "DD-APPLICATION-KEY",
426 HeaderValue::from_str(local_key.key.as_str())
427 .expect("failed to parse DD-APPLICATION-KEY header"),
428 );
429 };
430
431 local_req_builder = local_req_builder.headers(headers);
432 let local_req = local_req_builder.build()?;
433 log::debug!("request content: {:?}", local_req.body());
434 let local_resp = local_client.execute(local_req).await?;
435
436 let local_status = local_resp.status();
437 let local_content = local_resp.text().await?;
438 log::debug!("response content: {}", local_content);
439
440 if !local_status.is_client_error() && !local_status.is_server_error() {
441 match serde_json::from_str::<crate::datadogV2::model::RumMetricResponse>(&local_content)
442 {
443 Ok(e) => {
444 return Ok(datadog::ResponseContent {
445 status: local_status,
446 content: local_content,
447 entity: Some(e),
448 })
449 }
450 Err(e) => return Err(datadog::Error::Serde(e)),
451 };
452 } else {
453 let local_entity: Option<GetRumMetricError> = serde_json::from_str(&local_content).ok();
454 let local_error = datadog::ResponseContent {
455 status: local_status,
456 content: local_content,
457 entity: local_entity,
458 };
459 Err(datadog::Error::ResponseError(local_error))
460 }
461 }
462
463 pub async fn list_rum_metrics(
465 &self,
466 ) -> Result<crate::datadogV2::model::RumMetricsResponse, datadog::Error<ListRumMetricsError>>
467 {
468 match self.list_rum_metrics_with_http_info().await {
469 Ok(response_content) => {
470 if let Some(e) = response_content.entity {
471 Ok(e)
472 } else {
473 Err(datadog::Error::Serde(serde::de::Error::custom(
474 "response content was None",
475 )))
476 }
477 }
478 Err(err) => Err(err),
479 }
480 }
481
482 pub async fn list_rum_metrics_with_http_info(
484 &self,
485 ) -> Result<
486 datadog::ResponseContent<crate::datadogV2::model::RumMetricsResponse>,
487 datadog::Error<ListRumMetricsError>,
488 > {
489 let local_configuration = &self.config;
490 let operation_id = "v2.list_rum_metrics";
491
492 let local_client = &self.client;
493
494 let local_uri_str = format!(
495 "{}/api/v2/rum/config/metrics",
496 local_configuration.get_operation_host(operation_id)
497 );
498 let mut local_req_builder =
499 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
500
501 let mut headers = HeaderMap::new();
503 headers.insert("Accept", HeaderValue::from_static("application/json"));
504
505 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
507 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
508 Err(e) => {
509 log::warn!("Failed to parse user agent header: {e}, falling back to default");
510 headers.insert(
511 reqwest::header::USER_AGENT,
512 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
513 )
514 }
515 };
516
517 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
519 headers.insert(
520 "DD-API-KEY",
521 HeaderValue::from_str(local_key.key.as_str())
522 .expect("failed to parse DD-API-KEY header"),
523 );
524 };
525 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
526 headers.insert(
527 "DD-APPLICATION-KEY",
528 HeaderValue::from_str(local_key.key.as_str())
529 .expect("failed to parse DD-APPLICATION-KEY header"),
530 );
531 };
532
533 local_req_builder = local_req_builder.headers(headers);
534 let local_req = local_req_builder.build()?;
535 log::debug!("request content: {:?}", local_req.body());
536 let local_resp = local_client.execute(local_req).await?;
537
538 let local_status = local_resp.status();
539 let local_content = local_resp.text().await?;
540 log::debug!("response content: {}", local_content);
541
542 if !local_status.is_client_error() && !local_status.is_server_error() {
543 match serde_json::from_str::<crate::datadogV2::model::RumMetricsResponse>(
544 &local_content,
545 ) {
546 Ok(e) => {
547 return Ok(datadog::ResponseContent {
548 status: local_status,
549 content: local_content,
550 entity: Some(e),
551 })
552 }
553 Err(e) => return Err(datadog::Error::Serde(e)),
554 };
555 } else {
556 let local_entity: Option<ListRumMetricsError> =
557 serde_json::from_str(&local_content).ok();
558 let local_error = datadog::ResponseContent {
559 status: local_status,
560 content: local_content,
561 entity: local_entity,
562 };
563 Err(datadog::Error::ResponseError(local_error))
564 }
565 }
566
567 pub async fn update_rum_metric(
570 &self,
571 metric_id: String,
572 body: crate::datadogV2::model::RumMetricUpdateRequest,
573 ) -> Result<crate::datadogV2::model::RumMetricResponse, datadog::Error<UpdateRumMetricError>>
574 {
575 match self.update_rum_metric_with_http_info(metric_id, body).await {
576 Ok(response_content) => {
577 if let Some(e) = response_content.entity {
578 Ok(e)
579 } else {
580 Err(datadog::Error::Serde(serde::de::Error::custom(
581 "response content was None",
582 )))
583 }
584 }
585 Err(err) => Err(err),
586 }
587 }
588
589 pub async fn update_rum_metric_with_http_info(
592 &self,
593 metric_id: String,
594 body: crate::datadogV2::model::RumMetricUpdateRequest,
595 ) -> Result<
596 datadog::ResponseContent<crate::datadogV2::model::RumMetricResponse>,
597 datadog::Error<UpdateRumMetricError>,
598 > {
599 let local_configuration = &self.config;
600 let operation_id = "v2.update_rum_metric";
601
602 let local_client = &self.client;
603
604 let local_uri_str = format!(
605 "{}/api/v2/rum/config/metrics/{metric_id}",
606 local_configuration.get_operation_host(operation_id),
607 metric_id = datadog::urlencode(metric_id)
608 );
609 let mut local_req_builder =
610 local_client.request(reqwest::Method::PATCH, local_uri_str.as_str());
611
612 let mut headers = HeaderMap::new();
614 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
615 headers.insert("Accept", HeaderValue::from_static("application/json"));
616
617 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
619 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
620 Err(e) => {
621 log::warn!("Failed to parse user agent header: {e}, falling back to default");
622 headers.insert(
623 reqwest::header::USER_AGENT,
624 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
625 )
626 }
627 };
628
629 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
631 headers.insert(
632 "DD-API-KEY",
633 HeaderValue::from_str(local_key.key.as_str())
634 .expect("failed to parse DD-API-KEY header"),
635 );
636 };
637 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
638 headers.insert(
639 "DD-APPLICATION-KEY",
640 HeaderValue::from_str(local_key.key.as_str())
641 .expect("failed to parse DD-APPLICATION-KEY header"),
642 );
643 };
644
645 let output = Vec::new();
647 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
648 if body.serialize(&mut ser).is_ok() {
649 if let Some(content_encoding) = headers.get("Content-Encoding") {
650 match content_encoding.to_str().unwrap_or_default() {
651 "gzip" => {
652 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
653 let _ = enc.write_all(ser.into_inner().as_slice());
654 match enc.finish() {
655 Ok(buf) => {
656 local_req_builder = local_req_builder.body(buf);
657 }
658 Err(e) => return Err(datadog::Error::Io(e)),
659 }
660 }
661 "deflate" => {
662 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
663 let _ = enc.write_all(ser.into_inner().as_slice());
664 match enc.finish() {
665 Ok(buf) => {
666 local_req_builder = local_req_builder.body(buf);
667 }
668 Err(e) => return Err(datadog::Error::Io(e)),
669 }
670 }
671 "zstd1" => {
672 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
673 let _ = enc.write_all(ser.into_inner().as_slice());
674 match enc.finish() {
675 Ok(buf) => {
676 local_req_builder = local_req_builder.body(buf);
677 }
678 Err(e) => return Err(datadog::Error::Io(e)),
679 }
680 }
681 _ => {
682 local_req_builder = local_req_builder.body(ser.into_inner());
683 }
684 }
685 } else {
686 local_req_builder = local_req_builder.body(ser.into_inner());
687 }
688 }
689
690 local_req_builder = local_req_builder.headers(headers);
691 let local_req = local_req_builder.build()?;
692 log::debug!("request content: {:?}", local_req.body());
693 let local_resp = local_client.execute(local_req).await?;
694
695 let local_status = local_resp.status();
696 let local_content = local_resp.text().await?;
697 log::debug!("response content: {}", local_content);
698
699 if !local_status.is_client_error() && !local_status.is_server_error() {
700 match serde_json::from_str::<crate::datadogV2::model::RumMetricResponse>(&local_content)
701 {
702 Ok(e) => {
703 return Ok(datadog::ResponseContent {
704 status: local_status,
705 content: local_content,
706 entity: Some(e),
707 })
708 }
709 Err(e) => return Err(datadog::Error::Serde(e)),
710 };
711 } else {
712 let local_entity: Option<UpdateRumMetricError> =
713 serde_json::from_str(&local_content).ok();
714 let local_error = datadog::ResponseContent {
715 status: local_status,
716 content: local_content,
717 entity: local_entity,
718 };
719 Err(datadog::Error::ResponseError(local_error))
720 }
721 }
722}