harvest_api/request/
create_expense_category.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 CreateExpenseCategoryRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub name: Option<String>,
10    pub unit_name: Option<String>,
11    pub unit_price: Option<f64>,
12    pub is_active: Option<bool>,
13}
14impl<'a> CreateExpenseCategoryRequest<'a> {
15    pub async fn send(self) -> anyhow::Result<ExpenseCategory> {
16        let mut r = self.client.client.post("/expense_categories");
17        if let Some(ref unwrapped) = self.name {
18            r = r.push_json(json!({ "name" : unwrapped }));
19        }
20        if let Some(ref unwrapped) = self.unit_name {
21            r = r.push_json(json!({ "unit_name" : unwrapped }));
22        }
23        if let Some(ref unwrapped) = self.unit_price {
24            r = r.push_json(json!({ "unit_price" : unwrapped }));
25        }
26        if let Some(ref unwrapped) = self.is_active {
27            r = r.push_json(json!({ "is_active" : unwrapped }));
28        }
29        r = self.client.authenticate(r);
30        let res = r.send().await.unwrap().error_for_status();
31        match res {
32            Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
33            Err(res) => {
34                let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
35                Err(anyhow::anyhow!("{:?}", text))
36            }
37        }
38    }
39    pub fn name(mut self, name: &str) -> Self {
40        self.name = Some(name.to_owned());
41        self
42    }
43    pub fn unit_name(mut self, unit_name: &str) -> Self {
44        self.unit_name = Some(unit_name.to_owned());
45        self
46    }
47    pub fn unit_price(mut self, unit_price: f64) -> Self {
48        self.unit_price = Some(unit_price);
49        self
50    }
51    pub fn is_active(mut self, is_active: bool) -> Self {
52        self.is_active = Some(is_active);
53        self
54    }
55}