pushover_rs/pushover/data/
attachment_message.rs1use reqwest::multipart::{Form, Part};
2use serde::Serialize;
3use std::io;
4
5#[derive(Debug, Clone, Serialize)]
6pub struct AttachmentMessage {
12 pub app_token: String,
15 pub user_key: String,
17 pub message: String,
19 pub attachment: String,
21
22 #[serde(skip_serializing_if = "Option::is_none")]
25 pub title: Option<String>,
26 #[serde(skip_serializing_if = "Option::is_none")]
28 pub url: Option<String>,
29 #[serde(skip_serializing_if = "Option::is_none")]
31 pub url_title: Option<String>,
32 #[serde(skip_serializing_if = "Option::is_none")]
34 pub priority: Option<String>,
35 #[serde(skip_serializing_if = "Option::is_none")]
37 pub retry: Option<String>, #[serde(skip_serializing_if = "Option::is_none")]
40 pub expire: Option<String>, #[serde(skip_serializing_if = "Option::is_none")]
43 pub sound: Option<String>,
44 #[serde(skip_serializing_if = "Option::is_none")]
46 pub timestamp: Option<String>, #[serde(skip_serializing_if = "Option::is_none")]
49 pub device: Option<String>,
50 #[serde(skip_serializing_if = "Option::is_none")]
53 pub ttl: Option<u32>,
54}
55
56impl AttachmentMessage {
57 pub fn into_form(self) -> Result<reqwest::blocking::multipart::Form, std::io::Error> {
58 let mut form: reqwest::blocking::multipart::Form = reqwest::blocking::multipart::Form::new()
59 .text("token", self.app_token.clone())
60 .text("user", self.user_key.clone())
61 .text("message", self.message.clone())
62 .text("title", self.title.clone().unwrap_or(String::from("")))
63 .text("url", self.url.clone().unwrap_or(String::from("")))
64 .text("url_title", self.url_title.clone().unwrap_or(String::from("")))
65 .text("priority", self.priority.unwrap_or(String::from("")))
66 .text("retry", self.retry.clone().unwrap_or(String::from("")))
67 .text("expire", self.expire.clone().unwrap_or(String::from("")))
68 .text("sound", self.sound.clone().unwrap_or(String::from("")))
69 .text("timestamp", self.timestamp.unwrap_or(String::from("")))
70 .text("device", self.device.clone().unwrap_or(String::from("")));
71 if self.ttl.is_some() {
73 form = form.text("ttl", self.ttl.unwrap_or(999).to_string());
74 }
75 let attachment_part = reqwest::blocking::multipart::Part::bytes(std::fs::read(&self.attachment)?)
76 .file_name(self.attachment.clone());
77 Ok(form.part("attachment", attachment_part))
78 }
79
80 pub async fn into_form_async(self) -> Result<Form, io::Error> {
81 let mut form = Form::new()
82 .text("token", self.app_token.clone())
83 .text("user", self.user_key.clone())
84 .text("message", self.message.clone())
85 .text("title", self.title.clone().unwrap_or(String::from("")))
86 .text("url", self.url.clone().unwrap_or(String::from("")))
87 .text("url_title", self.url_title.clone().unwrap_or(String::from("")))
88 .text("priority", self.priority.unwrap_or(String::from("")))
89 .text("sound", self.sound.clone().unwrap_or(String::from("")))
90 .text("timestamp", self.timestamp.unwrap_or(String::from("")))
91 .text("device", self.device.clone().unwrap_or(String::from("")));
92 if self.ttl.is_some() {
93 form = form.text("ttl", self.ttl.unwrap_or(999).to_string());
94 }
95 let file_bytes = tokio::fs::read(&self.attachment).await?;
96 let part = Part::bytes(file_bytes)
97 .file_name(self.attachment.clone());
98 Ok(form.part("attachment", part))
99 }
100}
101
102impl Default for AttachmentMessage {
103 fn default() -> Self {
104 Self {
105 app_token: "".into(),
106 user_key: "".into(),
107 message: "".into(),
108 attachment: "".into(),
109 title: None,
110 url: None,
111 url_title: None,
112 priority: None,
113 retry: None,
114 expire: None,
115 sound: None,
116 timestamp: None,
117 device: None,
118 ttl: None,
119 }
120 }
121}