datadog_api_client/datadog/
mod.rs

1use serde::{Deserialize, Deserializer, Serialize, Serializer};
2use std::error;
3use std::fmt;
4
5mod configuration;
6pub use configuration::{APIKey, Configuration, DEFAULT_USER_AGENT};
7
8#[derive(Debug, Clone)]
9pub struct ResponseContent<T> {
10    pub status: reqwest::StatusCode,
11    pub content: String,
12    pub entity: Option<T>,
13}
14
15#[derive(Debug, Clone)]
16pub struct UnstableOperationDisabledError {
17    pub msg: String,
18}
19
20#[derive(Debug)]
21pub enum Error<T> {
22    Reqwest(reqwest::Error),
23    ReqwestMiddleware(reqwest_middleware::Error),
24    Serde(serde_json::Error),
25    Io(std::io::Error),
26    ResponseError(ResponseContent<T>),
27    UnstableOperationDisabledError(UnstableOperationDisabledError),
28}
29
30impl<T> fmt::Display for Error<T> {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        let (module, e) = match self {
33            Error::Reqwest(e) => ("reqwest", e.to_string()),
34            Error::ReqwestMiddleware(e) => ("reqwest_middleware", e.to_string()),
35            Error::Serde(e) => ("serde", e.to_string()),
36            Error::Io(e) => ("IO", e.to_string()),
37            Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
38            Error::UnstableOperationDisabledError(e) => {
39                ("unstable_operation_disabled", e.msg.to_string())
40            }
41        };
42        write!(f, "error in {}: {}", module, e)
43    }
44}
45
46impl<T: fmt::Debug> error::Error for Error<T> {
47    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
48        Some(match self {
49            Error::Reqwest(e) => e,
50            Error::ReqwestMiddleware(e) => e,
51            Error::Serde(e) => e,
52            Error::Io(e) => e,
53            Error::ResponseError(_) => return None,
54            Error::UnstableOperationDisabledError(_) => return None,
55        })
56    }
57}
58
59impl<T> From<reqwest::Error> for Error<T> {
60    fn from(e: reqwest::Error) -> Self {
61        Error::Reqwest(e)
62    }
63}
64
65impl<T> From<reqwest_middleware::Error> for Error<T> {
66    fn from(e: reqwest_middleware::Error) -> Self {
67        Error::ReqwestMiddleware(e)
68    }
69}
70
71impl<T> From<serde_json::Error> for Error<T> {
72    fn from(e: serde_json::Error) -> Self {
73        Error::Serde(e)
74    }
75}
76
77impl<T> From<std::io::Error> for Error<T> {
78    fn from(e: std::io::Error) -> Self {
79        Error::Io(e)
80    }
81}
82
83pub(crate) fn urlencode<T: AsRef<str>>(s: T) -> String {
84    ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
85}
86
87pub(crate) struct DDFormatter;
88
89impl serde_json::ser::Formatter for DDFormatter {
90    fn write_f64<W>(&mut self, writer: &mut W, value: f64) -> std::io::Result<()>
91    where
92        W: ?Sized + std::io::Write,
93    {
94        write!(writer, "{}", value.to_string())
95    }
96}
97
98#[derive(Clone, Debug, Eq, PartialEq)]
99pub struct UnparsedObject {
100    pub value: serde_json::Value,
101}
102
103impl<'de> Deserialize<'de> for UnparsedObject {
104    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
105    where
106        D: Deserializer<'de>,
107    {
108        let val: serde_json::Value = Deserialize::deserialize(deserializer)?;
109        Ok(UnparsedObject { value: val })
110    }
111}
112
113impl Serialize for UnparsedObject {
114    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
115        self.value.serialize(serializer)
116    }
117}