Skip to main content

rocie_client/apis/
api_set_auth_unit_property_api.rs

1// rocie - An enterprise grocery management system
2//
3// Copyright (C) 2026 Benedikt Peetz <benedikt.peetz@b-peetz.de>
4// SPDX-License-Identifier: GPL-3.0-or-later
5//
6// This file is part of Rocie.
7//
8// You should have received a copy of the License along with this program.
9// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
10
11/*
12 * rocie-server
13 *
14 * An enterprise grocery management system - server
15 *
16 * The version of the OpenAPI document: 0.1.0
17 * Contact: benedikt.peetz@b-peetz.de
18 * Generated by: https://openapi-generator.tech
19 */
20
21use super::{ContentType, Error, configuration};
22use crate::{apis::ResponseContent, models};
23use reqwest;
24use serde::{Deserialize, Serialize, de::Error as _};
25
26/// struct for typed errors of method [`register_unit_property`]
27#[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    // add a prefix to parameters to efficiently prevent name collisions
40    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}