harvest_api/request/
create_estimate_message.rs1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4pub struct CreateEstimateMessageRequest<'a> {
8 pub(crate) client: &'a HarvestClient,
9 pub estimate_id: String,
10 pub event_type: Option<String>,
11 pub recipients: Option<Vec<serde_json::Value>>,
12 pub subject: Option<String>,
13 pub body: Option<String>,
14 pub send_me_a_copy: Option<bool>,
15}
16impl<'a> CreateEstimateMessageRequest<'a> {
17 pub async fn send(self) -> anyhow::Result<EstimateMessage> {
18 let mut r = self
19 .client
20 .client
21 .post(
22 &format!(
23 "/estimates/{estimate_id}/messages", estimate_id = self.estimate_id
24 ),
25 );
26 if let Some(ref unwrapped) = self.event_type {
27 r = r.push_json(json!({ "event_type" : unwrapped }));
28 }
29 if let Some(ref unwrapped) = self.recipients {
30 r = r.push_json(json!({ "recipients" : unwrapped }));
31 }
32 if let Some(ref unwrapped) = self.subject {
33 r = r.push_json(json!({ "subject" : unwrapped }));
34 }
35 if let Some(ref unwrapped) = self.body {
36 r = r.push_json(json!({ "body" : unwrapped }));
37 }
38 if let Some(ref unwrapped) = self.send_me_a_copy {
39 r = r.push_json(json!({ "send_me_a_copy" : unwrapped }));
40 }
41 r = self.client.authenticate(r);
42 let res = r.send().await.unwrap().error_for_status();
43 match res {
44 Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
45 Err(res) => {
46 let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
47 Err(anyhow::anyhow!("{:?}", text))
48 }
49 }
50 }
51 pub fn event_type(mut self, event_type: &str) -> Self {
52 self.event_type = Some(event_type.to_owned());
53 self
54 }
55 pub fn recipients(mut self, recipients: Vec<serde_json::Value>) -> Self {
56 self.recipients = Some(recipients);
57 self
58 }
59 pub fn subject(mut self, subject: &str) -> Self {
60 self.subject = Some(subject.to_owned());
61 self
62 }
63 pub fn body(mut self, body: &str) -> Self {
64 self.body = Some(body.to_owned());
65 self
66 }
67 pub fn send_me_a_copy(mut self, send_me_a_copy: bool) -> Self {
68 self.send_me_a_copy = Some(send_me_a_copy);
69 self
70 }
71}