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 AcknowledgeOnCallPageError {
17 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
18 UnknownValue(serde_json::Value),
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
23#[serde(untagged)]
24pub enum CreateOnCallPageError {
25 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
26 UnknownValue(serde_json::Value),
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
31#[serde(untagged)]
32pub enum EscalateOnCallPageError {
33 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
34 UnknownValue(serde_json::Value),
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum ResolveOnCallPageError {
41 APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
42 UnknownValue(serde_json::Value),
43}
44
45#[derive(Debug, Clone)]
48pub struct OnCallPagingAPI {
49 config: datadog::Configuration,
50 client: reqwest_middleware::ClientWithMiddleware,
51}
52
53impl Default for OnCallPagingAPI {
54 fn default() -> Self {
55 Self::with_config(datadog::Configuration::default())
56 }
57}
58
59impl OnCallPagingAPI {
60 pub fn new() -> Self {
61 Self::default()
62 }
63 pub fn with_config(config: datadog::Configuration) -> Self {
64 let mut reqwest_client_builder = reqwest::Client::builder();
65
66 if let Some(proxy_url) = &config.proxy_url {
67 let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
68 reqwest_client_builder = reqwest_client_builder.proxy(proxy);
69 }
70
71 let mut middleware_client_builder =
72 reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
73
74 if config.enable_retry {
75 struct RetryableStatus;
76 impl reqwest_retry::RetryableStrategy for RetryableStatus {
77 fn handle(
78 &self,
79 res: &Result<reqwest::Response, reqwest_middleware::Error>,
80 ) -> Option<reqwest_retry::Retryable> {
81 match res {
82 Ok(success) => reqwest_retry::default_on_request_success(success),
83 Err(_) => None,
84 }
85 }
86 }
87 let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
88 .build_with_max_retries(config.max_retries);
89
90 let retry_middleware =
91 reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
92 backoff_policy,
93 RetryableStatus,
94 );
95
96 middleware_client_builder = middleware_client_builder.with(retry_middleware);
97 }
98
99 let client = middleware_client_builder.build();
100
101 Self { config, client }
102 }
103
104 pub fn with_client_and_config(
105 config: datadog::Configuration,
106 client: reqwest_middleware::ClientWithMiddleware,
107 ) -> Self {
108 Self { config, client }
109 }
110
111 pub async fn acknowledge_on_call_page(
113 &self,
114 page_id: uuid::Uuid,
115 ) -> Result<(), datadog::Error<AcknowledgeOnCallPageError>> {
116 match self.acknowledge_on_call_page_with_http_info(page_id).await {
117 Ok(_) => Ok(()),
118 Err(err) => Err(err),
119 }
120 }
121
122 pub async fn acknowledge_on_call_page_with_http_info(
124 &self,
125 page_id: uuid::Uuid,
126 ) -> Result<datadog::ResponseContent<()>, datadog::Error<AcknowledgeOnCallPageError>> {
127 let local_configuration = &self.config;
128 let operation_id = "v2.acknowledge_on_call_page";
129
130 let local_client = &self.client;
131
132 let local_uri_str = format!(
133 "{}/api/v2/on-call/pages/{page_id}/acknowledge",
134 local_configuration.get_operation_host(operation_id),
135 page_id = datadog::urlencode(page_id.to_string())
136 );
137 let mut local_req_builder =
138 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
139
140 let mut headers = HeaderMap::new();
142 headers.insert("Accept", HeaderValue::from_static("*/*"));
143
144 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
146 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
147 Err(e) => {
148 log::warn!("Failed to parse user agent header: {e}, falling back to default");
149 headers.insert(
150 reqwest::header::USER_AGENT,
151 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
152 )
153 }
154 };
155
156 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
158 headers.insert(
159 "DD-API-KEY",
160 HeaderValue::from_str(local_key.key.as_str())
161 .expect("failed to parse DD-API-KEY header"),
162 );
163 };
164 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
165 headers.insert(
166 "DD-APPLICATION-KEY",
167 HeaderValue::from_str(local_key.key.as_str())
168 .expect("failed to parse DD-APPLICATION-KEY header"),
169 );
170 };
171
172 local_req_builder = local_req_builder.headers(headers);
173 let local_req = local_req_builder.build()?;
174 log::debug!("request content: {:?}", local_req.body());
175 let local_resp = local_client.execute(local_req).await?;
176
177 let local_status = local_resp.status();
178 let local_content = local_resp.text().await?;
179 log::debug!("response content: {}", local_content);
180
181 if !local_status.is_client_error() && !local_status.is_server_error() {
182 Ok(datadog::ResponseContent {
183 status: local_status,
184 content: local_content,
185 entity: None,
186 })
187 } else {
188 let local_entity: Option<AcknowledgeOnCallPageError> =
189 serde_json::from_str(&local_content).ok();
190 let local_error = datadog::ResponseContent {
191 status: local_status,
192 content: local_content,
193 entity: local_entity,
194 };
195 Err(datadog::Error::ResponseError(local_error))
196 }
197 }
198
199 pub async fn create_on_call_page(
201 &self,
202 body: crate::datadogV2::model::CreatePageRequest,
203 ) -> Result<crate::datadogV2::model::CreatePageResponse, datadog::Error<CreateOnCallPageError>>
204 {
205 match self.create_on_call_page_with_http_info(body).await {
206 Ok(response_content) => {
207 if let Some(e) = response_content.entity {
208 Ok(e)
209 } else {
210 Err(datadog::Error::Serde(serde::de::Error::custom(
211 "response content was None",
212 )))
213 }
214 }
215 Err(err) => Err(err),
216 }
217 }
218
219 pub async fn create_on_call_page_with_http_info(
221 &self,
222 body: crate::datadogV2::model::CreatePageRequest,
223 ) -> Result<
224 datadog::ResponseContent<crate::datadogV2::model::CreatePageResponse>,
225 datadog::Error<CreateOnCallPageError>,
226 > {
227 let local_configuration = &self.config;
228 let operation_id = "v2.create_on_call_page";
229
230 let local_client = &self.client;
231
232 let local_uri_str = format!(
233 "{}/api/v2/on-call/pages",
234 local_configuration.get_operation_host(operation_id)
235 );
236 let mut local_req_builder =
237 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
238
239 let mut headers = HeaderMap::new();
241 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
242 headers.insert("Accept", HeaderValue::from_static("application/json"));
243
244 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
246 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
247 Err(e) => {
248 log::warn!("Failed to parse user agent header: {e}, falling back to default");
249 headers.insert(
250 reqwest::header::USER_AGENT,
251 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
252 )
253 }
254 };
255
256 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
258 headers.insert(
259 "DD-API-KEY",
260 HeaderValue::from_str(local_key.key.as_str())
261 .expect("failed to parse DD-API-KEY header"),
262 );
263 };
264 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
265 headers.insert(
266 "DD-APPLICATION-KEY",
267 HeaderValue::from_str(local_key.key.as_str())
268 .expect("failed to parse DD-APPLICATION-KEY header"),
269 );
270 };
271
272 let output = Vec::new();
274 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
275 if body.serialize(&mut ser).is_ok() {
276 if let Some(content_encoding) = headers.get("Content-Encoding") {
277 match content_encoding.to_str().unwrap_or_default() {
278 "gzip" => {
279 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
280 let _ = enc.write_all(ser.into_inner().as_slice());
281 match enc.finish() {
282 Ok(buf) => {
283 local_req_builder = local_req_builder.body(buf);
284 }
285 Err(e) => return Err(datadog::Error::Io(e)),
286 }
287 }
288 "deflate" => {
289 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
290 let _ = enc.write_all(ser.into_inner().as_slice());
291 match enc.finish() {
292 Ok(buf) => {
293 local_req_builder = local_req_builder.body(buf);
294 }
295 Err(e) => return Err(datadog::Error::Io(e)),
296 }
297 }
298 "zstd1" => {
299 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
300 let _ = enc.write_all(ser.into_inner().as_slice());
301 match enc.finish() {
302 Ok(buf) => {
303 local_req_builder = local_req_builder.body(buf);
304 }
305 Err(e) => return Err(datadog::Error::Io(e)),
306 }
307 }
308 _ => {
309 local_req_builder = local_req_builder.body(ser.into_inner());
310 }
311 }
312 } else {
313 local_req_builder = local_req_builder.body(ser.into_inner());
314 }
315 }
316
317 local_req_builder = local_req_builder.headers(headers);
318 let local_req = local_req_builder.build()?;
319 log::debug!("request content: {:?}", local_req.body());
320 let local_resp = local_client.execute(local_req).await?;
321
322 let local_status = local_resp.status();
323 let local_content = local_resp.text().await?;
324 log::debug!("response content: {}", local_content);
325
326 if !local_status.is_client_error() && !local_status.is_server_error() {
327 match serde_json::from_str::<crate::datadogV2::model::CreatePageResponse>(
328 &local_content,
329 ) {
330 Ok(e) => {
331 return Ok(datadog::ResponseContent {
332 status: local_status,
333 content: local_content,
334 entity: Some(e),
335 })
336 }
337 Err(e) => return Err(datadog::Error::Serde(e)),
338 };
339 } else {
340 let local_entity: Option<CreateOnCallPageError> =
341 serde_json::from_str(&local_content).ok();
342 let local_error = datadog::ResponseContent {
343 status: local_status,
344 content: local_content,
345 entity: local_entity,
346 };
347 Err(datadog::Error::ResponseError(local_error))
348 }
349 }
350
351 pub async fn escalate_on_call_page(
353 &self,
354 page_id: uuid::Uuid,
355 ) -> Result<(), datadog::Error<EscalateOnCallPageError>> {
356 match self.escalate_on_call_page_with_http_info(page_id).await {
357 Ok(_) => Ok(()),
358 Err(err) => Err(err),
359 }
360 }
361
362 pub async fn escalate_on_call_page_with_http_info(
364 &self,
365 page_id: uuid::Uuid,
366 ) -> Result<datadog::ResponseContent<()>, datadog::Error<EscalateOnCallPageError>> {
367 let local_configuration = &self.config;
368 let operation_id = "v2.escalate_on_call_page";
369
370 let local_client = &self.client;
371
372 let local_uri_str = format!(
373 "{}/api/v2/on-call/pages/{page_id}/escalate",
374 local_configuration.get_operation_host(operation_id),
375 page_id = datadog::urlencode(page_id.to_string())
376 );
377 let mut local_req_builder =
378 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
379
380 let mut headers = HeaderMap::new();
382 headers.insert("Accept", HeaderValue::from_static("*/*"));
383
384 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
386 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
387 Err(e) => {
388 log::warn!("Failed to parse user agent header: {e}, falling back to default");
389 headers.insert(
390 reqwest::header::USER_AGENT,
391 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
392 )
393 }
394 };
395
396 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
398 headers.insert(
399 "DD-API-KEY",
400 HeaderValue::from_str(local_key.key.as_str())
401 .expect("failed to parse DD-API-KEY header"),
402 );
403 };
404 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
405 headers.insert(
406 "DD-APPLICATION-KEY",
407 HeaderValue::from_str(local_key.key.as_str())
408 .expect("failed to parse DD-APPLICATION-KEY header"),
409 );
410 };
411
412 local_req_builder = local_req_builder.headers(headers);
413 let local_req = local_req_builder.build()?;
414 log::debug!("request content: {:?}", local_req.body());
415 let local_resp = local_client.execute(local_req).await?;
416
417 let local_status = local_resp.status();
418 let local_content = local_resp.text().await?;
419 log::debug!("response content: {}", local_content);
420
421 if !local_status.is_client_error() && !local_status.is_server_error() {
422 Ok(datadog::ResponseContent {
423 status: local_status,
424 content: local_content,
425 entity: None,
426 })
427 } else {
428 let local_entity: Option<EscalateOnCallPageError> =
429 serde_json::from_str(&local_content).ok();
430 let local_error = datadog::ResponseContent {
431 status: local_status,
432 content: local_content,
433 entity: local_entity,
434 };
435 Err(datadog::Error::ResponseError(local_error))
436 }
437 }
438
439 pub async fn resolve_on_call_page(
441 &self,
442 page_id: uuid::Uuid,
443 ) -> Result<(), datadog::Error<ResolveOnCallPageError>> {
444 match self.resolve_on_call_page_with_http_info(page_id).await {
445 Ok(_) => Ok(()),
446 Err(err) => Err(err),
447 }
448 }
449
450 pub async fn resolve_on_call_page_with_http_info(
452 &self,
453 page_id: uuid::Uuid,
454 ) -> Result<datadog::ResponseContent<()>, datadog::Error<ResolveOnCallPageError>> {
455 let local_configuration = &self.config;
456 let operation_id = "v2.resolve_on_call_page";
457
458 let local_client = &self.client;
459
460 let local_uri_str = format!(
461 "{}/api/v2/on-call/pages/{page_id}/resolve",
462 local_configuration.get_operation_host(operation_id),
463 page_id = datadog::urlencode(page_id.to_string())
464 );
465 let mut local_req_builder =
466 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
467
468 let mut headers = HeaderMap::new();
470 headers.insert("Accept", HeaderValue::from_static("*/*"));
471
472 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
474 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
475 Err(e) => {
476 log::warn!("Failed to parse user agent header: {e}, falling back to default");
477 headers.insert(
478 reqwest::header::USER_AGENT,
479 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
480 )
481 }
482 };
483
484 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
486 headers.insert(
487 "DD-API-KEY",
488 HeaderValue::from_str(local_key.key.as_str())
489 .expect("failed to parse DD-API-KEY header"),
490 );
491 };
492 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
493 headers.insert(
494 "DD-APPLICATION-KEY",
495 HeaderValue::from_str(local_key.key.as_str())
496 .expect("failed to parse DD-APPLICATION-KEY header"),
497 );
498 };
499
500 local_req_builder = local_req_builder.headers(headers);
501 let local_req = local_req_builder.build()?;
502 log::debug!("request content: {:?}", local_req.body());
503 let local_resp = local_client.execute(local_req).await?;
504
505 let local_status = local_resp.status();
506 let local_content = local_resp.text().await?;
507 log::debug!("response content: {}", local_content);
508
509 if !local_status.is_client_error() && !local_status.is_server_error() {
510 Ok(datadog::ResponseContent {
511 status: local_status,
512 content: local_content,
513 entity: None,
514 })
515 } else {
516 let local_entity: Option<ResolveOnCallPageError> =
517 serde_json::from_str(&local_content).ok();
518 let local_error = datadog::ResponseContent {
519 status: local_status,
520 content: local_content,
521 entity: local_entity,
522 };
523 Err(datadog::Error::ResponseError(local_error))
524 }
525 }
526}