harvest_api/request/
update_contact.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 UpdateContactRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub contact_id: String,
10    pub client_id: Option<i64>,
11    pub title: Option<String>,
12    pub first_name: Option<String>,
13    pub last_name: Option<String>,
14    pub email: Option<String>,
15    pub phone_office: Option<String>,
16    pub phone_mobile: Option<String>,
17    pub fax: Option<String>,
18}
19impl<'a> UpdateContactRequest<'a> {
20    pub async fn send(self) -> anyhow::Result<Contact> {
21        let mut r = self
22            .client
23            .client
24            .patch(&format!("/contacts/{contact_id}", contact_id = self.contact_id));
25        if let Some(ref unwrapped) = self.client_id {
26            r = r.push_json(json!({ "client_id" : unwrapped }));
27        }
28        if let Some(ref unwrapped) = self.title {
29            r = r.push_json(json!({ "title" : unwrapped }));
30        }
31        if let Some(ref unwrapped) = self.first_name {
32            r = r.push_json(json!({ "first_name" : unwrapped }));
33        }
34        if let Some(ref unwrapped) = self.last_name {
35            r = r.push_json(json!({ "last_name" : unwrapped }));
36        }
37        if let Some(ref unwrapped) = self.email {
38            r = r.push_json(json!({ "email" : unwrapped }));
39        }
40        if let Some(ref unwrapped) = self.phone_office {
41            r = r.push_json(json!({ "phone_office" : unwrapped }));
42        }
43        if let Some(ref unwrapped) = self.phone_mobile {
44            r = r.push_json(json!({ "phone_mobile" : unwrapped }));
45        }
46        if let Some(ref unwrapped) = self.fax {
47            r = r.push_json(json!({ "fax" : 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 client_id(mut self, client_id: i64) -> Self {
60        self.client_id = Some(client_id);
61        self
62    }
63    pub fn title(mut self, title: &str) -> Self {
64        self.title = Some(title.to_owned());
65        self
66    }
67    pub fn first_name(mut self, first_name: &str) -> Self {
68        self.first_name = Some(first_name.to_owned());
69        self
70    }
71    pub fn last_name(mut self, last_name: &str) -> Self {
72        self.last_name = Some(last_name.to_owned());
73        self
74    }
75    pub fn email(mut self, email: &str) -> Self {
76        self.email = Some(email.to_owned());
77        self
78    }
79    pub fn phone_office(mut self, phone_office: &str) -> Self {
80        self.phone_office = Some(phone_office.to_owned());
81        self
82    }
83    pub fn phone_mobile(mut self, phone_mobile: &str) -> Self {
84        self.phone_mobile = Some(phone_mobile.to_owned());
85        self
86    }
87    pub fn fax(mut self, fax: &str) -> Self {
88        self.fax = Some(fax.to_owned());
89        self
90    }
91}