datadog_api_client/datadogV1/api/
api_ip_ranges.rs1use crate::datadog;
5use reqwest::header::{HeaderMap, HeaderValue};
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(untagged)]
11pub enum GetIPRangesError {
12 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
13 UnknownValue(serde_json::Value),
14}
15
16#[derive(Debug, Clone)]
18pub struct IPRangesAPI {
19 config: datadog::Configuration,
20 client: reqwest_middleware::ClientWithMiddleware,
21}
22
23impl Default for IPRangesAPI {
24 fn default() -> Self {
25 Self::with_config(datadog::Configuration::default())
26 }
27}
28
29impl IPRangesAPI {
30 pub fn new() -> Self {
31 Self::default()
32 }
33 pub fn with_config(config: datadog::Configuration) -> Self {
34 let mut reqwest_client_builder = reqwest::Client::builder();
35
36 if let Some(proxy_url) = &config.proxy_url {
37 let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
38 reqwest_client_builder = reqwest_client_builder.proxy(proxy);
39 }
40
41 let mut middleware_client_builder =
42 reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
43
44 if config.enable_retry {
45 struct RetryableStatus;
46 impl reqwest_retry::RetryableStrategy for RetryableStatus {
47 fn handle(
48 &self,
49 res: &Result<reqwest::Response, reqwest_middleware::Error>,
50 ) -> Option<reqwest_retry::Retryable> {
51 match res {
52 Ok(success) => reqwest_retry::default_on_request_success(success),
53 Err(_) => None,
54 }
55 }
56 }
57 let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
58 .build_with_max_retries(config.max_retries);
59
60 let retry_middleware =
61 reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
62 backoff_policy,
63 RetryableStatus,
64 );
65
66 middleware_client_builder = middleware_client_builder.with(retry_middleware);
67 }
68
69 let client = middleware_client_builder.build();
70
71 Self { config, client }
72 }
73
74 pub fn with_client_and_config(
75 config: datadog::Configuration,
76 client: reqwest_middleware::ClientWithMiddleware,
77 ) -> Self {
78 Self { config, client }
79 }
80
81 pub async fn get_ip_ranges(
83 &self,
84 ) -> Result<crate::datadogV1::model::IPRanges, datadog::Error<GetIPRangesError>> {
85 match self.get_ip_ranges_with_http_info().await {
86 Ok(response_content) => {
87 if let Some(e) = response_content.entity {
88 Ok(e)
89 } else {
90 Err(datadog::Error::Serde(serde::de::Error::custom(
91 "response content was None",
92 )))
93 }
94 }
95 Err(err) => Err(err),
96 }
97 }
98
99 pub async fn get_ip_ranges_with_http_info(
101 &self,
102 ) -> Result<
103 datadog::ResponseContent<crate::datadogV1::model::IPRanges>,
104 datadog::Error<GetIPRangesError>,
105 > {
106 let local_configuration = &self.config;
107 let operation_id = "v1.get_ip_ranges";
108
109 let local_client = &self.client;
110
111 let local_uri_str = format!("{}/", local_configuration.get_operation_host(operation_id));
112 let mut local_req_builder =
113 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
114
115 let mut headers = HeaderMap::new();
117 headers.insert("Accept", HeaderValue::from_static("application/json"));
118
119 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
121 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
122 Err(e) => {
123 log::warn!("Failed to parse user agent header: {e}, falling back to default");
124 headers.insert(
125 reqwest::header::USER_AGENT,
126 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
127 )
128 }
129 };
130
131 local_req_builder = local_req_builder.headers(headers);
134 let local_req = local_req_builder.build()?;
135 log::debug!("request content: {:?}", local_req.body());
136 let local_resp = local_client.execute(local_req).await?;
137
138 let local_status = local_resp.status();
139 let local_content = local_resp.text().await?;
140 log::debug!("response content: {}", local_content);
141
142 if !local_status.is_client_error() && !local_status.is_server_error() {
143 match serde_json::from_str::<crate::datadogV1::model::IPRanges>(&local_content) {
144 Ok(e) => {
145 return Ok(datadog::ResponseContent {
146 status: local_status,
147 content: local_content,
148 entity: Some(e),
149 })
150 }
151 Err(e) => return Err(datadog::Error::Serde(e)),
152 };
153 } else {
154 let local_entity: Option<GetIPRangesError> = serde_json::from_str(&local_content).ok();
155 let local_error = datadog::ResponseContent {
156 status: local_status,
157 content: local_content,
158 entity: local_entity,
159 };
160 Err(datadog::Error::ResponseError(local_error))
161 }
162 }
163}