pub mod error;
pub mod types;
mod core;
mod utils;
use serde_json::json;
use core::{crypto, http::HttpClient, payload::verify_payload};
pub use error::CatzError;
use types::{
Channel,
MessageType,
Template,
SendInput,
EnvValues,
};
pub struct CatzConnect;
impl CatzConnect {
pub async fn send(input: SendInput, env: Option<EnvValues>) -> Result<serde_json::Value, CatzError> {
verify_payload(&input)?;
let final_payload = match (
&input.channel,
&input.message_type,
&input.template,
) {
(Channel::Email, MessageType::Verification, Template::Otp) => json!({
"message_type": format!("{:?}", input.message_type),
"channel": format!("{:?}", input.channel),
"template": format!("{:?}", input.template),
"identity": input.identity,
"to": input.payload.to,
"otp": input.payload.otp,
}),
(Channel::Email, MessageType::Transactional, Template::Custom) => json!({
"message_type": format!("{:?}", input.message_type),
"channel": format!("{:?}", input.channel),
"template": format!("{:?}", input.template),
"identity": input.identity,
"to": input.payload.to,
"subject": input.payload.subject,
"body": input.payload.body,
}),
_ => {
return Err(CatzError::Validation(
"Unsupported channel/message_type/template combination".into(),
));
}
};
let encrypted = crypto::encrypt(&final_payload, env.clone())?;
let client = HttpClient::from_env(env)?;
let response = client.post("/sdk/send", &encrypted).await?;
Ok(response)
}
}