use reqwest::Method;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::{client::Sendry, error::Error, Page, PaginationParams};
#[derive(Debug, Clone)]
pub struct Inbound {
client: Sendry,
}
impl Inbound {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn list(&self, params: PaginationParams) -> Result<Page<InboundEmail>, Error> {
let q = params.to_query();
self.client
.request(self.client.build::<()>(Method::GET, "/v1/inbound", &q, None))
.await
}
pub async fn get(&self, id: &str) -> Result<InboundEmail, Error> {
self.client
.request(self.client.build::<()>(
Method::GET,
&format!("/v1/inbound/{id}"),
&[],
None,
))
.await
}
pub async fn get_config(&self) -> Result<InboundConfig, Error> {
self.client
.request(self.client.build::<()>(
Method::GET,
"/v1/inbound/config",
&[],
None,
))
.await
}
pub async fn update_config(
&self,
params: UpdateInboundConfig,
) -> Result<InboundConfig, Error> {
self.client
.request(self.client.build(
Method::PUT,
"/v1/inbound/config",
&[],
Some(¶ms),
))
.await
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct InboundEmailAttachment {
pub filename: String,
#[serde(rename = "contentType")]
pub content_type: String,
pub size: u64,
#[serde(rename = "contentId", default)]
pub content_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct InboundEmail {
pub id: String,
pub from: String,
pub to: Vec<String>,
#[serde(default)]
pub cc: Vec<String>,
pub subject: Option<String>,
pub text: Option<String>,
pub html: Option<String>,
pub headers: Option<HashMap<String, String>>,
#[serde(default)]
pub attachments: Vec<InboundEmailAttachment>,
pub webhook_delivered: bool,
pub created_at: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct InboundConfig {
pub url: Option<String>,
pub secret: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct UpdateInboundConfig {
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub secret: Option<String>,
}