harvest_api/request/
update_expense_category.rs1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4pub struct UpdateExpenseCategoryRequest<'a> {
8 pub(crate) client: &'a HarvestClient,
9 pub expense_category_id: String,
10 pub name: Option<String>,
11 pub unit_name: Option<String>,
12 pub unit_price: Option<f64>,
13 pub is_active: Option<bool>,
14}
15impl<'a> UpdateExpenseCategoryRequest<'a> {
16 pub async fn send(self) -> anyhow::Result<ExpenseCategory> {
17 let mut r = self
18 .client
19 .client
20 .patch(
21 &format!(
22 "/expense_categories/{expense_category_id}", expense_category_id =
23 self.expense_category_id
24 ),
25 );
26 if let Some(ref unwrapped) = self.name {
27 r = r.push_json(json!({ "name" : unwrapped }));
28 }
29 if let Some(ref unwrapped) = self.unit_name {
30 r = r.push_json(json!({ "unit_name" : unwrapped }));
31 }
32 if let Some(ref unwrapped) = self.unit_price {
33 r = r.push_json(json!({ "unit_price" : unwrapped }));
34 }
35 if let Some(ref unwrapped) = self.is_active {
36 r = r.push_json(json!({ "is_active" : unwrapped }));
37 }
38 r = self.client.authenticate(r);
39 let res = r.send().await.unwrap().error_for_status();
40 match res {
41 Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
42 Err(res) => {
43 let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
44 Err(anyhow::anyhow!("{:?}", text))
45 }
46 }
47 }
48 pub fn name(mut self, name: &str) -> Self {
49 self.name = Some(name.to_owned());
50 self
51 }
52 pub fn unit_name(mut self, unit_name: &str) -> Self {
53 self.unit_name = Some(unit_name.to_owned());
54 self
55 }
56 pub fn unit_price(mut self, unit_price: f64) -> Self {
57 self.unit_price = Some(unit_price);
58 self
59 }
60 pub fn is_active(mut self, is_active: bool) -> Self {
61 self.is_active = Some(is_active);
62 self
63 }
64}