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