harvest_api/request/
update_expense.rs1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4pub struct UpdateExpenseRequest<'a> {
8 pub(crate) client: &'a HarvestClient,
9 pub expense_id: String,
10 pub project_id: Option<i64>,
11 pub expense_category_id: Option<i64>,
12 pub spent_date: Option<String>,
13 pub units: Option<i64>,
14 pub total_cost: Option<f64>,
15 pub notes: Option<String>,
16 pub billable: Option<bool>,
17 pub receipt: Option<String>,
18 pub delete_receipt: Option<bool>,
19}
20impl<'a> UpdateExpenseRequest<'a> {
21 pub async fn send(self) -> anyhow::Result<Expense> {
22 let mut r = self
23 .client
24 .client
25 .patch(&format!("/expenses/{expense_id}", expense_id = self.expense_id));
26 if let Some(ref unwrapped) = self.project_id {
27 r = r.push_json(json!({ "project_id" : unwrapped }));
28 }
29 if let Some(ref unwrapped) = self.expense_category_id {
30 r = r.push_json(json!({ "expense_category_id" : unwrapped }));
31 }
32 if let Some(ref unwrapped) = self.spent_date {
33 r = r.push_json(json!({ "spent_date" : unwrapped }));
34 }
35 if let Some(ref unwrapped) = self.units {
36 r = r.push_json(json!({ "units" : unwrapped }));
37 }
38 if let Some(ref unwrapped) = self.total_cost {
39 r = r.push_json(json!({ "total_cost" : unwrapped }));
40 }
41 if let Some(ref unwrapped) = self.notes {
42 r = r.push_json(json!({ "notes" : unwrapped }));
43 }
44 if let Some(ref unwrapped) = self.billable {
45 r = r.push_json(json!({ "billable" : unwrapped }));
46 }
47 if let Some(ref unwrapped) = self.receipt {
48 r = r.push_json(json!({ "receipt" : unwrapped }));
49 }
50 if let Some(ref unwrapped) = self.delete_receipt {
51 r = r.push_json(json!({ "delete_receipt" : unwrapped }));
52 }
53 r = self.client.authenticate(r);
54 let res = r.send().await.unwrap().error_for_status();
55 match res {
56 Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
57 Err(res) => {
58 let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
59 Err(anyhow::anyhow!("{:?}", text))
60 }
61 }
62 }
63 pub fn project_id(mut self, project_id: i64) -> Self {
64 self.project_id = Some(project_id);
65 self
66 }
67 pub fn expense_category_id(mut self, expense_category_id: i64) -> Self {
68 self.expense_category_id = Some(expense_category_id);
69 self
70 }
71 pub fn spent_date(mut self, spent_date: &str) -> Self {
72 self.spent_date = Some(spent_date.to_owned());
73 self
74 }
75 pub fn units(mut self, units: i64) -> Self {
76 self.units = Some(units);
77 self
78 }
79 pub fn total_cost(mut self, total_cost: f64) -> Self {
80 self.total_cost = Some(total_cost);
81 self
82 }
83 pub fn notes(mut self, notes: &str) -> Self {
84 self.notes = Some(notes.to_owned());
85 self
86 }
87 pub fn billable(mut self, billable: bool) -> Self {
88 self.billable = Some(billable);
89 self
90 }
91 pub fn receipt(mut self, receipt: &str) -> Self {
92 self.receipt = Some(receipt.to_owned());
93 self
94 }
95 pub fn delete_receipt(mut self, delete_receipt: bool) -> Self {
96 self.delete_receipt = Some(delete_receipt);
97 self
98 }
99}