rocie_client/apis/
api_set_auth_unit_property_api.rs1use super::{ContentType, Error, configuration};
22use crate::{apis::ResponseContent, models};
23use reqwest;
24use serde::{Deserialize, Serialize, de::Error as _};
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum RegisterUnitPropertyError {
30 Status401(),
31 Status500(String),
32 UnknownValue(serde_json::Value),
33}
34
35pub async fn register_unit_property(
36 configuration: &configuration::Configuration,
37 unit_property_stub: models::UnitPropertyStub,
38) -> Result<models::UnitPropertyId, Error<RegisterUnitPropertyError>> {
39 let p_body_unit_property_stub = unit_property_stub;
41
42 let uri_str = format!("{}/unit-property/new", configuration.base_path);
43 let mut req_builder = configuration
44 .client
45 .request(reqwest::Method::POST, &uri_str);
46
47 if let Some(ref user_agent) = configuration.user_agent {
48 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
49 }
50 req_builder = req_builder.json(&p_body_unit_property_stub);
51
52 let req = req_builder.build()?;
53 let resp = configuration.client.execute(req).await?;
54
55 let status = resp.status();
56 let content_type = resp
57 .headers()
58 .get("content-type")
59 .and_then(|v| v.to_str().ok())
60 .unwrap_or("application/octet-stream");
61 let content_type = super::ContentType::from(content_type);
62
63 if !status.is_client_error() && !status.is_server_error() {
64 let content = resp.text().await?;
65 match content_type {
66 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
67 ContentType::Text => {
68 return Err(Error::from(serde_json::Error::custom(
69 "Received `text/plain` content type response that cannot be converted to `models::UnitPropertyId`",
70 )));
71 }
72 ContentType::Unsupported(unknown_type) => {
73 return Err(Error::from(serde_json::Error::custom(format!(
74 "Received `{unknown_type}` content type response that cannot be converted to `models::UnitPropertyId`"
75 ))));
76 }
77 }
78 } else {
79 let content = resp.text().await?;
80 let entity: Option<RegisterUnitPropertyError> = serde_json::from_str(&content).ok();
81 Err(Error::ResponseError(ResponseContent {
82 status,
83 content,
84 entity,
85 }))
86 }
87}