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