ably_chat_openapi/apis/
occupancy_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetOccupancyError {
22 Status401(models::ErrorResponse),
23 Status404(models::ErrorResponse),
24 UnknownValue(serde_json::Value),
25}
26
27
28pub async fn get_occupancy(configuration: &configuration::Configuration, room_name: &str, x_ably_version: Option<&str>) -> Result<models::Occupancy, Error<GetOccupancyError>> {
30 let p_path_room_name = room_name;
32 let p_header_x_ably_version = x_ably_version;
33
34 let uri_str = format!("{}/chat/v4/rooms/{roomName}/occupancy", configuration.base_path, roomName=crate::apis::urlencode(p_path_room_name));
35 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
36
37 if let Some(ref user_agent) = configuration.user_agent {
38 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
39 }
40 if let Some(param_value) = p_header_x_ably_version {
41 req_builder = req_builder.header("X-Ably-Version", param_value.to_string());
42 }
43 if let Some(ref auth_conf) = configuration.basic_auth {
44 req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
45 };
46 if let Some(ref token) = configuration.bearer_access_token {
47 req_builder = req_builder.bearer_auth(token.to_owned());
48 };
49
50 let req = req_builder.build()?;
51 let resp = configuration.client.execute(req).await?;
52
53 let status = resp.status();
54 let content_type = resp
55 .headers()
56 .get("content-type")
57 .and_then(|v| v.to_str().ok())
58 .unwrap_or("application/octet-stream");
59 let content_type = super::ContentType::from(content_type);
60
61 if !status.is_client_error() && !status.is_server_error() {
62 let content = resp.text().await?;
63 match content_type {
64 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
65 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Occupancy`"))),
66 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::Occupancy`")))),
67 }
68 } else {
69 let content = resp.text().await?;
70 let entity: Option<GetOccupancyError> = serde_json::from_str(&content).ok();
71 Err(Error::ResponseError(ResponseContent { status, content, entity }))
72 }
73}
74