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