harvest_api/request/
create_contact.rs1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4pub struct CreateContactRequest<'a> {
8 pub(crate) client: &'a HarvestClient,
9 pub client_id: Option<i64>,
10 pub title: Option<String>,
11 pub first_name: Option<String>,
12 pub last_name: Option<String>,
13 pub email: Option<String>,
14 pub phone_office: Option<String>,
15 pub phone_mobile: Option<String>,
16 pub fax: Option<String>,
17}
18impl<'a> CreateContactRequest<'a> {
19 pub async fn send(self) -> anyhow::Result<Contact> {
20 let mut r = self.client.client.post("/contacts");
21 if let Some(ref unwrapped) = self.client_id {
22 r = r.push_json(json!({ "client_id" : unwrapped }));
23 }
24 if let Some(ref unwrapped) = self.title {
25 r = r.push_json(json!({ "title" : unwrapped }));
26 }
27 if let Some(ref unwrapped) = self.first_name {
28 r = r.push_json(json!({ "first_name" : unwrapped }));
29 }
30 if let Some(ref unwrapped) = self.last_name {
31 r = r.push_json(json!({ "last_name" : unwrapped }));
32 }
33 if let Some(ref unwrapped) = self.email {
34 r = r.push_json(json!({ "email" : unwrapped }));
35 }
36 if let Some(ref unwrapped) = self.phone_office {
37 r = r.push_json(json!({ "phone_office" : unwrapped }));
38 }
39 if let Some(ref unwrapped) = self.phone_mobile {
40 r = r.push_json(json!({ "phone_mobile" : unwrapped }));
41 }
42 if let Some(ref unwrapped) = self.fax {
43 r = r.push_json(json!({ "fax" : unwrapped }));
44 }
45 r = self.client.authenticate(r);
46 let res = r.send().await.unwrap().error_for_status();
47 match res {
48 Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
49 Err(res) => {
50 let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
51 Err(anyhow::anyhow!("{:?}", text))
52 }
53 }
54 }
55 pub fn client_id(mut self, client_id: i64) -> Self {
56 self.client_id = Some(client_id);
57 self
58 }
59 pub fn title(mut self, title: &str) -> Self {
60 self.title = Some(title.to_owned());
61 self
62 }
63 pub fn first_name(mut self, first_name: &str) -> Self {
64 self.first_name = Some(first_name.to_owned());
65 self
66 }
67 pub fn last_name(mut self, last_name: &str) -> Self {
68 self.last_name = Some(last_name.to_owned());
69 self
70 }
71 pub fn email(mut self, email: &str) -> Self {
72 self.email = Some(email.to_owned());
73 self
74 }
75 pub fn phone_office(mut self, phone_office: &str) -> Self {
76 self.phone_office = Some(phone_office.to_owned());
77 self
78 }
79 pub fn phone_mobile(mut self, phone_mobile: &str) -> Self {
80 self.phone_mobile = Some(phone_mobile.to_owned());
81 self
82 }
83 pub fn fax(mut self, fax: &str) -> Self {
84 self.fax = Some(fax.to_owned());
85 self
86 }
87}