1use crate::datadog;
5use async_stream::try_stream;
6use flate2::{
7 write::{GzEncoder, ZlibEncoder},
8 Compression,
9};
10use futures_core::stream::Stream;
11use reqwest::header::{HeaderMap, HeaderValue};
12use serde::{Deserialize, Serialize};
13use std::io::Write;
14
15#[non_exhaustive]
17#[derive(Clone, Default, Debug)]
18pub struct ListNotebooksOptionalParams {
19 pub author_handle: Option<String>,
21 pub exclude_author_handle: Option<String>,
23 pub start: Option<i64>,
25 pub count: Option<i64>,
27 pub sort_field: Option<String>,
29 pub sort_dir: Option<String>,
31 pub query: Option<String>,
33 pub include_cells: Option<bool>,
35 pub is_template: Option<bool>,
37 pub type_: Option<String>,
39}
40
41impl ListNotebooksOptionalParams {
42 pub fn author_handle(mut self, value: String) -> Self {
44 self.author_handle = Some(value);
45 self
46 }
47 pub fn exclude_author_handle(mut self, value: String) -> Self {
49 self.exclude_author_handle = Some(value);
50 self
51 }
52 pub fn start(mut self, value: i64) -> Self {
54 self.start = Some(value);
55 self
56 }
57 pub fn count(mut self, value: i64) -> Self {
59 self.count = Some(value);
60 self
61 }
62 pub fn sort_field(mut self, value: String) -> Self {
64 self.sort_field = Some(value);
65 self
66 }
67 pub fn sort_dir(mut self, value: String) -> Self {
69 self.sort_dir = Some(value);
70 self
71 }
72 pub fn query(mut self, value: String) -> Self {
74 self.query = Some(value);
75 self
76 }
77 pub fn include_cells(mut self, value: bool) -> Self {
79 self.include_cells = Some(value);
80 self
81 }
82 pub fn is_template(mut self, value: bool) -> Self {
84 self.is_template = Some(value);
85 self
86 }
87 pub fn type_(mut self, value: String) -> Self {
89 self.type_ = Some(value);
90 self
91 }
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(untagged)]
97pub enum CreateNotebookError {
98 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
99 UnknownValue(serde_json::Value),
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
104#[serde(untagged)]
105pub enum DeleteNotebookError {
106 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
107 UnknownValue(serde_json::Value),
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize)]
112#[serde(untagged)]
113pub enum GetNotebookError {
114 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
115 UnknownValue(serde_json::Value),
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
120#[serde(untagged)]
121pub enum ListNotebooksError {
122 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
123 UnknownValue(serde_json::Value),
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
128#[serde(untagged)]
129pub enum UpdateNotebookError {
130 APIErrorResponse(crate::datadogV1::model::APIErrorResponse),
131 UnknownValue(serde_json::Value),
132}
133
134#[derive(Debug, Clone)]
138pub struct NotebooksAPI {
139 config: datadog::Configuration,
140 client: reqwest_middleware::ClientWithMiddleware,
141}
142
143impl Default for NotebooksAPI {
144 fn default() -> Self {
145 Self::with_config(datadog::Configuration::default())
146 }
147}
148
149impl NotebooksAPI {
150 pub fn new() -> Self {
151 Self::default()
152 }
153 pub fn with_config(config: datadog::Configuration) -> Self {
154 let mut reqwest_client_builder = reqwest::Client::builder();
155
156 if let Some(proxy_url) = &config.proxy_url {
157 let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL");
158 reqwest_client_builder = reqwest_client_builder.proxy(proxy);
159 }
160
161 let mut middleware_client_builder =
162 reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap());
163
164 if config.enable_retry {
165 struct RetryableStatus;
166 impl reqwest_retry::RetryableStrategy for RetryableStatus {
167 fn handle(
168 &self,
169 res: &Result<reqwest::Response, reqwest_middleware::Error>,
170 ) -> Option<reqwest_retry::Retryable> {
171 match res {
172 Ok(success) => reqwest_retry::default_on_request_success(success),
173 Err(_) => None,
174 }
175 }
176 }
177 let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder()
178 .build_with_max_retries(config.max_retries);
179
180 let retry_middleware =
181 reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy(
182 backoff_policy,
183 RetryableStatus,
184 );
185
186 middleware_client_builder = middleware_client_builder.with(retry_middleware);
187 }
188
189 let client = middleware_client_builder.build();
190
191 Self { config, client }
192 }
193
194 pub fn with_client_and_config(
195 config: datadog::Configuration,
196 client: reqwest_middleware::ClientWithMiddleware,
197 ) -> Self {
198 Self { config, client }
199 }
200
201 pub async fn create_notebook(
203 &self,
204 body: crate::datadogV1::model::NotebookCreateRequest,
205 ) -> Result<crate::datadogV1::model::NotebookResponse, datadog::Error<CreateNotebookError>>
206 {
207 match self.create_notebook_with_http_info(body).await {
208 Ok(response_content) => {
209 if let Some(e) = response_content.entity {
210 Ok(e)
211 } else {
212 Err(datadog::Error::Serde(serde::de::Error::custom(
213 "response content was None",
214 )))
215 }
216 }
217 Err(err) => Err(err),
218 }
219 }
220
221 pub async fn create_notebook_with_http_info(
223 &self,
224 body: crate::datadogV1::model::NotebookCreateRequest,
225 ) -> Result<
226 datadog::ResponseContent<crate::datadogV1::model::NotebookResponse>,
227 datadog::Error<CreateNotebookError>,
228 > {
229 let local_configuration = &self.config;
230 let operation_id = "v1.create_notebook";
231
232 let local_client = &self.client;
233
234 let local_uri_str = format!(
235 "{}/api/v1/notebooks",
236 local_configuration.get_operation_host(operation_id)
237 );
238 let mut local_req_builder =
239 local_client.request(reqwest::Method::POST, local_uri_str.as_str());
240
241 let mut headers = HeaderMap::new();
243 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
244 headers.insert("Accept", HeaderValue::from_static("application/json"));
245
246 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
248 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
249 Err(e) => {
250 log::warn!("Failed to parse user agent header: {e}, falling back to default");
251 headers.insert(
252 reqwest::header::USER_AGENT,
253 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
254 )
255 }
256 };
257
258 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
260 headers.insert(
261 "DD-API-KEY",
262 HeaderValue::from_str(local_key.key.as_str())
263 .expect("failed to parse DD-API-KEY header"),
264 );
265 };
266 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
267 headers.insert(
268 "DD-APPLICATION-KEY",
269 HeaderValue::from_str(local_key.key.as_str())
270 .expect("failed to parse DD-APPLICATION-KEY header"),
271 );
272 };
273
274 let output = Vec::new();
276 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
277 if body.serialize(&mut ser).is_ok() {
278 if let Some(content_encoding) = headers.get("Content-Encoding") {
279 match content_encoding.to_str().unwrap_or_default() {
280 "gzip" => {
281 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
282 let _ = enc.write_all(ser.into_inner().as_slice());
283 match enc.finish() {
284 Ok(buf) => {
285 local_req_builder = local_req_builder.body(buf);
286 }
287 Err(e) => return Err(datadog::Error::Io(e)),
288 }
289 }
290 "deflate" => {
291 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
292 let _ = enc.write_all(ser.into_inner().as_slice());
293 match enc.finish() {
294 Ok(buf) => {
295 local_req_builder = local_req_builder.body(buf);
296 }
297 Err(e) => return Err(datadog::Error::Io(e)),
298 }
299 }
300 "zstd1" => {
301 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
302 let _ = enc.write_all(ser.into_inner().as_slice());
303 match enc.finish() {
304 Ok(buf) => {
305 local_req_builder = local_req_builder.body(buf);
306 }
307 Err(e) => return Err(datadog::Error::Io(e)),
308 }
309 }
310 _ => {
311 local_req_builder = local_req_builder.body(ser.into_inner());
312 }
313 }
314 } else {
315 local_req_builder = local_req_builder.body(ser.into_inner());
316 }
317 }
318
319 local_req_builder = local_req_builder.headers(headers);
320 let local_req = local_req_builder.build()?;
321 log::debug!("request content: {:?}", local_req.body());
322 let local_resp = local_client.execute(local_req).await?;
323
324 let local_status = local_resp.status();
325 let local_content = local_resp.text().await?;
326 log::debug!("response content: {}", local_content);
327
328 if !local_status.is_client_error() && !local_status.is_server_error() {
329 match serde_json::from_str::<crate::datadogV1::model::NotebookResponse>(&local_content)
330 {
331 Ok(e) => {
332 return Ok(datadog::ResponseContent {
333 status: local_status,
334 content: local_content,
335 entity: Some(e),
336 })
337 }
338 Err(e) => return Err(datadog::Error::Serde(e)),
339 };
340 } else {
341 let local_entity: Option<CreateNotebookError> =
342 serde_json::from_str(&local_content).ok();
343 let local_error = datadog::ResponseContent {
344 status: local_status,
345 content: local_content,
346 entity: local_entity,
347 };
348 Err(datadog::Error::ResponseError(local_error))
349 }
350 }
351
352 pub async fn delete_notebook(
354 &self,
355 notebook_id: i64,
356 ) -> Result<(), datadog::Error<DeleteNotebookError>> {
357 match self.delete_notebook_with_http_info(notebook_id).await {
358 Ok(_) => Ok(()),
359 Err(err) => Err(err),
360 }
361 }
362
363 pub async fn delete_notebook_with_http_info(
365 &self,
366 notebook_id: i64,
367 ) -> Result<datadog::ResponseContent<()>, datadog::Error<DeleteNotebookError>> {
368 let local_configuration = &self.config;
369 let operation_id = "v1.delete_notebook";
370
371 let local_client = &self.client;
372
373 let local_uri_str = format!(
374 "{}/api/v1/notebooks/{notebook_id}",
375 local_configuration.get_operation_host(operation_id),
376 notebook_id = notebook_id
377 );
378 let mut local_req_builder =
379 local_client.request(reqwest::Method::DELETE, local_uri_str.as_str());
380
381 let mut headers = HeaderMap::new();
383 headers.insert("Accept", HeaderValue::from_static("*/*"));
384
385 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
387 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
388 Err(e) => {
389 log::warn!("Failed to parse user agent header: {e}, falling back to default");
390 headers.insert(
391 reqwest::header::USER_AGENT,
392 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
393 )
394 }
395 };
396
397 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
399 headers.insert(
400 "DD-API-KEY",
401 HeaderValue::from_str(local_key.key.as_str())
402 .expect("failed to parse DD-API-KEY header"),
403 );
404 };
405 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
406 headers.insert(
407 "DD-APPLICATION-KEY",
408 HeaderValue::from_str(local_key.key.as_str())
409 .expect("failed to parse DD-APPLICATION-KEY header"),
410 );
411 };
412
413 local_req_builder = local_req_builder.headers(headers);
414 let local_req = local_req_builder.build()?;
415 log::debug!("request content: {:?}", local_req.body());
416 let local_resp = local_client.execute(local_req).await?;
417
418 let local_status = local_resp.status();
419 let local_content = local_resp.text().await?;
420 log::debug!("response content: {}", local_content);
421
422 if !local_status.is_client_error() && !local_status.is_server_error() {
423 Ok(datadog::ResponseContent {
424 status: local_status,
425 content: local_content,
426 entity: None,
427 })
428 } else {
429 let local_entity: Option<DeleteNotebookError> =
430 serde_json::from_str(&local_content).ok();
431 let local_error = datadog::ResponseContent {
432 status: local_status,
433 content: local_content,
434 entity: local_entity,
435 };
436 Err(datadog::Error::ResponseError(local_error))
437 }
438 }
439
440 pub async fn get_notebook(
442 &self,
443 notebook_id: i64,
444 ) -> Result<crate::datadogV1::model::NotebookResponse, datadog::Error<GetNotebookError>> {
445 match self.get_notebook_with_http_info(notebook_id).await {
446 Ok(response_content) => {
447 if let Some(e) = response_content.entity {
448 Ok(e)
449 } else {
450 Err(datadog::Error::Serde(serde::de::Error::custom(
451 "response content was None",
452 )))
453 }
454 }
455 Err(err) => Err(err),
456 }
457 }
458
459 pub async fn get_notebook_with_http_info(
461 &self,
462 notebook_id: i64,
463 ) -> Result<
464 datadog::ResponseContent<crate::datadogV1::model::NotebookResponse>,
465 datadog::Error<GetNotebookError>,
466 > {
467 let local_configuration = &self.config;
468 let operation_id = "v1.get_notebook";
469
470 let local_client = &self.client;
471
472 let local_uri_str = format!(
473 "{}/api/v1/notebooks/{notebook_id}",
474 local_configuration.get_operation_host(operation_id),
475 notebook_id = notebook_id
476 );
477 let mut local_req_builder =
478 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
479
480 let mut headers = HeaderMap::new();
482 headers.insert("Accept", HeaderValue::from_static("application/json"));
483
484 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
486 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
487 Err(e) => {
488 log::warn!("Failed to parse user agent header: {e}, falling back to default");
489 headers.insert(
490 reqwest::header::USER_AGENT,
491 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
492 )
493 }
494 };
495
496 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
498 headers.insert(
499 "DD-API-KEY",
500 HeaderValue::from_str(local_key.key.as_str())
501 .expect("failed to parse DD-API-KEY header"),
502 );
503 };
504 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
505 headers.insert(
506 "DD-APPLICATION-KEY",
507 HeaderValue::from_str(local_key.key.as_str())
508 .expect("failed to parse DD-APPLICATION-KEY header"),
509 );
510 };
511
512 local_req_builder = local_req_builder.headers(headers);
513 let local_req = local_req_builder.build()?;
514 log::debug!("request content: {:?}", local_req.body());
515 let local_resp = local_client.execute(local_req).await?;
516
517 let local_status = local_resp.status();
518 let local_content = local_resp.text().await?;
519 log::debug!("response content: {}", local_content);
520
521 if !local_status.is_client_error() && !local_status.is_server_error() {
522 match serde_json::from_str::<crate::datadogV1::model::NotebookResponse>(&local_content)
523 {
524 Ok(e) => {
525 return Ok(datadog::ResponseContent {
526 status: local_status,
527 content: local_content,
528 entity: Some(e),
529 })
530 }
531 Err(e) => return Err(datadog::Error::Serde(e)),
532 };
533 } else {
534 let local_entity: Option<GetNotebookError> = serde_json::from_str(&local_content).ok();
535 let local_error = datadog::ResponseContent {
536 status: local_status,
537 content: local_content,
538 entity: local_entity,
539 };
540 Err(datadog::Error::ResponseError(local_error))
541 }
542 }
543
544 pub async fn list_notebooks(
547 &self,
548 params: ListNotebooksOptionalParams,
549 ) -> Result<crate::datadogV1::model::NotebooksResponse, datadog::Error<ListNotebooksError>>
550 {
551 match self.list_notebooks_with_http_info(params).await {
552 Ok(response_content) => {
553 if let Some(e) = response_content.entity {
554 Ok(e)
555 } else {
556 Err(datadog::Error::Serde(serde::de::Error::custom(
557 "response content was None",
558 )))
559 }
560 }
561 Err(err) => Err(err),
562 }
563 }
564
565 pub fn list_notebooks_with_pagination(
566 &self,
567 mut params: ListNotebooksOptionalParams,
568 ) -> impl Stream<
569 Item = Result<
570 crate::datadogV1::model::NotebooksResponseData,
571 datadog::Error<ListNotebooksError>,
572 >,
573 > + '_ {
574 try_stream! {
575 let mut page_size: i64 = 100;
576 if params.count.is_none() {
577 params.count = Some(page_size);
578 } else {
579 page_size = params.count.unwrap().clone();
580 }
581 loop {
582 let resp = self.list_notebooks(params.clone()).await?;
583 let Some(data) = resp.data else { break };
584
585 let r = data;
586 let count = r.len();
587 for team in r {
588 yield team;
589 }
590
591 if count < page_size as usize {
592 break;
593 }
594 if params.start.is_none() {
595 params.start = Some(page_size.clone());
596 } else {
597 params.start = Some(params.start.unwrap() + page_size.clone());
598 }
599 }
600 }
601 }
602
603 pub async fn list_notebooks_with_http_info(
606 &self,
607 params: ListNotebooksOptionalParams,
608 ) -> Result<
609 datadog::ResponseContent<crate::datadogV1::model::NotebooksResponse>,
610 datadog::Error<ListNotebooksError>,
611 > {
612 let local_configuration = &self.config;
613 let operation_id = "v1.list_notebooks";
614
615 let author_handle = params.author_handle;
617 let exclude_author_handle = params.exclude_author_handle;
618 let start = params.start;
619 let count = params.count;
620 let sort_field = params.sort_field;
621 let sort_dir = params.sort_dir;
622 let query = params.query;
623 let include_cells = params.include_cells;
624 let is_template = params.is_template;
625 let type_ = params.type_;
626
627 let local_client = &self.client;
628
629 let local_uri_str = format!(
630 "{}/api/v1/notebooks",
631 local_configuration.get_operation_host(operation_id)
632 );
633 let mut local_req_builder =
634 local_client.request(reqwest::Method::GET, local_uri_str.as_str());
635
636 if let Some(ref local_query_param) = author_handle {
637 local_req_builder =
638 local_req_builder.query(&[("author_handle", &local_query_param.to_string())]);
639 };
640 if let Some(ref local_query_param) = exclude_author_handle {
641 local_req_builder = local_req_builder
642 .query(&[("exclude_author_handle", &local_query_param.to_string())]);
643 };
644 if let Some(ref local_query_param) = start {
645 local_req_builder =
646 local_req_builder.query(&[("start", &local_query_param.to_string())]);
647 };
648 if let Some(ref local_query_param) = count {
649 local_req_builder =
650 local_req_builder.query(&[("count", &local_query_param.to_string())]);
651 };
652 if let Some(ref local_query_param) = sort_field {
653 local_req_builder =
654 local_req_builder.query(&[("sort_field", &local_query_param.to_string())]);
655 };
656 if let Some(ref local_query_param) = sort_dir {
657 local_req_builder =
658 local_req_builder.query(&[("sort_dir", &local_query_param.to_string())]);
659 };
660 if let Some(ref local_query_param) = query {
661 local_req_builder =
662 local_req_builder.query(&[("query", &local_query_param.to_string())]);
663 };
664 if let Some(ref local_query_param) = include_cells {
665 local_req_builder =
666 local_req_builder.query(&[("include_cells", &local_query_param.to_string())]);
667 };
668 if let Some(ref local_query_param) = is_template {
669 local_req_builder =
670 local_req_builder.query(&[("is_template", &local_query_param.to_string())]);
671 };
672 if let Some(ref local_query_param) = type_ {
673 local_req_builder =
674 local_req_builder.query(&[("type", &local_query_param.to_string())]);
675 };
676
677 let mut headers = HeaderMap::new();
679 headers.insert("Accept", HeaderValue::from_static("application/json"));
680
681 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
683 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
684 Err(e) => {
685 log::warn!("Failed to parse user agent header: {e}, falling back to default");
686 headers.insert(
687 reqwest::header::USER_AGENT,
688 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
689 )
690 }
691 };
692
693 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
695 headers.insert(
696 "DD-API-KEY",
697 HeaderValue::from_str(local_key.key.as_str())
698 .expect("failed to parse DD-API-KEY header"),
699 );
700 };
701 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
702 headers.insert(
703 "DD-APPLICATION-KEY",
704 HeaderValue::from_str(local_key.key.as_str())
705 .expect("failed to parse DD-APPLICATION-KEY header"),
706 );
707 };
708
709 local_req_builder = local_req_builder.headers(headers);
710 let local_req = local_req_builder.build()?;
711 log::debug!("request content: {:?}", local_req.body());
712 let local_resp = local_client.execute(local_req).await?;
713
714 let local_status = local_resp.status();
715 let local_content = local_resp.text().await?;
716 log::debug!("response content: {}", local_content);
717
718 if !local_status.is_client_error() && !local_status.is_server_error() {
719 match serde_json::from_str::<crate::datadogV1::model::NotebooksResponse>(&local_content)
720 {
721 Ok(e) => {
722 return Ok(datadog::ResponseContent {
723 status: local_status,
724 content: local_content,
725 entity: Some(e),
726 })
727 }
728 Err(e) => return Err(datadog::Error::Serde(e)),
729 };
730 } else {
731 let local_entity: Option<ListNotebooksError> =
732 serde_json::from_str(&local_content).ok();
733 let local_error = datadog::ResponseContent {
734 status: local_status,
735 content: local_content,
736 entity: local_entity,
737 };
738 Err(datadog::Error::ResponseError(local_error))
739 }
740 }
741
742 pub async fn update_notebook(
744 &self,
745 notebook_id: i64,
746 body: crate::datadogV1::model::NotebookUpdateRequest,
747 ) -> Result<crate::datadogV1::model::NotebookResponse, datadog::Error<UpdateNotebookError>>
748 {
749 match self.update_notebook_with_http_info(notebook_id, body).await {
750 Ok(response_content) => {
751 if let Some(e) = response_content.entity {
752 Ok(e)
753 } else {
754 Err(datadog::Error::Serde(serde::de::Error::custom(
755 "response content was None",
756 )))
757 }
758 }
759 Err(err) => Err(err),
760 }
761 }
762
763 pub async fn update_notebook_with_http_info(
765 &self,
766 notebook_id: i64,
767 body: crate::datadogV1::model::NotebookUpdateRequest,
768 ) -> Result<
769 datadog::ResponseContent<crate::datadogV1::model::NotebookResponse>,
770 datadog::Error<UpdateNotebookError>,
771 > {
772 let local_configuration = &self.config;
773 let operation_id = "v1.update_notebook";
774
775 let local_client = &self.client;
776
777 let local_uri_str = format!(
778 "{}/api/v1/notebooks/{notebook_id}",
779 local_configuration.get_operation_host(operation_id),
780 notebook_id = notebook_id
781 );
782 let mut local_req_builder =
783 local_client.request(reqwest::Method::PUT, local_uri_str.as_str());
784
785 let mut headers = HeaderMap::new();
787 headers.insert("Content-Type", HeaderValue::from_static("application/json"));
788 headers.insert("Accept", HeaderValue::from_static("application/json"));
789
790 match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
792 Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
793 Err(e) => {
794 log::warn!("Failed to parse user agent header: {e}, falling back to default");
795 headers.insert(
796 reqwest::header::USER_AGENT,
797 HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
798 )
799 }
800 };
801
802 if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
804 headers.insert(
805 "DD-API-KEY",
806 HeaderValue::from_str(local_key.key.as_str())
807 .expect("failed to parse DD-API-KEY header"),
808 );
809 };
810 if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
811 headers.insert(
812 "DD-APPLICATION-KEY",
813 HeaderValue::from_str(local_key.key.as_str())
814 .expect("failed to parse DD-APPLICATION-KEY header"),
815 );
816 };
817
818 let output = Vec::new();
820 let mut ser = serde_json::Serializer::with_formatter(output, datadog::DDFormatter);
821 if body.serialize(&mut ser).is_ok() {
822 if let Some(content_encoding) = headers.get("Content-Encoding") {
823 match content_encoding.to_str().unwrap_or_default() {
824 "gzip" => {
825 let mut enc = GzEncoder::new(Vec::new(), Compression::default());
826 let _ = enc.write_all(ser.into_inner().as_slice());
827 match enc.finish() {
828 Ok(buf) => {
829 local_req_builder = local_req_builder.body(buf);
830 }
831 Err(e) => return Err(datadog::Error::Io(e)),
832 }
833 }
834 "deflate" => {
835 let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
836 let _ = enc.write_all(ser.into_inner().as_slice());
837 match enc.finish() {
838 Ok(buf) => {
839 local_req_builder = local_req_builder.body(buf);
840 }
841 Err(e) => return Err(datadog::Error::Io(e)),
842 }
843 }
844 "zstd1" => {
845 let mut enc = zstd::stream::Encoder::new(Vec::new(), 0).unwrap();
846 let _ = enc.write_all(ser.into_inner().as_slice());
847 match enc.finish() {
848 Ok(buf) => {
849 local_req_builder = local_req_builder.body(buf);
850 }
851 Err(e) => return Err(datadog::Error::Io(e)),
852 }
853 }
854 _ => {
855 local_req_builder = local_req_builder.body(ser.into_inner());
856 }
857 }
858 } else {
859 local_req_builder = local_req_builder.body(ser.into_inner());
860 }
861 }
862
863 local_req_builder = local_req_builder.headers(headers);
864 let local_req = local_req_builder.build()?;
865 log::debug!("request content: {:?}", local_req.body());
866 let local_resp = local_client.execute(local_req).await?;
867
868 let local_status = local_resp.status();
869 let local_content = local_resp.text().await?;
870 log::debug!("response content: {}", local_content);
871
872 if !local_status.is_client_error() && !local_status.is_server_error() {
873 match serde_json::from_str::<crate::datadogV1::model::NotebookResponse>(&local_content)
874 {
875 Ok(e) => {
876 return Ok(datadog::ResponseContent {
877 status: local_status,
878 content: local_content,
879 entity: Some(e),
880 })
881 }
882 Err(e) => return Err(datadog::Error::Serde(e)),
883 };
884 } else {
885 let local_entity: Option<UpdateNotebookError> =
886 serde_json::from_str(&local_content).ok();
887 let local_error = datadog::ResponseContent {
888 status: local_status,
889 content: local_content,
890 entity: local_entity,
891 };
892 Err(datadog::Error::ResponseError(local_error))
893 }
894 }
895}