harvest_api/request/
create_invoice_message.rs1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4pub struct CreateInvoiceMessageRequest<'a> {
8 pub(crate) client: &'a HarvestClient,
9 pub invoice_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 include_link_to_client_invoice: Option<bool>,
15 pub attach_pdf: Option<bool>,
16 pub send_me_a_copy: Option<bool>,
17 pub thank_you: Option<bool>,
18}
19impl<'a> CreateInvoiceMessageRequest<'a> {
20 pub async fn send(self) -> anyhow::Result<InvoiceMessage> {
21 let mut r = self
22 .client
23 .client
24 .post(
25 &format!("/invoices/{invoice_id}/messages", invoice_id = self.invoice_id),
26 );
27 if let Some(ref unwrapped) = self.event_type {
28 r = r.push_json(json!({ "event_type" : unwrapped }));
29 }
30 if let Some(ref unwrapped) = self.recipients {
31 r = r.push_json(json!({ "recipients" : unwrapped }));
32 }
33 if let Some(ref unwrapped) = self.subject {
34 r = r.push_json(json!({ "subject" : unwrapped }));
35 }
36 if let Some(ref unwrapped) = self.body {
37 r = r.push_json(json!({ "body" : unwrapped }));
38 }
39 if let Some(ref unwrapped) = self.include_link_to_client_invoice {
40 r = r.push_json(json!({ "include_link_to_client_invoice" : unwrapped }));
41 }
42 if let Some(ref unwrapped) = self.attach_pdf {
43 r = r.push_json(json!({ "attach_pdf" : unwrapped }));
44 }
45 if let Some(ref unwrapped) = self.send_me_a_copy {
46 r = r.push_json(json!({ "send_me_a_copy" : unwrapped }));
47 }
48 if let Some(ref unwrapped) = self.thank_you {
49 r = r.push_json(json!({ "thank_you" : unwrapped }));
50 }
51 r = self.client.authenticate(r);
52 let res = r.send().await.unwrap().error_for_status();
53 match res {
54 Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
55 Err(res) => {
56 let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
57 Err(anyhow::anyhow!("{:?}", text))
58 }
59 }
60 }
61 pub fn event_type(mut self, event_type: &str) -> Self {
62 self.event_type = Some(event_type.to_owned());
63 self
64 }
65 pub fn recipients(mut self, recipients: Vec<serde_json::Value>) -> Self {
66 self.recipients = Some(recipients);
67 self
68 }
69 pub fn subject(mut self, subject: &str) -> Self {
70 self.subject = Some(subject.to_owned());
71 self
72 }
73 pub fn body(mut self, body: &str) -> Self {
74 self.body = Some(body.to_owned());
75 self
76 }
77 pub fn include_link_to_client_invoice(
78 mut self,
79 include_link_to_client_invoice: bool,
80 ) -> Self {
81 self.include_link_to_client_invoice = Some(include_link_to_client_invoice);
82 self
83 }
84 pub fn attach_pdf(mut self, attach_pdf: bool) -> Self {
85 self.attach_pdf = Some(attach_pdf);
86 self
87 }
88 pub fn send_me_a_copy(mut self, send_me_a_copy: bool) -> Self {
89 self.send_me_a_copy = Some(send_me_a_copy);
90 self
91 }
92 pub fn thank_you(mut self, thank_you: bool) -> Self {
93 self.thank_you = Some(thank_you);
94 self
95 }
96}