incus_client/apis/
server_api.rs1use 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 ServerGetError {
22 Status500(models::InstancesGet500Response),
23 UnknownValue(serde_json::Value),
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum ServerGetUntrustedError {
30 Status500(models::InstancesGet500Response),
31 UnknownValue(serde_json::Value),
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum ServerPatchError {
38 Status400(models::InstancesPut400Response),
39 Status403(models::InstancesGet403Response),
40 Status412(models::InstanceMetadataPut412Response),
41 Status500(models::InstancesGet500Response),
42 UnknownValue(serde_json::Value),
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum ServerPutError {
49 Status400(models::InstancesPut400Response),
50 Status403(models::InstancesGet403Response),
51 Status412(models::InstanceMetadataPut412Response),
52 Status500(models::InstancesGet500Response),
53 UnknownValue(serde_json::Value),
54}
55
56
57pub async fn server_get(configuration: &configuration::Configuration, target: Option<&str>, project: Option<&str>) -> Result<models::ServerGet200Response, Error<ServerGetError>> {
59 let p_query_target = target;
61 let p_query_project = project;
62
63 let uri_str = format!("{}/1.0", configuration.base_path);
64 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
65
66 if let Some(ref param_value) = p_query_target {
67 req_builder = req_builder.query(&[("target", ¶m_value.to_string())]);
68 }
69 if let Some(ref param_value) = p_query_project {
70 req_builder = req_builder.query(&[("project", ¶m_value.to_string())]);
71 }
72 if let Some(ref user_agent) = configuration.user_agent {
73 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
74 }
75
76 let req = req_builder.build()?;
77 let resp = configuration.client.execute(req).await?;
78
79 let status = resp.status();
80 let content_type = resp
81 .headers()
82 .get("content-type")
83 .and_then(|v| v.to_str().ok())
84 .unwrap_or("application/octet-stream");
85 let content_type = super::ContentType::from(content_type);
86
87 if !status.is_client_error() && !status.is_server_error() {
88 let content = resp.text().await?;
89 match content_type {
90 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
91 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ServerGet200Response`"))),
92 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::ServerGet200Response`")))),
93 }
94 } else {
95 let content = resp.text().await?;
96 let entity: Option<ServerGetError> = serde_json::from_str(&content).ok();
97 Err(Error::ResponseError(ResponseContent { status, content, entity }))
98 }
99}
100
101pub async fn server_get_untrusted(configuration: &configuration::Configuration, ) -> Result<models::ServerGetUntrusted200Response, Error<ServerGetUntrustedError>> {
103
104 let uri_str = format!("{}/1.0?public", configuration.base_path);
105 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
106
107 if let Some(ref user_agent) = configuration.user_agent {
108 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
109 }
110
111 let req = req_builder.build()?;
112 let resp = configuration.client.execute(req).await?;
113
114 let status = resp.status();
115 let content_type = resp
116 .headers()
117 .get("content-type")
118 .and_then(|v| v.to_str().ok())
119 .unwrap_or("application/octet-stream");
120 let content_type = super::ContentType::from(content_type);
121
122 if !status.is_client_error() && !status.is_server_error() {
123 let content = resp.text().await?;
124 match content_type {
125 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
126 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ServerGetUntrusted200Response`"))),
127 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::ServerGetUntrusted200Response`")))),
128 }
129 } else {
130 let content = resp.text().await?;
131 let entity: Option<ServerGetUntrustedError> = serde_json::from_str(&content).ok();
132 Err(Error::ResponseError(ResponseContent { status, content, entity }))
133 }
134}
135
136pub async fn server_patch(configuration: &configuration::Configuration, server: models::ServerPut, target: Option<&str>) -> Result<models::InstancePatch200Response, Error<ServerPatchError>> {
138 let p_body_server = server;
140 let p_query_target = target;
141
142 let uri_str = format!("{}/1.0", configuration.base_path);
143 let mut req_builder = configuration.client.request(reqwest::Method::PATCH, &uri_str);
144
145 if let Some(ref param_value) = p_query_target {
146 req_builder = req_builder.query(&[("target", ¶m_value.to_string())]);
147 }
148 if let Some(ref user_agent) = configuration.user_agent {
149 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
150 }
151 req_builder = req_builder.json(&p_body_server);
152
153 let req = req_builder.build()?;
154 let resp = configuration.client.execute(req).await?;
155
156 let status = resp.status();
157 let content_type = resp
158 .headers()
159 .get("content-type")
160 .and_then(|v| v.to_str().ok())
161 .unwrap_or("application/octet-stream");
162 let content_type = super::ContentType::from(content_type);
163
164 if !status.is_client_error() && !status.is_server_error() {
165 let content = resp.text().await?;
166 match content_type {
167 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
168 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::InstancePatch200Response`"))),
169 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::InstancePatch200Response`")))),
170 }
171 } else {
172 let content = resp.text().await?;
173 let entity: Option<ServerPatchError> = serde_json::from_str(&content).ok();
174 Err(Error::ResponseError(ResponseContent { status, content, entity }))
175 }
176}
177
178pub async fn server_put(configuration: &configuration::Configuration, server: models::ServerPut, target: Option<&str>) -> Result<models::InstancePatch200Response, Error<ServerPutError>> {
180 let p_body_server = server;
182 let p_query_target = target;
183
184 let uri_str = format!("{}/1.0", configuration.base_path);
185 let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
186
187 if let Some(ref param_value) = p_query_target {
188 req_builder = req_builder.query(&[("target", ¶m_value.to_string())]);
189 }
190 if let Some(ref user_agent) = configuration.user_agent {
191 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
192 }
193 req_builder = req_builder.json(&p_body_server);
194
195 let req = req_builder.build()?;
196 let resp = configuration.client.execute(req).await?;
197
198 let status = resp.status();
199 let content_type = resp
200 .headers()
201 .get("content-type")
202 .and_then(|v| v.to_str().ok())
203 .unwrap_or("application/octet-stream");
204 let content_type = super::ContentType::from(content_type);
205
206 if !status.is_client_error() && !status.is_server_error() {
207 let content = resp.text().await?;
208 match content_type {
209 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
210 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::InstancePatch200Response`"))),
211 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::InstancePatch200Response`")))),
212 }
213 } else {
214 let content = resp.text().await?;
215 let entity: Option<ServerPutError> = serde_json::from_str(&content).ok();
216 Err(Error::ResponseError(ResponseContent { status, content, entity }))
217 }
218}
219