hab_rs_api_client/apis/
actions_api.rs1use 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 ActionsApi: Send + Sync {
24 async fn execute_thing_action<'thing_uid, 'action_uid, 'accept_language, 'request_body>(
28 &self,
29 thing_uid: &'thing_uid str,
30 action_uid: &'action_uid str,
31 accept_language: Option<&'accept_language str>,
32 request_body: Option<std::collections::HashMap<String, serde_json::Value>>,
33 ) -> Result<String, Error<ExecuteThingActionError>>;
34
35 async fn get_available_actions_for_thing<'thing_uid, 'accept_language>(
39 &self,
40 thing_uid: &'thing_uid str,
41 accept_language: Option<&'accept_language str>,
42 ) -> Result<Vec<models::ThingActionDto>, Error<GetAvailableActionsForThingError>>;
43}
44
45pub struct ActionsApiClient {
46 configuration: Arc<configuration::Configuration>,
47}
48
49impl ActionsApiClient {
50 pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
51 Self { configuration }
52 }
53}
54
55#[async_trait]
56impl ActionsApi for ActionsApiClient {
57 async fn execute_thing_action<'thing_uid, 'action_uid, 'accept_language, 'request_body>(
58 &self,
59 thing_uid: &'thing_uid str,
60 action_uid: &'action_uid str,
61 accept_language: Option<&'accept_language str>,
62 request_body: Option<std::collections::HashMap<String, serde_json::Value>>,
63 ) -> Result<String, Error<ExecuteThingActionError>> {
64 let local_var_configuration = &self.configuration;
65
66 let local_var_client = &local_var_configuration.client;
67
68 let local_var_uri_str = format!(
69 "{}/actions/{thingUID}/{actionUid}",
70 local_var_configuration.base_path,
71 thingUID = crate::apis::urlencode(thing_uid),
72 actionUid = crate::apis::urlencode(action_uid)
73 );
74 let mut local_var_req_builder =
75 local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
76
77 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
78 local_var_req_builder = local_var_req_builder
79 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
80 }
81 if let Some(local_var_param_value) = accept_language {
82 local_var_req_builder =
83 local_var_req_builder.header("Accept-Language", local_var_param_value.to_string());
84 }
85 local_var_req_builder = local_var_req_builder.json(&request_body);
86
87 let local_var_req = local_var_req_builder.build()?;
88 let local_var_resp = local_var_client.execute(local_var_req).await?;
89
90 let local_var_status = local_var_resp.status();
91 let local_var_content_type = local_var_resp
92 .headers()
93 .get("content-type")
94 .and_then(|v| v.to_str().ok())
95 .unwrap_or("application/octet-stream");
96 let local_var_content_type = super::ContentType::from(local_var_content_type);
97 let local_var_content = local_var_resp.text().await?;
98
99 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
100 match local_var_content_type {
101 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
102 ContentType::Text => {
103 return Err(Error::from(serde_json::Error::custom(
104 "Received `text/plain` content type response that cannot be converted to `String`",
105 )));
106 }
107 ContentType::Unsupported(local_var_unknown_type) => {
108 return Err(Error::from(serde_json::Error::custom(format!(
109 "Received `{local_var_unknown_type}` content type response that cannot be converted to `String`"
110 ))));
111 }
112 }
113 } else {
114 let local_var_entity: Option<ExecuteThingActionError> =
115 serde_json::from_str(&local_var_content).ok();
116 let local_var_error = ResponseContent {
117 status: local_var_status,
118 content: local_var_content,
119 entity: local_var_entity,
120 };
121 Err(Error::ResponseError(local_var_error))
122 }
123 }
124
125 async fn get_available_actions_for_thing<'thing_uid, 'accept_language>(
126 &self,
127 thing_uid: &'thing_uid str,
128 accept_language: Option<&'accept_language str>,
129 ) -> Result<Vec<models::ThingActionDto>, Error<GetAvailableActionsForThingError>> {
130 let local_var_configuration = &self.configuration;
131
132 let local_var_client = &local_var_configuration.client;
133
134 let local_var_uri_str = format!(
135 "{}/actions/{thingUID}",
136 local_var_configuration.base_path,
137 thingUID = crate::apis::urlencode(thing_uid)
138 );
139 let mut local_var_req_builder =
140 local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
141
142 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
143 local_var_req_builder = local_var_req_builder
144 .header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
145 }
146 if let Some(local_var_param_value) = accept_language {
147 local_var_req_builder =
148 local_var_req_builder.header("Accept-Language", local_var_param_value.to_string());
149 }
150
151 let local_var_req = local_var_req_builder.build()?;
152 let local_var_resp = local_var_client.execute(local_var_req).await?;
153
154 let local_var_status = local_var_resp.status();
155 let local_var_content_type = local_var_resp
156 .headers()
157 .get("content-type")
158 .and_then(|v| v.to_str().ok())
159 .unwrap_or("application/octet-stream");
160 let local_var_content_type = super::ContentType::from(local_var_content_type);
161 let local_var_content = local_var_resp.text().await?;
162
163 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
164 match local_var_content_type {
165 ContentType::Json => serde_json::from_str(&local_var_content).map_err(Error::from),
166 ContentType::Text => {
167 return Err(Error::from(serde_json::Error::custom(
168 "Received `text/plain` content type response that cannot be converted to `Vec<models::ThingActionDto>`",
169 )));
170 }
171 ContentType::Unsupported(local_var_unknown_type) => {
172 return Err(Error::from(serde_json::Error::custom(format!(
173 "Received `{local_var_unknown_type}` content type response that cannot be converted to `Vec<models::ThingActionDto>`"
174 ))));
175 }
176 }
177 } else {
178 let local_var_entity: Option<GetAvailableActionsForThingError> =
179 serde_json::from_str(&local_var_content).ok();
180 let local_var_error = ResponseContent {
181 status: local_var_status,
182 content: local_var_content,
183 entity: local_var_entity,
184 };
185 Err(Error::ResponseError(local_var_error))
186 }
187 }
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
192#[serde(untagged)]
193pub enum ExecuteThingActionError {
194 Status404(),
195 Status500(),
196 UnknownValue(serde_json::Value),
197}
198
199#[derive(Debug, Clone, Serialize, Deserialize)]
201#[serde(untagged)]
202pub enum GetAvailableActionsForThingError {
203 Status404(),
204 UnknownValue(serde_json::Value),
205}