use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::error::Result;
use crate::http::HttpClient;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SendTokenBody {
pub recipient: String,
pub channel: String,
}
impl SendTokenBody {
pub fn new<R, C>(recipient: R, channel: C) -> Self
where
R: Into<String>,
C: Into<String>,
{
Self {
recipient: recipient.into(),
channel: channel.into(),
}
}
pub fn email<S: Into<String>>(recipient: S) -> Self {
Self::new(recipient, "email")
}
pub fn whatsapp<S: Into<String>>(recipient: S) -> Self {
Self::new(recipient, "whatsapp")
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SendTokenResult {
pub document: serde_json::Value,
pub channel: String,
pub recipient: String,
}
#[derive(Debug)]
pub struct PublicApi<'a> {
http: &'a HttpClient,
}
impl<'a> PublicApi<'a> {
pub(crate) fn new(http: &'a HttpClient) -> Self {
Self { http }
}
pub async fn document<S: AsRef<str>>(&self, document_id: S) -> Result<serde_json::Value> {
let path = format!("public/documents/{}", document_id.as_ref());
let req = self.http.request(Method::GET, &path)?;
self.http.send_envelope(req).await
}
pub async fn send_token<S: AsRef<str>>(
&self,
document_id: S,
body: &SendTokenBody,
) -> Result<SendTokenResult> {
let path = format!("public/documents/{}/send-token", document_id.as_ref());
let req = self.http.request(Method::PUT, &path)?.json(body);
self.http.send_envelope(req).await
}
}