hab_rs_api_client/apis/
habpanel_api.rs

1/*
2 * openHAB REST API
3 *
4 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
5 *
6 * The version of the OpenAPI document: 8
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{Error, configuration};
12use crate::apis::ContentType;
13use crate::{apis::ResponseContent, models};
14use async_trait::async_trait;
15#[cfg(feature = "mockall")]
16use mockall::automock;
17use reqwest;
18use serde::{Deserialize, Serialize, de::Error as _};
19use std::sync::Arc;
20
21#[cfg_attr(feature = "mockall", automock)]
22#[async_trait]
23pub trait HabpanelApi: Send + Sync {
24    /// GET /habpanel/gallery/{galleryName}/widgets
25    ///
26    ///
27    async fn get_gallery_widget_list<'gallery_name>(
28        &self,
29        gallery_name: &'gallery_name str,
30    ) -> Result<Vec<models::GalleryWidgetsListItem>, Error<GetGalleryWidgetListError>>;
31
32    /// GET /habpanel/gallery/{galleryName}/widgets/{id}
33    ///
34    ///
35    async fn get_gallery_widgets_item<'gallery_name, 'id>(
36        &self,
37        gallery_name: &'gallery_name str,
38        id: &'id str,
39    ) -> Result<models::GalleryItem, Error<GetGalleryWidgetsItemError>>;
40}
41
42pub struct HabpanelApiClient {
43    configuration: Arc<configuration::Configuration>,
44}
45
46impl HabpanelApiClient {
47    pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
48        Self { configuration }
49    }
50}
51
52#[async_trait]
53impl HabpanelApi for HabpanelApiClient {
54    async fn get_gallery_widget_list<'gallery_name>(
55        &self,
56        gallery_name: &'gallery_name str,
57    ) -> Result<Vec<models::GalleryWidgetsListItem>, Error<GetGalleryWidgetListError>> {
58        let local_var_configuration = &self.configuration;
59
60        let local_var_client = &local_var_configuration.client;
61
62        let local_var_uri_str = format!(
63            "{}/habpanel/gallery/{galleryName}/widgets",
64            local_var_configuration.base_path,
65            galleryName = crate::apis::urlencode(gallery_name)
66        );
67        let mut local_var_req_builder =
68            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
69
70        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
71            local_var_req_builder = local_var_req_builder
72                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
73        }
74
75        let local_var_req = local_var_req_builder.build()?;
76        let local_var_resp = local_var_client.execute(local_var_req).await?;
77
78        let local_var_status = local_var_resp.status();
79        let local_var_content_type = local_var_resp
80            .headers()
81            .get("content-type")
82            .and_then(|v| v.to_str().ok())
83            .unwrap_or("application/octet-stream");
84        let local_var_content_type = super::ContentType::from(local_var_content_type);
85        let local_var_content = local_var_resp.text().await?;
86
87        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
88            match local_var_content_type {
89                ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
90                ContentType::Text => {
91                    return Err(Error::from(serde_json::Error::custom(
92                        "Received `text/plain` content type response that cannot be converted to `Vec&lt;models::GalleryWidgetsListItem&gt;`",
93                    )));
94                }
95                ContentType::Unsupported(local_var_unknown_type) => {
96                    return Err(Error::from(serde_json::Error::custom(format!(
97                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `Vec&lt;models::GalleryWidgetsListItem&gt;`"
98                    ))));
99                }
100            }
101        } else {
102            let local_var_entity: Option<GetGalleryWidgetListError> =
103                serde_json::from_str(&local_var_content).ok();
104            let local_var_error = ResponseContent {
105                status: local_var_status,
106                content: local_var_content,
107                entity: local_var_entity,
108            };
109            Err(Error::ResponseError(local_var_error))
110        }
111    }
112
113    async fn get_gallery_widgets_item<'gallery_name, 'id>(
114        &self,
115        gallery_name: &'gallery_name str,
116        id: &'id str,
117    ) -> Result<models::GalleryItem, Error<GetGalleryWidgetsItemError>> {
118        let local_var_configuration = &self.configuration;
119
120        let local_var_client = &local_var_configuration.client;
121
122        let local_var_uri_str = format!(
123            "{}/habpanel/gallery/{galleryName}/widgets/{id}",
124            local_var_configuration.base_path,
125            galleryName = crate::apis::urlencode(gallery_name),
126            id = crate::apis::urlencode(id)
127        );
128        let mut local_var_req_builder =
129            local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
130
131        if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
132            local_var_req_builder = local_var_req_builder
133                .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
134        }
135
136        let local_var_req = local_var_req_builder.build()?;
137        let local_var_resp = local_var_client.execute(local_var_req).await?;
138
139        let local_var_status = local_var_resp.status();
140        let local_var_content_type = local_var_resp
141            .headers()
142            .get("content-type")
143            .and_then(|v| v.to_str().ok())
144            .unwrap_or("application/octet-stream");
145        let local_var_content_type = super::ContentType::from(local_var_content_type);
146        let local_var_content = local_var_resp.text().await?;
147
148        if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
149            match local_var_content_type {
150                ContentType::Json => serde_json::from_str(&local_var_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::GalleryItem`",
154                    )));
155                }
156                ContentType::Unsupported(local_var_unknown_type) => {
157                    return Err(Error::from(serde_json::Error::custom(format!(
158                        "Received `{local_var_unknown_type}` content type response that cannot be converted to `models::GalleryItem`"
159                    ))));
160                }
161            }
162        } else {
163            let local_var_entity: Option<GetGalleryWidgetsItemError> =
164                serde_json::from_str(&local_var_content).ok();
165            let local_var_error = ResponseContent {
166                status: local_var_status,
167                content: local_var_content,
168                entity: local_var_entity,
169            };
170            Err(Error::ResponseError(local_var_error))
171        }
172    }
173}
174
175/// struct for typed errors of method [HabpanelApi::get_gallery_widget_list]
176#[derive(Debug, Clone, Serialize, Deserialize)]
177#[serde(untagged)]
178pub enum GetGalleryWidgetListError {
179    Status404(),
180    UnknownValue(serde_json::Value),
181}
182
183/// struct for typed errors of method [HabpanelApi::get_gallery_widgets_item]
184#[derive(Debug, Clone, Serialize, Deserialize)]
185#[serde(untagged)]
186pub enum GetGalleryWidgetsItemError {
187    Status404(),
188    UnknownValue(serde_json::Value),
189}