harvest_api/request/
create_role.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 CreateRoleRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub name: Option<String>,
10    pub user_ids: Option<Vec<i64>>,
11}
12impl<'a> CreateRoleRequest<'a> {
13    pub async fn send(self) -> anyhow::Result<Role> {
14        let mut r = self.client.client.post("/roles");
15        if let Some(ref unwrapped) = self.name {
16            r = r.push_json(json!({ "name" : unwrapped }));
17        }
18        if let Some(ref unwrapped) = self.user_ids {
19            r = r.push_json(json!({ "user_ids" : unwrapped }));
20        }
21        r = self.client.authenticate(r);
22        let res = r.send().await.unwrap().error_for_status();
23        match res {
24            Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
25            Err(res) => {
26                let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
27                Err(anyhow::anyhow!("{:?}", text))
28            }
29        }
30    }
31    pub fn name(mut self, name: &str) -> Self {
32        self.name = Some(name.to_owned());
33        self
34    }
35    pub fn user_ids(mut self, user_ids: Vec<i64>) -> Self {
36        self.user_ids = Some(user_ids);
37        self
38    }
39}