1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateCommandError {
22    UnknownValue(serde_json::Value),
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum DeleteCommandError {
29    UnknownValue(serde_json::Value),
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum GetCommandByIdError {
36    UnknownValue(serde_json::Value),
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum ListCommandError {
43    UnknownValue(serde_json::Value),
44}
45
46
47pub async fn create_command(configuration: &configuration::Configuration, command_resource: Option<models::CommandResource>) -> Result<models::CommandResource, Error<CreateCommandError>> {
48    let p_body_command_resource = command_resource;
50
51    let uri_str = format!("{}/api/v1/command", configuration.base_path);
52    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
53
54    if let Some(ref apikey) = configuration.api_key {
55        let key = apikey.key.clone();
56        let value = match apikey.prefix {
57            Some(ref prefix) => format!("{} {}", prefix, key),
58            None => key,
59        };
60        req_builder = req_builder.query(&[("apikey", value)]);
61    }
62    if let Some(ref user_agent) = configuration.user_agent {
63        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
64    }
65    if let Some(ref apikey) = configuration.api_key {
66        let key = apikey.key.clone();
67        let value = match apikey.prefix {
68            Some(ref prefix) => format!("{} {}", prefix, key),
69            None => key,
70        };
71        req_builder = req_builder.header("X-Api-Key", value);
72    };
73    req_builder = req_builder.json(&p_body_command_resource);
74
75    let req = req_builder.build()?;
76    let resp = configuration.client.execute(req).await?;
77
78    let status = resp.status();
79    let content_type = resp
80        .headers()
81        .get("content-type")
82        .and_then(|v| v.to_str().ok())
83        .unwrap_or("application/octet-stream");
84    let content_type = super::ContentType::from(content_type);
85
86    if !status.is_client_error() && !status.is_server_error() {
87        let content = resp.text().await?;
88        match content_type {
89            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
90            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CommandResource`"))),
91            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CommandResource`")))),
92        }
93    } else {
94        let content = resp.text().await?;
95        let entity: Option<CreateCommandError> = serde_json::from_str(&content).ok();
96        Err(Error::ResponseError(ResponseContent { status, content, entity }))
97    }
98}
99
100pub async fn delete_command(configuration: &configuration::Configuration, id: i32) -> Result<(), Error<DeleteCommandError>> {
101    let p_path_id = id;
103
104    let uri_str = format!("{}/api/v1/command/{id}", configuration.base_path, id=p_path_id);
105    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
106
107    if let Some(ref apikey) = configuration.api_key {
108        let key = apikey.key.clone();
109        let value = match apikey.prefix {
110            Some(ref prefix) => format!("{} {}", prefix, key),
111            None => key,
112        };
113        req_builder = req_builder.query(&[("apikey", value)]);
114    }
115    if let Some(ref user_agent) = configuration.user_agent {
116        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
117    }
118    if let Some(ref apikey) = configuration.api_key {
119        let key = apikey.key.clone();
120        let value = match apikey.prefix {
121            Some(ref prefix) => format!("{} {}", prefix, key),
122            None => key,
123        };
124        req_builder = req_builder.header("X-Api-Key", value);
125    };
126
127    let req = req_builder.build()?;
128    let resp = configuration.client.execute(req).await?;
129
130    let status = resp.status();
131
132    if !status.is_client_error() && !status.is_server_error() {
133        Ok(())
134    } else {
135        let content = resp.text().await?;
136        let entity: Option<DeleteCommandError> = serde_json::from_str(&content).ok();
137        Err(Error::ResponseError(ResponseContent { status, content, entity }))
138    }
139}
140
141pub async fn get_command_by_id(configuration: &configuration::Configuration, id: i32) -> Result<models::CommandResource, Error<GetCommandByIdError>> {
142    let p_path_id = id;
144
145    let uri_str = format!("{}/api/v1/command/{id}", configuration.base_path, id=p_path_id);
146    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
147
148    if let Some(ref apikey) = configuration.api_key {
149        let key = apikey.key.clone();
150        let value = match apikey.prefix {
151            Some(ref prefix) => format!("{} {}", prefix, key),
152            None => key,
153        };
154        req_builder = req_builder.query(&[("apikey", value)]);
155    }
156    if let Some(ref user_agent) = configuration.user_agent {
157        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
158    }
159    if let Some(ref apikey) = configuration.api_key {
160        let key = apikey.key.clone();
161        let value = match apikey.prefix {
162            Some(ref prefix) => format!("{} {}", prefix, key),
163            None => key,
164        };
165        req_builder = req_builder.header("X-Api-Key", value);
166    };
167
168    let req = req_builder.build()?;
169    let resp = configuration.client.execute(req).await?;
170
171    let status = resp.status();
172    let content_type = resp
173        .headers()
174        .get("content-type")
175        .and_then(|v| v.to_str().ok())
176        .unwrap_or("application/octet-stream");
177    let content_type = super::ContentType::from(content_type);
178
179    if !status.is_client_error() && !status.is_server_error() {
180        let content = resp.text().await?;
181        match content_type {
182            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
183            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CommandResource`"))),
184            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CommandResource`")))),
185        }
186    } else {
187        let content = resp.text().await?;
188        let entity: Option<GetCommandByIdError> = serde_json::from_str(&content).ok();
189        Err(Error::ResponseError(ResponseContent { status, content, entity }))
190    }
191}
192
193pub async fn list_command(configuration: &configuration::Configuration, ) -> Result<Vec<models::CommandResource>, Error<ListCommandError>> {
194
195    let uri_str = format!("{}/api/v1/command", configuration.base_path);
196    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
197
198    if let Some(ref apikey) = configuration.api_key {
199        let key = apikey.key.clone();
200        let value = match apikey.prefix {
201            Some(ref prefix) => format!("{} {}", prefix, key),
202            None => key,
203        };
204        req_builder = req_builder.query(&[("apikey", value)]);
205    }
206    if let Some(ref user_agent) = configuration.user_agent {
207        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
208    }
209    if let Some(ref apikey) = configuration.api_key {
210        let key = apikey.key.clone();
211        let value = match apikey.prefix {
212            Some(ref prefix) => format!("{} {}", prefix, key),
213            None => key,
214        };
215        req_builder = req_builder.header("X-Api-Key", value);
216    };
217
218    let req = req_builder.build()?;
219    let resp = configuration.client.execute(req).await?;
220
221    let status = resp.status();
222    let content_type = resp
223        .headers()
224        .get("content-type")
225        .and_then(|v| v.to_str().ok())
226        .unwrap_or("application/octet-stream");
227    let content_type = super::ContentType::from(content_type);
228
229    if !status.is_client_error() && !status.is_server_error() {
230        let content = resp.text().await?;
231        match content_type {
232            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
233            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::CommandResource>`"))),
234            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::CommandResource>`")))),
235        }
236    } else {
237        let content = resp.text().await?;
238        let entity: Option<ListCommandError> = serde_json::from_str(&content).ok();
239        Err(Error::ResponseError(ResponseContent { status, content, entity }))
240    }
241}
242