harvest_api/request/
create_task.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 CreateTaskRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub name: Option<String>,
10    pub billable_by_default: Option<bool>,
11    pub default_hourly_rate: Option<f64>,
12    pub is_default: Option<bool>,
13    pub is_active: Option<bool>,
14}
15impl<'a> CreateTaskRequest<'a> {
16    pub async fn send(self) -> anyhow::Result<Task> {
17        let mut r = self.client.client.post("/tasks");
18        if let Some(ref unwrapped) = self.name {
19            r = r.push_json(json!({ "name" : unwrapped }));
20        }
21        if let Some(ref unwrapped) = self.billable_by_default {
22            r = r.push_json(json!({ "billable_by_default" : unwrapped }));
23        }
24        if let Some(ref unwrapped) = self.default_hourly_rate {
25            r = r.push_json(json!({ "default_hourly_rate" : unwrapped }));
26        }
27        if let Some(ref unwrapped) = self.is_default {
28            r = r.push_json(json!({ "is_default" : unwrapped }));
29        }
30        if let Some(ref unwrapped) = self.is_active {
31            r = r.push_json(json!({ "is_active" : unwrapped }));
32        }
33        r = self.client.authenticate(r);
34        let res = r.send().await.unwrap().error_for_status();
35        match res {
36            Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
37            Err(res) => {
38                let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
39                Err(anyhow::anyhow!("{:?}", text))
40            }
41        }
42    }
43    pub fn name(mut self, name: &str) -> Self {
44        self.name = Some(name.to_owned());
45        self
46    }
47    pub fn billable_by_default(mut self, billable_by_default: bool) -> Self {
48        self.billable_by_default = Some(billable_by_default);
49        self
50    }
51    pub fn default_hourly_rate(mut self, default_hourly_rate: f64) -> Self {
52        self.default_hourly_rate = Some(default_hourly_rate);
53        self
54    }
55    pub fn is_default(mut self, is_default: bool) -> Self {
56        self.is_default = Some(is_default);
57        self
58    }
59    pub fn is_active(mut self, is_active: bool) -> Self {
60        self.is_active = Some(is_active);
61        self
62    }
63}