harvest_api/request/
create_client.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 CreateClientRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub name: Option<String>,
10    pub is_active: Option<bool>,
11    pub address: Option<String>,
12    pub currency: Option<String>,
13}
14impl<'a> CreateClientRequest<'a> {
15    pub async fn send(self) -> anyhow::Result<Client> {
16        let mut r = self.client.client.post("/clients");
17        if let Some(ref unwrapped) = self.name {
18            r = r.push_json(json!({ "name" : unwrapped }));
19        }
20        if let Some(ref unwrapped) = self.is_active {
21            r = r.push_json(json!({ "is_active" : unwrapped }));
22        }
23        if let Some(ref unwrapped) = self.address {
24            r = r.push_json(json!({ "address" : unwrapped }));
25        }
26        if let Some(ref unwrapped) = self.currency {
27            r = r.push_json(json!({ "currency" : 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 is_active(mut self, is_active: bool) -> Self {
44        self.is_active = Some(is_active);
45        self
46    }
47    pub fn address(mut self, address: &str) -> Self {
48        self.address = Some(address.to_owned());
49        self
50    }
51    pub fn currency(mut self, currency: &str) -> Self {
52        self.currency = Some(currency.to_owned());
53        self
54    }
55}