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