1use std::{error, fmt};
2
3#[derive(Debug, Clone)]
4pub struct ResponseContent<T> {
5 pub status: reqwest::StatusCode,
6 pub content: String,
7 pub entity: Option<T>
8}
9
10#[derive(Debug)]
11pub enum Error<T> {
12 Reqwest(reqwest::Error),
13 Serde(serde_json::Error),
14 Io(std::io::Error),
15 ResponseError(ResponseContent<T>)
16}
17
18impl<T> fmt::Display for Error<T> {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 let (module, e) = match self {
21 Error::Reqwest(e) => ("reqwest", e.to_string()),
22 Error::Serde(e) => ("serde", e.to_string()),
23 Error::Io(e) => ("IO", e.to_string()),
24 Error::ResponseError(e) => ("response", format!("status code {}", e.status))
25 };
26 write!(f, "error in {}: {}", module, e)
27 }
28}
29
30impl<T: fmt::Debug> error::Error for Error<T> {
31 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
32 Some(match self {
33 Error::Reqwest(e) => e,
34 Error::Serde(e) => e,
35 Error::Io(e) => e,
36 Error::ResponseError(_) => return None
37 })
38 }
39}
40
41impl<T> From<reqwest::Error> for Error<T> {
42 fn from(e: reqwest::Error) -> Self {
43 Error::Reqwest(e)
44 }
45}
46
47impl<T> From<serde_json::Error> for Error<T> {
48 fn from(e: serde_json::Error) -> Self {
49 Error::Serde(e)
50 }
51}
52
53impl<T> From<std::io::Error> for Error<T> {
54 fn from(e: std::io::Error) -> Self {
55 Error::Io(e)
56 }
57}
58
59pub fn urlencode<T: AsRef<str>>(s: T) -> String {
60 ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
61}
62
63pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
64 if let serde_json::Value::Object(object) = value {
65 let mut params = vec![];
66
67 for (key, value) in object {
68 match value {
69 serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
70 &format!("{}[{}]", prefix, key),
71 value
72 )),
73 serde_json::Value::Array(array) => {
74 for (i, value) in array.iter().enumerate() {
75 params.append(&mut parse_deep_object(
76 &format!("{}[{}][{}]", prefix, key, i),
77 value
78 ));
79 }
80 }
81 serde_json::Value::String(s) => {
82 params.push((format!("{}[{}]", prefix, key), s.clone()))
83 }
84 _ => params.push((format!("{}[{}]", prefix, key), value.to_string()))
85 }
86 }
87
88 return params;
89 }
90
91 unimplemented!("Only objects are supported with style=deepObject")
92}
93
94#[allow(dead_code)]
97enum ContentType {
98 Json,
99 Text,
100 Unsupported(String)
101}
102
103impl From<&str> for ContentType {
104 fn from(content_type: &str) -> Self {
105 if content_type.starts_with("application") && content_type.contains("json") {
106 return Self::Json;
107 } else if content_type.starts_with("text/plain") {
108 return Self::Text;
109 } else {
110 return Self::Unsupported(content_type.to_string());
111 }
112 }
113}
114
115#[cfg(feature = "account")]
116pub mod account_api;
117#[cfg(feature = "ai-agents")]
118pub mod ai_agents_api;
119#[cfg(feature = "apps")]
120pub mod apps_api;
121#[cfg(feature = "balancers")]
122pub mod balancers_api;
123#[cfg(feature = "container-registry")]
124pub mod container_registry_api;
125#[cfg(feature = "databases")]
126pub mod databases_api;
127#[cfg(feature = "dedicated-servers")]
128pub mod dedicated_servers_api;
129#[cfg(feature = "domains")]
130pub mod domains_api;
131#[cfg(feature = "firewall")]
132pub mod firewall_api;
133#[cfg(feature = "floating-ip")]
134pub mod floating_ip_api;
135#[cfg(feature = "images")]
136pub mod images_api;
137#[cfg(feature = "knowledge-bases")]
138pub mod knowledge_bases_api;
139#[cfg(feature = "kubernetes")]
140pub mod kubernetes_api;
141#[cfg(feature = "locations")]
142pub mod locations_api;
143#[cfg(feature = "mail")]
144pub mod mail_api;
145#[cfg(feature = "network-drives")]
146pub mod network_drives_api;
147#[cfg(feature = "payments")]
148pub mod payments_api;
149#[cfg(feature = "projects")]
150pub mod projects_api;
151#[cfg(feature = "routers")]
152pub mod routers_api;
153#[cfg(feature = "s3")]
154pub mod s3_api;
155#[cfg(feature = "servers")]
156pub mod servers_api;
157#[cfg(feature = "ssh")]
158pub mod ssh_api;
159#[cfg(feature = "vpc")]
160pub mod vpc_api;
161
162pub mod configuration;