harvest_api/request/
create_estimate.rs1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4pub struct CreateEstimateRequest<'a> {
8 pub(crate) client: &'a HarvestClient,
9 pub client_id: Option<i64>,
10 pub number: Option<String>,
11 pub purchase_order: Option<String>,
12 pub tax: Option<f64>,
13 pub tax2: Option<f64>,
14 pub discount: Option<f64>,
15 pub subject: Option<String>,
16 pub notes: Option<String>,
17 pub currency: Option<String>,
18 pub issue_date: Option<String>,
19 pub line_items: Option<Vec<serde_json::Value>>,
20}
21impl<'a> CreateEstimateRequest<'a> {
22 pub async fn send(self) -> anyhow::Result<Estimate> {
23 let mut r = self.client.client.post("/estimates");
24 if let Some(ref unwrapped) = self.client_id {
25 r = r.push_json(json!({ "client_id" : unwrapped }));
26 }
27 if let Some(ref unwrapped) = self.number {
28 r = r.push_json(json!({ "number" : unwrapped }));
29 }
30 if let Some(ref unwrapped) = self.purchase_order {
31 r = r.push_json(json!({ "purchase_order" : unwrapped }));
32 }
33 if let Some(ref unwrapped) = self.tax {
34 r = r.push_json(json!({ "tax" : unwrapped }));
35 }
36 if let Some(ref unwrapped) = self.tax2 {
37 r = r.push_json(json!({ "tax2" : unwrapped }));
38 }
39 if let Some(ref unwrapped) = self.discount {
40 r = r.push_json(json!({ "discount" : unwrapped }));
41 }
42 if let Some(ref unwrapped) = self.subject {
43 r = r.push_json(json!({ "subject" : unwrapped }));
44 }
45 if let Some(ref unwrapped) = self.notes {
46 r = r.push_json(json!({ "notes" : unwrapped }));
47 }
48 if let Some(ref unwrapped) = self.currency {
49 r = r.push_json(json!({ "currency" : unwrapped }));
50 }
51 if let Some(ref unwrapped) = self.issue_date {
52 r = r.push_json(json!({ "issue_date" : unwrapped }));
53 }
54 if let Some(ref unwrapped) = self.line_items {
55 r = r.push_json(json!({ "line_items" : unwrapped }));
56 }
57 r = self.client.authenticate(r);
58 let res = r.send().await.unwrap().error_for_status();
59 match res {
60 Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
61 Err(res) => {
62 let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
63 Err(anyhow::anyhow!("{:?}", text))
64 }
65 }
66 }
67 pub fn client_id(mut self, client_id: i64) -> Self {
68 self.client_id = Some(client_id);
69 self
70 }
71 pub fn number(mut self, number: &str) -> Self {
72 self.number = Some(number.to_owned());
73 self
74 }
75 pub fn purchase_order(mut self, purchase_order: &str) -> Self {
76 self.purchase_order = Some(purchase_order.to_owned());
77 self
78 }
79 pub fn tax(mut self, tax: f64) -> Self {
80 self.tax = Some(tax);
81 self
82 }
83 pub fn tax2(mut self, tax2: f64) -> Self {
84 self.tax2 = Some(tax2);
85 self
86 }
87 pub fn discount(mut self, discount: f64) -> Self {
88 self.discount = Some(discount);
89 self
90 }
91 pub fn subject(mut self, subject: &str) -> Self {
92 self.subject = Some(subject.to_owned());
93 self
94 }
95 pub fn notes(mut self, notes: &str) -> Self {
96 self.notes = Some(notes.to_owned());
97 self
98 }
99 pub fn currency(mut self, currency: &str) -> Self {
100 self.currency = Some(currency.to_owned());
101 self
102 }
103 pub fn issue_date(mut self, issue_date: &str) -> Self {
104 self.issue_date = Some(issue_date.to_owned());
105 self
106 }
107 pub fn line_items(mut self, line_items: Vec<serde_json::Value>) -> Self {
108 self.line_items = Some(line_items);
109 self
110 }
111}