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 ListOrgConnectionsOptionalParams {
17 pub sink_org_id: Option<String>,
19 pub source_org_id: Option<String>,
21 pub limit: Option<i64>,
23 pub offset: Option<i64>,
25}
26
27impl ListOrgConnectionsOptionalParams {
28 pub fn sink_org_id(mut self, value: String) -> Self {
30 self.sink_org_id = Some(value);
31 self
32 }
33 pub fn source_org_id(mut self, value: String) -> Self {
35 self.source_org_id = Some(value);
36 self
37 }
38 pub fn limit(mut self, value: i64) -> Self {
40 self.limit = Some(value);
41 self
42 }
43 pub fn offset(mut self, value: i64) -> Self {
45 self.offset = Some(value);
46 self
47 }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum CreateOrgConnectionsError {
54 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
55 UnknownValue(serde_json::Value),
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(untagged)]
61pub enum DeleteOrgConnectionsError {
62 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
63 UnknownValue(serde_json::Value),
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
68#[serde(untagged)]
69pub enum ListOrgConnectionsError {
70 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
71 UnknownValue(serde_json::Value),
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(untagged)]
77pub enum UpdateOrgConnectionsError {
78 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
79 UnknownValue(serde_json::Value),
80}
81
82#[derive(Debug, Clone)]
84pub struct OrgConnectionsAPI {
85 config: datadog::Configuration,
86 client: reqwest_middleware::ClientWithMiddleware,
87}
88
89impl Default for OrgConnectionsAPI {
90 fn default() -> Self {
91 Self::with_config(datadog::Configuration::default())
92 }
93}
94
95impl OrgConnectionsAPI {
96 pub fn new() -> Self {
97 Self::default()
98 }
99 pub fn with_config(config: datadog::Configuration) -> Self {
100 let mut reqwest_client_builder = reqwest::Client::builder();
101
102 if let Some(proxy_url) = &config.proxy_url {
103 let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
104 reqwest_client_builder = reqwest_client_builder.proxy(proxy);
105 }
106
107 let mut middleware_client_builder =
108 reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
109
110 if config.enable_retry {
111 struct RetryableStatus;
112 impl reqwest_retry::RetryableStrategy for RetryableStatus {
113 fn handle(
114 &self,
115 res: &Result<reqwest::Response, reqwest_middleware::Error>,
116 ) -> Option<reqwest_retry::Retryable> {
117 match res {
118 Ok(success) => reqwest_retry::default_on_request_success(success),
119 Err(_) => None,
120 }
121 }
122 }
123 let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
124 .build_with_max_retries(config.max_retries);
125
126 let retry_middleware =
127 reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
128 backoff_policy,
129 RetryableStatus,
130 );
131
132 middleware_client_builder = middleware_client_builder.with(retry_middleware);
133 }
134
135 let client = middleware_client_builder.build();
136
137 Self { config, client }
138 }
139
140 pub fn with_client_and_config(
141 config: datadog::Configuration,
142 client: reqwest_middleware::ClientWithMiddleware,
143 ) -> Self {
144 Self { config, client }
145 }
146
147 pub async fn create_org_connections(
149 &self,
150 body: crate::datadogV2::model::OrgConnectionCreateRequest,
151 ) -> Result<
152 crate::datadogV2::model::OrgConnectionResponse,
153 datadog::Error<CreateOrgConnectionsError>,
154 > {
155 match self.create_org_connections_with_http_info(body).await {
156 Ok(response_content) => {
157 if let Some(e) = response_content.entity {
158 Ok(e)
159 } else {
160 Err(datadog::Error::Serde(serde::de::Error::custom(
161 "response content was None",
162 )))
163 }
164 }
165 Err(err) => Err(err),
166 }
167 }
168
169 pub async fn create_org_connections_with_http_info(
171 &self,
172 body: crate::datadogV2::model::OrgConnectionCreateRequest,
173 ) -> Result<
174 datadog::ResponseContent<crate::datadogV2::model::OrgConnectionResponse>,
175 datadog::Error<CreateOrgConnectionsError>,
176 > {
177 let local_configuration = &self.config;
178 let operation_id = "v2.create_org_connections";
179
180 let local_client = &self.client;
181
182 let local_uri_str = format!(
183 "{}/api/v2/org_connections",
184 local_configuration.get_operation_host(operation_id)
185 );
186 let mut local_req_builder =
187 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
188
189 let mut headers = HeaderMap::new();
191 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
192 headers.insert("Accept", HeaderValue::from_static("application/json"));
193
194 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
196 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
197 Err(e) => {
198 log::warn!("Failed to parse user agent header: {e}, falling back to default");
199 headers.insert(
200 reqwest::header::USER_AGENT,
201 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
202 )
203 }
204 };
205
206 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
208 headers.insert(
209 "DD-API-KEY",
210 HeaderValue::from_str(local_key.key.as_str())
211 .expect("failed to parse DD-API-KEY header"),
212 );
213 };
214 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
215 headers.insert(
216 "DD-APPLICATION-KEY",
217 HeaderValue::from_str(local_key.key.as_str())
218 .expect("failed to parse DD-APPLICATION-KEY header"),
219 );
220 };
221
222 let output = Vec::new();
224 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
225 if body.serialize(&mut ser).is_ok() {
226 if let Some(content_encoding) = headers.get("Content-Encoding") {
227 match content_encoding.to_str().unwrap_or_default() {
228 "gzip" => {
229 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
230 let _ = enc.write_all(ser.into_inner().as_slice());
231 match enc.finish() {
232 Ok(buf) => {
233 local_req_builder = local_req_builder.body(buf);
234 }
235 Err(e) => return Err(datadog::Error::Io(e)),
236 }
237 }
238 "deflate" => {
239 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
240 let _ = enc.write_all(ser.into_inner().as_slice());
241 match enc.finish() {
242 Ok(buf) => {
243 local_req_builder = local_req_builder.body(buf);
244 }
245 Err(e) => return Err(datadog::Error::Io(e)),
246 }
247 }
248 "zstd1" => {
249 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
250 let _ = enc.write_all(ser.into_inner().as_slice());
251 match enc.finish() {
252 Ok(buf) => {
253 local_req_builder = local_req_builder.body(buf);
254 }
255 Err(e) => return Err(datadog::Error::Io(e)),
256 }
257 }
258 _ => {
259 local_req_builder = local_req_builder.body(ser.into_inner());
260 }
261 }
262 } else {
263 local_req_builder = local_req_builder.body(ser.into_inner());
264 }
265 }
266
267 local_req_builder = local_req_builder.headers(headers);
268 let local_req = local_req_builder.build()?;
269 log::debug!("request content: {:?}", local_req.body());
270 let local_resp = local_client.execute(local_req).await?;
271
272 let local_status = local_resp.status();
273 let local_content = local_resp.text().await?;
274 log::debug!("response content: {}", local_content);
275
276 if !local_status.is_client_error() && !local_status.is_server_error() {
277 match serde_json::from_str::<crate::datadogV2::model::OrgConnectionResponse>(
278 &local_content,
279 ) {
280 Ok(e) => {
281 return Ok(datadog::ResponseContent {
282 status: local_status,
283 content: local_content,
284 entity: Some(e),
285 })
286 }
287 Err(e) => return Err(datadog::Error::Serde(e)),
288 };
289 } else {
290 let local_entity: Option<CreateOrgConnectionsError> =
291 serde_json::from_str(&local_content).ok();
292 let local_error = datadog::ResponseContent {
293 status: local_status,
294 content: local_content,
295 entity: local_entity,
296 };
297 Err(datadog::Error::ResponseError(local_error))
298 }
299 }
300
301 pub async fn delete_org_connections(
303 &self,
304 connection_id: uuid::Uuid,
305 ) -> Result<(), datadog::Error<DeleteOrgConnectionsError>> {
306 match self
307 .delete_org_connections_with_http_info(connection_id)
308 .await
309 {
310 Ok(_) => Ok(()),
311 Err(err) => Err(err),
312 }
313 }
314
315 pub async fn delete_org_connections_with_http_info(
317 &self,
318 connection_id: uuid::Uuid,
319 ) -> Result<datadog::ResponseContent<()>, datadog::Error<DeleteOrgConnectionsError>> {
320 let local_configuration = &self.config;
321 let operation_id = "v2.delete_org_connections";
322
323 let local_client = &self.client;
324
325 let local_uri_str = format!(
326 "{}/api/v2/org_connections/{connection_id}",
327 local_configuration.get_operation_host(operation_id),
328 connection_id = datadog::urlencode(connection_id.to_string())
329 );
330 let mut local_req_builder =
331 local_client.request(reqwest::Method::DELETE, local_uri_str.as_str());
332
333 let mut headers = HeaderMap::new();
335 headers.insert("Accept", HeaderValue::from_static("*/*"));
336
337 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
339 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
340 Err(e) => {
341 log::warn!("Failed to parse user agent header: {e}, falling back to default");
342 headers.insert(
343 reqwest::header::USER_AGENT,
344 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
345 )
346 }
347 };
348
349 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
351 headers.insert(
352 "DD-API-KEY",
353 HeaderValue::from_str(local_key.key.as_str())
354 .expect("failed to parse DD-API-KEY header"),
355 );
356 };
357 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
358 headers.insert(
359 "DD-APPLICATION-KEY",
360 HeaderValue::from_str(local_key.key.as_str())
361 .expect("failed to parse DD-APPLICATION-KEY header"),
362 );
363 };
364
365 local_req_builder = local_req_builder.headers(headers);
366 let local_req = local_req_builder.build()?;
367 log::debug!("request content: {:?}", local_req.body());
368 let local_resp = local_client.execute(local_req).await?;
369
370 let local_status = local_resp.status();
371 let local_content = local_resp.text().await?;
372 log::debug!("response content: {}", local_content);
373
374 if !local_status.is_client_error() && !local_status.is_server_error() {
375 Ok(datadog::ResponseContent {
376 status: local_status,
377 content: local_content,
378 entity: None,
379 })
380 } else {
381 let local_entity: Option<DeleteOrgConnectionsError> =
382 serde_json::from_str(&local_content).ok();
383 let local_error = datadog::ResponseContent {
384 status: local_status,
385 content: local_content,
386 entity: local_entity,
387 };
388 Err(datadog::Error::ResponseError(local_error))
389 }
390 }
391
392 pub async fn list_org_connections(
394 &self,
395 params: ListOrgConnectionsOptionalParams,
396 ) -> Result<
397 crate::datadogV2::model::OrgConnectionListResponse,
398 datadog::Error<ListOrgConnectionsError>,
399 > {
400 match self.list_org_connections_with_http_info(params).await {
401 Ok(response_content) => {
402 if let Some(e) = response_content.entity {
403 Ok(e)
404 } else {
405 Err(datadog::Error::Serde(serde::de::Error::custom(
406 "response content was None",
407 )))
408 }
409 }
410 Err(err) => Err(err),
411 }
412 }
413
414 pub async fn list_org_connections_with_http_info(
416 &self,
417 params: ListOrgConnectionsOptionalParams,
418 ) -> Result<
419 datadog::ResponseContent<crate::datadogV2::model::OrgConnectionListResponse>,
420 datadog::Error<ListOrgConnectionsError>,
421 > {
422 let local_configuration = &self.config;
423 let operation_id = "v2.list_org_connections";
424
425 let sink_org_id = params.sink_org_id;
427 let source_org_id = params.source_org_id;
428 let limit = params.limit;
429 let offset = params.offset;
430
431 let local_client = &self.client;
432
433 let local_uri_str = format!(
434 "{}/api/v2/org_connections",
435 local_configuration.get_operation_host(operation_id)
436 );
437 let mut local_req_builder =
438 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
439
440 if let Some(ref local_query_param) = sink_org_id {
441 local_req_builder =
442 local_req_builder.query(&[("sink_org_id", &local_query_param.to_string())]);
443 };
444 if let Some(ref local_query_param) = source_org_id {
445 local_req_builder =
446 local_req_builder.query(&[("source_org_id", &local_query_param.to_string())]);
447 };
448 if let Some(ref local_query_param) = limit {
449 local_req_builder =
450 local_req_builder.query(&[("limit", &local_query_param.to_string())]);
451 };
452 if let Some(ref local_query_param) = offset {
453 local_req_builder =
454 local_req_builder.query(&[("offset", &local_query_param.to_string())]);
455 };
456
457 let mut headers = HeaderMap::new();
459 headers.insert("Accept", HeaderValue::from_static("application/json"));
460
461 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
463 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
464 Err(e) => {
465 log::warn!("Failed to parse user agent header: {e}, falling back to default");
466 headers.insert(
467 reqwest::header::USER_AGENT,
468 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
469 )
470 }
471 };
472
473 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
475 headers.insert(
476 "DD-API-KEY",
477 HeaderValue::from_str(local_key.key.as_str())
478 .expect("failed to parse DD-API-KEY header"),
479 );
480 };
481 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
482 headers.insert(
483 "DD-APPLICATION-KEY",
484 HeaderValue::from_str(local_key.key.as_str())
485 .expect("failed to parse DD-APPLICATION-KEY header"),
486 );
487 };
488
489 local_req_builder = local_req_builder.headers(headers);
490 let local_req = local_req_builder.build()?;
491 log::debug!("request content: {:?}", local_req.body());
492 let local_resp = local_client.execute(local_req).await?;
493
494 let local_status = local_resp.status();
495 let local_content = local_resp.text().await?;
496 log::debug!("response content: {}", local_content);
497
498 if !local_status.is_client_error() && !local_status.is_server_error() {
499 match serde_json::from_str::<crate::datadogV2::model::OrgConnectionListResponse>(
500 &local_content,
501 ) {
502 Ok(e) => {
503 return Ok(datadog::ResponseContent {
504 status: local_status,
505 content: local_content,
506 entity: Some(e),
507 })
508 }
509 Err(e) => return Err(datadog::Error::Serde(e)),
510 };
511 } else {
512 let local_entity: Option<ListOrgConnectionsError> =
513 serde_json::from_str(&local_content).ok();
514 let local_error = datadog::ResponseContent {
515 status: local_status,
516 content: local_content,
517 entity: local_entity,
518 };
519 Err(datadog::Error::ResponseError(local_error))
520 }
521 }
522
523 pub async fn update_org_connections(
525 &self,
526 connection_id: uuid::Uuid,
527 body: crate::datadogV2::model::OrgConnectionUpdateRequest,
528 ) -> Result<
529 crate::datadogV2::model::OrgConnectionResponse,
530 datadog::Error<UpdateOrgConnectionsError>,
531 > {
532 match self
533 .update_org_connections_with_http_info(connection_id, body)
534 .await
535 {
536 Ok(response_content) => {
537 if let Some(e) = response_content.entity {
538 Ok(e)
539 } else {
540 Err(datadog::Error::Serde(serde::de::Error::custom(
541 "response content was None",
542 )))
543 }
544 }
545 Err(err) => Err(err),
546 }
547 }
548
549 pub async fn update_org_connections_with_http_info(
551 &self,
552 connection_id: uuid::Uuid,
553 body: crate::datadogV2::model::OrgConnectionUpdateRequest,
554 ) -> Result<
555 datadog::ResponseContent<crate::datadogV2::model::OrgConnectionResponse>,
556 datadog::Error<UpdateOrgConnectionsError>,
557 > {
558 let local_configuration = &self.config;
559 let operation_id = "v2.update_org_connections";
560
561 let local_client = &self.client;
562
563 let local_uri_str = format!(
564 "{}/api/v2/org_connections/{connection_id}",
565 local_configuration.get_operation_host(operation_id),
566 connection_id = datadog::urlencode(connection_id.to_string())
567 );
568 let mut local_req_builder =
569 local_client.request(reqwest::Method::PATCH, local_uri_str.as_str());
570
571 let mut headers = HeaderMap::new();
573 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
574 headers.insert("Accept", HeaderValue::from_static("application/json"));
575
576 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
578 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
579 Err(e) => {
580 log::warn!("Failed to parse user agent header: {e}, falling back to default");
581 headers.insert(
582 reqwest::header::USER_AGENT,
583 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
584 )
585 }
586 };
587
588 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
590 headers.insert(
591 "DD-API-KEY",
592 HeaderValue::from_str(local_key.key.as_str())
593 .expect("failed to parse DD-API-KEY header"),
594 );
595 };
596 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
597 headers.insert(
598 "DD-APPLICATION-KEY",
599 HeaderValue::from_str(local_key.key.as_str())
600 .expect("failed to parse DD-APPLICATION-KEY header"),
601 );
602 };
603
604 let output = Vec::new();
606 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
607 if body.serialize(&mut ser).is_ok() {
608 if let Some(content_encoding) = headers.get("Content-Encoding") {
609 match content_encoding.to_str().unwrap_or_default() {
610 "gzip" => {
611 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
612 let _ = enc.write_all(ser.into_inner().as_slice());
613 match enc.finish() {
614 Ok(buf) => {
615 local_req_builder = local_req_builder.body(buf);
616 }
617 Err(e) => return Err(datadog::Error::Io(e)),
618 }
619 }
620 "deflate" => {
621 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
622 let _ = enc.write_all(ser.into_inner().as_slice());
623 match enc.finish() {
624 Ok(buf) => {
625 local_req_builder = local_req_builder.body(buf);
626 }
627 Err(e) => return Err(datadog::Error::Io(e)),
628 }
629 }
630 "zstd1" => {
631 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
632 let _ = enc.write_all(ser.into_inner().as_slice());
633 match enc.finish() {
634 Ok(buf) => {
635 local_req_builder = local_req_builder.body(buf);
636 }
637 Err(e) => return Err(datadog::Error::Io(e)),
638 }
639 }
640 _ => {
641 local_req_builder = local_req_builder.body(ser.into_inner());
642 }
643 }
644 } else {
645 local_req_builder = local_req_builder.body(ser.into_inner());
646 }
647 }
648
649 local_req_builder = local_req_builder.headers(headers);
650 let local_req = local_req_builder.build()?;
651 log::debug!("request content: {:?}", local_req.body());
652 let local_resp = local_client.execute(local_req).await?;
653
654 let local_status = local_resp.status();
655 let local_content = local_resp.text().await?;
656 log::debug!("response content: {}", local_content);
657
658 if !local_status.is_client_error() && !local_status.is_server_error() {
659 match serde_json::from_str::<crate::datadogV2::model::OrgConnectionResponse>(
660 &local_content,
661 ) {
662 Ok(e) => {
663 return Ok(datadog::ResponseContent {
664 status: local_status,
665 content: local_content,
666 entity: Some(e),
667 })
668 }
669 Err(e) => return Err(datadog::Error::Serde(e)),
670 };
671 } else {
672 let local_entity: Option<UpdateOrgConnectionsError> =
673 serde_json::from_str(&local_content).ok();
674 let local_error = datadog::ResponseContent {
675 status: local_status,
676 content: local_content,
677 entity: local_entity,
678 };
679 Err(datadog::Error::ResponseError(local_error))
680 }
681 }
682}