use async_trait::async_trait;
#[cfg(feature = "mockall")]
use mockall::automock;
use reqwest;
use std::sync::Arc;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration};
use crate::apis::ContentType;
#[cfg_attr(feature = "mockall", automock)]
#[async_trait]
pub trait MessagesApi: Send + Sync {
async fn delete_by_transaction<'transaction_id>(&self, transaction_id: &'transaction_id str) -> Result<(), Error<DeleteByTransactionError>>;
async fn delete_message<'id>(&self, id: &'id str) -> Result<(), Error<DeleteMessageError>>;
async fn list_messages<'limit, 'after, 'before, 'channel, 'subscriber_id, 'q>(&self, limit: Option<u32>, after: Option<&'after str>, before: Option<&'before str>, channel: Option<&'channel str>, subscriber_id: Option<&'subscriber_id str>, q: Option<&'q str>) -> Result<models::CursorPaginatedMessageRecord, Error<ListMessagesError>>;
}
pub struct MessagesApiClient {
configuration: Arc<configuration::Configuration>
}
impl MessagesApiClient {
pub fn new(configuration: Arc<configuration::Configuration>) -> Self {
Self { configuration }
}
}
#[async_trait]
impl MessagesApi for MessagesApiClient {
async fn delete_by_transaction<'transaction_id>(&self, transaction_id: &'transaction_id str) -> Result<(), Error<DeleteByTransactionError>> {
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!("{}/v1/messages/transaction/{transaction_id}/", local_var_configuration.base_path, transaction_id=crate::apis::urlencode(transaction_id));
let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
Ok(())
} else {
let local_var_entity: Option<DeleteByTransactionError> = serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
Err(Error::ResponseError(local_var_error))
}
}
async fn delete_message<'id>(&self, id: &'id str) -> Result<(), Error<DeleteMessageError>> {
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!("{}/v1/messages/{id}/", local_var_configuration.base_path, id=crate::apis::urlencode(id));
let mut local_var_req_builder = local_var_client.request(reqwest::Method::DELETE, local_var_uri_str.as_str());
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
Ok(())
} else {
let local_var_entity: Option<DeleteMessageError> = serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
Err(Error::ResponseError(local_var_error))
}
}
async fn list_messages<'limit, 'after, 'before, 'channel, 'subscriber_id, 'q>(&self, limit: Option<u32>, after: Option<&'after str>, before: Option<&'before str>, channel: Option<&'channel str>, subscriber_id: Option<&'subscriber_id str>, q: Option<&'q str>) -> Result<models::CursorPaginatedMessageRecord, Error<ListMessagesError>> {
let local_var_configuration = &self.configuration;
let local_var_client = &local_var_configuration.client;
let local_var_uri_str = format!("{}/v1/messages/", local_var_configuration.base_path);
let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
if let Some(ref param_value) = limit {
local_var_req_builder = local_var_req_builder.query(&[("limit", ¶m_value.to_string())]);
}
if let Some(ref param_value) = after {
local_var_req_builder = local_var_req_builder.query(&[("after", ¶m_value.to_string())]);
}
if let Some(ref param_value) = before {
local_var_req_builder = local_var_req_builder.query(&[("before", ¶m_value.to_string())]);
}
if let Some(ref param_value) = channel {
local_var_req_builder = local_var_req_builder.query(&[("channel", ¶m_value.to_string())]);
}
if let Some(ref param_value) = subscriber_id {
local_var_req_builder = local_var_req_builder.query(&[("subscriber_id", ¶m_value.to_string())]);
}
if let Some(ref param_value) = q {
local_var_req_builder = local_var_req_builder.query(&[("q", ¶m_value.to_string())]);
}
if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content_type = local_var_resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let local_var_content_type = super::ContentType::from(local_var_content_type);
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
match local_var_content_type {
ContentType::Json => serde_json::from_str(&local_var_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::CursorPaginatedMessageRecord`"))),
ContentType::Unsupported(local_var_unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{local_var_unknown_type}` content type response that cannot be converted to `models::CursorPaginatedMessageRecord`")))),
}
} else {
let local_var_entity: Option<ListMessagesError> = serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
Err(Error::ResponseError(local_var_error))
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteByTransactionError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteMessageError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListMessagesError {
UnknownValue(serde_json::Value),
}