harvest_api/request/
update_user.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 UpdateUserRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub user_id: String,
10    pub first_name: Option<String>,
11    pub last_name: Option<String>,
12    pub email: Option<String>,
13    pub timezone: Option<String>,
14    pub has_access_to_all_future_projects: Option<bool>,
15    pub is_contractor: Option<bool>,
16    pub is_active: Option<bool>,
17    pub weekly_capacity: Option<i64>,
18    pub default_hourly_rate: Option<f64>,
19    pub cost_rate: Option<f64>,
20    pub roles: Option<Vec<String>>,
21    pub access_roles: Option<Vec<String>>,
22}
23impl<'a> UpdateUserRequest<'a> {
24    pub async fn send(self) -> anyhow::Result<User> {
25        let mut r = self
26            .client
27            .client
28            .patch(&format!("/users/{user_id}", user_id = self.user_id));
29        if let Some(ref unwrapped) = self.first_name {
30            r = r.push_json(json!({ "first_name" : unwrapped }));
31        }
32        if let Some(ref unwrapped) = self.last_name {
33            r = r.push_json(json!({ "last_name" : unwrapped }));
34        }
35        if let Some(ref unwrapped) = self.email {
36            r = r.push_json(json!({ "email" : unwrapped }));
37        }
38        if let Some(ref unwrapped) = self.timezone {
39            r = r.push_json(json!({ "timezone" : unwrapped }));
40        }
41        if let Some(ref unwrapped) = self.has_access_to_all_future_projects {
42            r = r.push_json(json!({ "has_access_to_all_future_projects" : unwrapped }));
43        }
44        if let Some(ref unwrapped) = self.is_contractor {
45            r = r.push_json(json!({ "is_contractor" : unwrapped }));
46        }
47        if let Some(ref unwrapped) = self.is_active {
48            r = r.push_json(json!({ "is_active" : unwrapped }));
49        }
50        if let Some(ref unwrapped) = self.weekly_capacity {
51            r = r.push_json(json!({ "weekly_capacity" : unwrapped }));
52        }
53        if let Some(ref unwrapped) = self.default_hourly_rate {
54            r = r.push_json(json!({ "default_hourly_rate" : unwrapped }));
55        }
56        if let Some(ref unwrapped) = self.cost_rate {
57            r = r.push_json(json!({ "cost_rate" : unwrapped }));
58        }
59        if let Some(ref unwrapped) = self.roles {
60            r = r.push_json(json!({ "roles" : unwrapped }));
61        }
62        if let Some(ref unwrapped) = self.access_roles {
63            r = r.push_json(json!({ "access_roles" : unwrapped }));
64        }
65        r = self.client.authenticate(r);
66        let res = r.send().await.unwrap().error_for_status();
67        match res {
68            Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
69            Err(res) => {
70                let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
71                Err(anyhow::anyhow!("{:?}", text))
72            }
73        }
74    }
75    pub fn first_name(mut self, first_name: &str) -> Self {
76        self.first_name = Some(first_name.to_owned());
77        self
78    }
79    pub fn last_name(mut self, last_name: &str) -> Self {
80        self.last_name = Some(last_name.to_owned());
81        self
82    }
83    pub fn email(mut self, email: &str) -> Self {
84        self.email = Some(email.to_owned());
85        self
86    }
87    pub fn timezone(mut self, timezone: &str) -> Self {
88        self.timezone = Some(timezone.to_owned());
89        self
90    }
91    pub fn has_access_to_all_future_projects(
92        mut self,
93        has_access_to_all_future_projects: bool,
94    ) -> Self {
95        self.has_access_to_all_future_projects = Some(has_access_to_all_future_projects);
96        self
97    }
98    pub fn is_contractor(mut self, is_contractor: bool) -> Self {
99        self.is_contractor = Some(is_contractor);
100        self
101    }
102    pub fn is_active(mut self, is_active: bool) -> Self {
103        self.is_active = Some(is_active);
104        self
105    }
106    pub fn weekly_capacity(mut self, weekly_capacity: i64) -> Self {
107        self.weekly_capacity = Some(weekly_capacity);
108        self
109    }
110    pub fn default_hourly_rate(mut self, default_hourly_rate: f64) -> Self {
111        self.default_hourly_rate = Some(default_hourly_rate);
112        self
113    }
114    pub fn cost_rate(mut self, cost_rate: f64) -> Self {
115        self.cost_rate = Some(cost_rate);
116        self
117    }
118    pub fn roles(mut self, roles: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
119        self.roles = Some(roles.into_iter().map(|s| s.as_ref().to_owned()).collect());
120        self
121    }
122    pub fn access_roles(
123        mut self,
124        access_roles: impl IntoIterator<Item = impl AsRef<str>>,
125    ) -> Self {
126        self
127            .access_roles = Some(
128            access_roles.into_iter().map(|s| s.as_ref().to_owned()).collect(),
129        );
130        self
131    }
132}