Skip to main content

rocie_client/apis/
mod.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
11use std::error;
12use std::fmt;
13
14#[derive(Debug, Clone)]
15pub struct ResponseContent<T> {
16    pub status: reqwest::StatusCode,
17    pub content: String,
18    pub entity: Option<T>,
19}
20
21#[derive(Debug)]
22pub enum Error<T> {
23    Reqwest(reqwest::Error),
24    Serde(serde_json::Error),
25    Io(std::io::Error),
26    ResponseError(ResponseContent<T>),
27}
28
29impl<T> fmt::Display for Error<T> {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        let (module, e) = match self {
32            Error::Reqwest(e) => ("reqwest", e.to_string()),
33            Error::Serde(e) => ("serde", e.to_string()),
34            Error::Io(e) => ("IO", e.to_string()),
35            Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
36        };
37        write!(f, "error in {}: {}", module, e)
38    }
39}
40
41impl<T: fmt::Debug> error::Error for Error<T> {
42    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
43        Some(match self {
44            Error::Reqwest(e) => e,
45            Error::Serde(e) => e,
46            Error::Io(e) => e,
47            Error::ResponseError(_) => return None,
48        })
49    }
50}
51
52impl<T> From<reqwest::Error> for Error<T> {
53    fn from(e: reqwest::Error) -> Self {
54        Error::Reqwest(e)
55    }
56}
57
58impl<T> From<serde_json::Error> for Error<T> {
59    fn from(e: serde_json::Error) -> Self {
60        Error::Serde(e)
61    }
62}
63
64impl<T> From<std::io::Error> for Error<T> {
65    fn from(e: std::io::Error) -> Self {
66        Error::Io(e)
67    }
68}
69
70pub fn urlencode<T: AsRef<str>>(s: T) -> String {
71    ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
72}
73
74pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
75    if let serde_json::Value::Object(object) = value {
76        let mut params = vec![];
77
78        for (key, value) in object {
79            match value {
80                serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
81                    &format!("{}[{}]", prefix, key),
82                    value,
83                )),
84                serde_json::Value::Array(array) => {
85                    for (i, value) in array.iter().enumerate() {
86                        params.append(&mut parse_deep_object(
87                            &format!("{}[{}][{}]", prefix, key, i),
88                            value,
89                        ));
90                    }
91                }
92                serde_json::Value::String(s) => {
93                    params.push((format!("{}[{}]", prefix, key), s.clone()))
94                }
95                _ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
96            }
97        }
98
99        return params;
100    }
101
102    unimplemented!("Only objects are supported with style=deepObject")
103}
104
105/// Internal use only
106/// A content type supported by this client.
107#[allow(dead_code)]
108enum ContentType {
109    Json,
110    Text,
111    Unsupported(String),
112}
113
114impl From<&str> for ContentType {
115    fn from(content_type: &str) -> Self {
116        if content_type.starts_with("application") && content_type.contains("json") {
117            return Self::Json;
118        } else if content_type.starts_with("text/plain") {
119            return Self::Text;
120        } else {
121            return Self::Unsupported(content_type.to_string());
122        }
123    }
124}
125
126pub mod api_get_auth_inventory_api;
127pub mod api_get_auth_product_api;
128pub mod api_get_auth_product_parent_api;
129pub mod api_get_auth_recipe_api;
130pub mod api_get_auth_recipe_parent_api;
131pub mod api_get_auth_unit_api;
132pub mod api_get_auth_unit_property_api;
133pub mod api_get_auth_user_api;
134pub mod api_get_no_auth_state_api;
135pub mod api_set_auth_barcode_api;
136pub mod api_set_auth_product_api;
137pub mod api_set_auth_product_parent_api;
138pub mod api_set_auth_recipe_api;
139pub mod api_set_auth_recipe_parent_api;
140pub mod api_set_auth_unit_api;
141pub mod api_set_auth_unit_property_api;
142pub mod api_set_auth_user_api;
143pub mod api_set_no_auth_user_api;
144
145pub mod configuration;