ably-chat-openapi 0.1.0

Unofficial generated OpenAPI bindings for the Ably Chat REST API (v4). Not affiliated with or endorsed by Ably. Prefer the `ably-chat-rs` crate.
Documentation
/*
 * Ably Chat REST API
 *
 * 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. 
 *
 * The version of the OpenAPI document: 4
 * 
 * Generated by: https://openapi-generator.tech
 */


use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};


/// struct for typed errors of method [`delete_message_reaction`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteMessageReactionError {
    Status400(models::ErrorResponse),
    Status401(models::ErrorResponse),
    Status404(models::ErrorResponse),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_client_reactions`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetClientReactionsError {
    Status401(models::ErrorResponse),
    Status404(models::ErrorResponse),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`send_message_reaction`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SendMessageReactionError {
    Status400(models::ErrorResponse),
    Status401(models::ErrorResponse),
    Status404(models::ErrorResponse),
    UnknownValue(serde_json::Value),
}


/// Removes a reaction from a message. For `unique` reactions the `name` is not required; for `distinct` and `multiple` reactions the `name` is required. 
pub async fn delete_message_reaction(configuration: &configuration::Configuration, room_name: &str, serial: &str, r#type: models::MessageReactionType, x_ably_version: Option<&str>, name: Option<&str>) -> Result<(), Error<DeleteMessageReactionError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_room_name = room_name;
    let p_path_serial = serial;
    let p_query_type = r#type;
    let p_header_x_ably_version = x_ably_version;
    let p_query_name = name;

    let uri_str = format!("{}/chat/v4/rooms/{roomName}/messages/{serial}/reactions", configuration.base_path, roomName=crate::apis::urlencode(p_path_room_name), serial=crate::apis::urlencode(p_path_serial));
    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);

    req_builder = req_builder.query(&[("type", &p_query_type.to_string())]);
    if let Some(ref param_value) = p_query_name {
        req_builder = req_builder.query(&[("name", &param_value.to_string())]);
    }
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(param_value) = p_header_x_ably_version {
        req_builder = req_builder.header("X-Ably-Version", param_value.to_string());
    }
    if let Some(ref auth_conf) = configuration.basic_auth {
        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
    };
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();

    if !status.is_client_error() && !status.is_server_error() {
        Ok(())
    } else {
        let content = resp.text().await?;
        let entity: Option<DeleteMessageReactionError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Returns the reaction summary filtered to a single client. Useful when a message's reaction summary is clipped (too many reacting clients) and you need to determine whether a specific client has reacted. 
pub async fn get_client_reactions(configuration: &configuration::Configuration, room_name: &str, serial: &str, x_ably_version: Option<&str>, for_client_id: Option<&str>) -> Result<models::MessageReactions, Error<GetClientReactionsError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_room_name = room_name;
    let p_path_serial = serial;
    let p_header_x_ably_version = x_ably_version;
    let p_query_for_client_id = for_client_id;

    let uri_str = format!("{}/chat/v4/rooms/{roomName}/messages/{serial}/client-reactions", configuration.base_path, roomName=crate::apis::urlencode(p_path_room_name), serial=crate::apis::urlencode(p_path_serial));
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = p_query_for_client_id {
        req_builder = req_builder.query(&[("forClientId", &param_value.to_string())]);
    }
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(param_value) = p_header_x_ably_version {
        req_builder = req_builder.header("X-Ably-Version", param_value.to_string());
    }
    if let Some(ref auth_conf) = configuration.basic_auth {
        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
    };
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::MessageReactions`"))),
            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::MessageReactions`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetClientReactionsError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Adds a reaction to a message. Behaviour depends on the reaction `type`: `unique` (at most one reaction per client), `distinct` (at most one of each named reaction per client), `multiple` (repeatable, counted by `count`). 
pub async fn send_message_reaction(configuration: &configuration::Configuration, room_name: &str, serial: &str, send_message_reaction_request: models::SendMessageReactionRequest, x_ably_version: Option<&str>) -> Result<(), Error<SendMessageReactionError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_room_name = room_name;
    let p_path_serial = serial;
    let p_body_send_message_reaction_request = send_message_reaction_request;
    let p_header_x_ably_version = x_ably_version;

    let uri_str = format!("{}/chat/v4/rooms/{roomName}/messages/{serial}/reactions", configuration.base_path, roomName=crate::apis::urlencode(p_path_room_name), serial=crate::apis::urlencode(p_path_serial));
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(param_value) = p_header_x_ably_version {
        req_builder = req_builder.header("X-Ably-Version", param_value.to_string());
    }
    if let Some(ref auth_conf) = configuration.basic_auth {
        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
    };
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };
    req_builder = req_builder.json(&p_body_send_message_reaction_request);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();

    if !status.is_client_error() && !status.is_server_error() {
        Ok(())
    } else {
        let content = resp.text().await?;
        let entity: Option<SendMessageReactionError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}