revolt_api/apis/
direct_messaging_api.rs

1/*
2 * Revolt API
3 *
4 * Open source user-first chat platform.
5 *
6 * The version of the OpenAPI document: 0.6.5
7 * Contact: contact@revolt.chat
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13
14use crate::apis::ResponseContent;
15use super::{Error, configuration};
16
17
18/// struct for typed errors of method [`fetch_dms_req`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum FetchDmsReqError {
22    DefaultResponse(crate::models::Error),
23    UnknownValue(serde_json::Value),
24}
25
26/// struct for typed errors of method [`open_dm_req`]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum OpenDmReqError {
30    DefaultResponse(crate::models::Error),
31    UnknownValue(serde_json::Value),
32}
33
34
35/// This fetches your direct messages, including any DM and group DM conversations.
36pub async fn fetch_dms_req(configuration: &configuration::Configuration, ) -> Result<Vec<crate::models::Channel>, Error<FetchDmsReqError>> {
37    let local_var_configuration = configuration;
38
39    let local_var_client = &local_var_configuration.client;
40
41    let local_var_uri_str = format!("{}/users/dms", local_var_configuration.base_path);
42    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
43
44    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
45        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
46    }
47    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
48        let local_var_key = local_var_apikey.key.clone();
49        let local_var_value = match local_var_apikey.prefix {
50            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
51            None => local_var_key,
52        };
53        local_var_req_builder = local_var_req_builder.header("x-session-token", local_var_value);
54    };
55
56    let local_var_req = local_var_req_builder.build()?;
57    let local_var_resp = local_var_client.execute(local_var_req).await?;
58
59    let local_var_status = local_var_resp.status();
60    let local_var_content = local_var_resp.text().await?;
61
62    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
63        serde_json::from_str(&local_var_content).map_err(Error::from)
64    } else {
65        let local_var_entity: Option<FetchDmsReqError> = serde_json::from_str(&local_var_content).ok();
66        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
67        Err(Error::ResponseError(local_var_error))
68    }
69}
70
71/// Open a DM with another user.  If the target is oneself, a saved messages channel is returned.
72pub async fn open_dm_req(configuration: &configuration::Configuration, target: &str) -> Result<crate::models::Channel, Error<OpenDmReqError>> {
73    let local_var_configuration = configuration;
74
75    let local_var_client = &local_var_configuration.client;
76
77    let local_var_uri_str = format!("{}/users/{target}/dm", local_var_configuration.base_path, target=crate::apis::urlencode(target));
78    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
79
80    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
81        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
82    }
83    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
84        let local_var_key = local_var_apikey.key.clone();
85        let local_var_value = match local_var_apikey.prefix {
86            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
87            None => local_var_key,
88        };
89        local_var_req_builder = local_var_req_builder.header("x-session-token", local_var_value);
90    };
91
92    let local_var_req = local_var_req_builder.build()?;
93    let local_var_resp = local_var_client.execute(local_var_req).await?;
94
95    let local_var_status = local_var_resp.status();
96    let local_var_content = local_var_resp.text().await?;
97
98    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
99        serde_json::from_str(&local_var_content).map_err(Error::from)
100    } else {
101        let local_var_entity: Option<OpenDmReqError> = serde_json::from_str(&local_var_content).ok();
102        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
103        Err(Error::ResponseError(local_var_error))
104    }
105}
106