Skip to main content

ably_chat_openapi/apis/
occupancy_api.rs

1/*
2 * Ably Chat REST API
3 *
4 * REST API for [Ably Chat](https://ably.com/docs/chat).  This specification describes the HTTP surface of the Ably Chat REST API (path prefix `/chat/v4`) that the official Chat client SDKs use for request/response operations such as sending, editing, deleting and querying messages, message reactions and room occupancy.  ## Scope & provenance Ably does **not** publish an official OpenAPI document for the Chat REST API (the official `ably/open-specs` `platform-v1.yaml` covers only the generic pub/sub REST API: `/channels`, `/push`, `/keys`, `/stats`, `/time`). This document was reconstructed from the `@ably/chat` JavaScript SDK v1.4.0 REST layer (`src/core/chat-api.ts`, `src/core/rest-types.ts`) together with Ably's documented REST conventions (host, authentication, versioning, pagination, error envelope).  It therefore covers the endpoints the client SDK exercises. It is **not** an exhaustive description of every server capability, and there are intentionally no endpoints for creating or deleting rooms: Chat rooms are channel-backed and implicit — they are not provisioned via REST.  ## Realtime vs REST Many Chat features (presence, typing indicators, room reactions, subscribing to live messages and reaction summaries) are delivered over Ably's realtime/pub-sub transport, not this REST API, and are out of scope here. 
5 *
6 * The version of the OpenAPI document: 4
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`get_occupancy`]
19#[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
28/// Returns current occupancy metrics for a room.
29pub async fn get_occupancy(configuration: &configuration::Configuration, room_name: &str, x_ably_version: Option<&str>) -> Result<models::Occupancy, Error<GetOccupancyError>> {
30    // add a prefix to parameters to efficiently prevent name collisions
31    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