Skip to main content

rocie_client/apis/
api_set_no_auth_user_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 [`login`]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum LoginError {
30    Status403(),
31    Status404(),
32    Status500(String),
33    UnknownValue(serde_json::Value),
34}
35
36/// struct for typed errors of method [`logout`]
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[serde(untagged)]
39pub enum LogoutError {
40    Status500(String),
41    UnknownValue(serde_json::Value),
42}
43
44/// struct for typed errors of method [`provision`]
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(untagged)]
47pub enum ProvisionError {
48    Status403(),
49    Status500(String),
50    UnknownValue(serde_json::Value),
51}
52
53pub async fn login(
54    configuration: &configuration::Configuration,
55    login_info: models::LoginInfo,
56) -> Result<(), Error<LoginError>> {
57    // add a prefix to parameters to efficiently prevent name collisions
58    let p_body_login_info = login_info;
59
60    let uri_str = format!("{}/login", configuration.base_path);
61    let mut req_builder = configuration
62        .client
63        .request(reqwest::Method::POST, &uri_str);
64
65    if let Some(ref user_agent) = configuration.user_agent {
66        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
67    }
68    req_builder = req_builder.json(&p_body_login_info);
69
70    let req = req_builder.build()?;
71    let resp = configuration.client.execute(req).await?;
72
73    let status = resp.status();
74
75    if !status.is_client_error() && !status.is_server_error() {
76        Ok(())
77    } else {
78        let content = resp.text().await?;
79        let entity: Option<LoginError> = serde_json::from_str(&content).ok();
80        Err(Error::ResponseError(ResponseContent {
81            status,
82            content,
83            entity,
84        }))
85    }
86}
87
88pub async fn logout(
89    configuration: &configuration::Configuration,
90) -> Result<(), Error<LogoutError>> {
91    let uri_str = format!("{}/logout", configuration.base_path);
92    let mut req_builder = configuration
93        .client
94        .request(reqwest::Method::POST, &uri_str);
95
96    if let Some(ref user_agent) = configuration.user_agent {
97        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
98    }
99
100    let req = req_builder.build()?;
101    let resp = configuration.client.execute(req).await?;
102
103    let status = resp.status();
104
105    if !status.is_client_error() && !status.is_server_error() {
106        Ok(())
107    } else {
108        let content = resp.text().await?;
109        let entity: Option<LogoutError> = serde_json::from_str(&content).ok();
110        Err(Error::ResponseError(ResponseContent {
111            status,
112            content,
113            entity,
114        }))
115    }
116}
117
118/// This only works, if no users exist yet.
119pub async fn provision(
120    configuration: &configuration::Configuration,
121    provision_info: models::ProvisionInfo,
122) -> Result<models::UserId, Error<ProvisionError>> {
123    // add a prefix to parameters to efficiently prevent name collisions
124    let p_body_provision_info = provision_info;
125
126    let uri_str = format!("{}/provision", configuration.base_path);
127    let mut req_builder = configuration
128        .client
129        .request(reqwest::Method::POST, &uri_str);
130
131    if let Some(ref user_agent) = configuration.user_agent {
132        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
133    }
134    req_builder = req_builder.json(&p_body_provision_info);
135
136    let req = req_builder.build()?;
137    let resp = configuration.client.execute(req).await?;
138
139    let status = resp.status();
140    let content_type = resp
141        .headers()
142        .get("content-type")
143        .and_then(|v| v.to_str().ok())
144        .unwrap_or("application/octet-stream");
145    let content_type = super::ContentType::from(content_type);
146
147    if !status.is_client_error() && !status.is_server_error() {
148        let content = resp.text().await?;
149        match content_type {
150            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
151            ContentType::Text => {
152                return Err(Error::from(serde_json::Error::custom(
153                    "Received `text/plain` content type response that cannot be converted to `models::UserId`",
154                )));
155            }
156            ContentType::Unsupported(unknown_type) => {
157                return Err(Error::from(serde_json::Error::custom(format!(
158                    "Received `{unknown_type}` content type response that cannot be converted to `models::UserId`"
159                ))));
160            }
161        }
162    } else {
163        let content = resp.text().await?;
164        let entity: Option<ProvisionError> = serde_json::from_str(&content).ok();
165        Err(Error::ResponseError(ResponseContent {
166            status,
167            content,
168            entity,
169        }))
170    }
171}