harvest_api/request/
create_expense.rs

1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4/**Create this with the associated client method.
5
6That method takes required values as arguments. Set optional values using builder methods on this struct.*/
7pub struct CreateExpenseRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub user_id: Option<i64>,
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}
19impl<'a> CreateExpenseRequest<'a> {
20    pub async fn send(self) -> anyhow::Result<Expense> {
21        let mut r = self.client.client.post("/expenses");
22        if let Some(ref unwrapped) = self.user_id {
23            r = r.push_json(json!({ "user_id" : unwrapped }));
24        }
25        if let Some(ref unwrapped) = self.project_id {
26            r = r.push_json(json!({ "project_id" : unwrapped }));
27        }
28        if let Some(ref unwrapped) = self.expense_category_id {
29            r = r.push_json(json!({ "expense_category_id" : unwrapped }));
30        }
31        if let Some(ref unwrapped) = self.spent_date {
32            r = r.push_json(json!({ "spent_date" : unwrapped }));
33        }
34        if let Some(ref unwrapped) = self.units {
35            r = r.push_json(json!({ "units" : unwrapped }));
36        }
37        if let Some(ref unwrapped) = self.total_cost {
38            r = r.push_json(json!({ "total_cost" : unwrapped }));
39        }
40        if let Some(ref unwrapped) = self.notes {
41            r = r.push_json(json!({ "notes" : unwrapped }));
42        }
43        if let Some(ref unwrapped) = self.billable {
44            r = r.push_json(json!({ "billable" : unwrapped }));
45        }
46        if let Some(ref unwrapped) = self.receipt {
47            r = r.push_json(json!({ "receipt" : unwrapped }));
48        }
49        r = self.client.authenticate(r);
50        let res = r.send().await.unwrap().error_for_status();
51        match res {
52            Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
53            Err(res) => {
54                let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
55                Err(anyhow::anyhow!("{:?}", text))
56            }
57        }
58    }
59    pub fn user_id(mut self, user_id: i64) -> Self {
60        self.user_id = Some(user_id);
61        self
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}