use reqwest::multipart::{Form, Part};
use serde::Serialize;
use std::io;
#[derive(Debug, Clone, Serialize)]
pub struct AttachmentMessage {
pub app_token: String,
pub user_key: String,
pub message: String,
pub attachment: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url_title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub retry: Option<String>, #[serde(skip_serializing_if = "Option::is_none")]
pub expire: Option<String>, #[serde(skip_serializing_if = "Option::is_none")]
pub sound: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<String>, #[serde(skip_serializing_if = "Option::is_none")]
pub device: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ttl: Option<u32>,
}
impl AttachmentMessage {
pub fn into_form(self) -> Result<reqwest::blocking::multipart::Form, std::io::Error> {
let mut form: reqwest::blocking::multipart::Form = reqwest::blocking::multipart::Form::new()
.text("token", self.app_token.clone())
.text("user", self.user_key.clone())
.text("message", self.message.clone())
.text("title", self.title.clone().unwrap_or(String::from("")))
.text("url", self.url.clone().unwrap_or(String::from("")))
.text("url_title", self.url_title.clone().unwrap_or(String::from("")))
.text("priority", self.priority.unwrap_or(String::from("")))
.text("retry", self.retry.clone().unwrap_or(String::from("")))
.text("expire", self.expire.clone().unwrap_or(String::from("")))
.text("sound", self.sound.clone().unwrap_or(String::from("")))
.text("timestamp", self.timestamp.unwrap_or(String::from("")))
.text("device", self.device.clone().unwrap_or(String::from("")));
if self.ttl.is_some() {
form = form.text("ttl", self.ttl.unwrap_or(999).to_string());
}
let attachment_part = reqwest::blocking::multipart::Part::bytes(std::fs::read(&self.attachment)?)
.file_name(self.attachment.clone());
Ok(form.part("attachment", attachment_part))
}
pub async fn into_form_async(self) -> Result<Form, io::Error> {
let mut form = Form::new()
.text("token", self.app_token.clone())
.text("user", self.user_key.clone())
.text("message", self.message.clone())
.text("title", self.title.clone().unwrap_or(String::from("")))
.text("url", self.url.clone().unwrap_or(String::from("")))
.text("url_title", self.url_title.clone().unwrap_or(String::from("")))
.text("priority", self.priority.unwrap_or(String::from("")))
.text("sound", self.sound.clone().unwrap_or(String::from("")))
.text("timestamp", self.timestamp.unwrap_or(String::from("")))
.text("device", self.device.clone().unwrap_or(String::from("")));
if self.ttl.is_some() {
form = form.text("ttl", self.ttl.unwrap_or(999).to_string());
}
let file_bytes = tokio::fs::read(&self.attachment).await?;
let part = Part::bytes(file_bytes)
.file_name(self.attachment.clone());
Ok(form.part("attachment", part))
}
}
impl Default for AttachmentMessage {
fn default() -> Self {
Self {
app_token: "".into(),
user_key: "".into(),
message: "".into(),
attachment: "".into(),
title: None,
url: None,
url_title: None,
priority: None,
retry: None,
expire: None,
sound: None,
timestamp: None,
device: None,
ttl: None,
}
}
}