harvest_api/request/
create_time_entry.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 CreateTimeEntryRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub user_id: Option<i64>,
10    pub project_id: Option<i64>,
11    pub task_id: Option<i64>,
12    pub spent_date: Option<String>,
13    pub started_time: Option<String>,
14    pub ended_time: Option<String>,
15    pub notes: Option<String>,
16    pub external_reference: Option<serde_json::Value>,
17    pub hours: Option<f64>,
18}
19impl<'a> CreateTimeEntryRequest<'a> {
20    pub async fn send(self) -> anyhow::Result<TimeEntry> {
21        let mut r = self.client.client.post("/time_entries");
22        if let Some(ref unwrapped) = self.user_id {
23            r = r.push_json(json!({ "user_id" : unwrapped }));
24        }
25        if let Some(ref unwrapped) = self.project_id {
26            r = r.push_json(json!({ "project_id" : unwrapped }));
27        }
28        if let Some(ref unwrapped) = self.task_id {
29            r = r.push_json(json!({ "task_id" : unwrapped }));
30        }
31        if let Some(ref unwrapped) = self.spent_date {
32            r = r.push_json(json!({ "spent_date" : unwrapped }));
33        }
34        if let Some(ref unwrapped) = self.started_time {
35            r = r.push_json(json!({ "started_time" : unwrapped }));
36        }
37        if let Some(ref unwrapped) = self.ended_time {
38            r = r.push_json(json!({ "ended_time" : unwrapped }));
39        }
40        if let Some(ref unwrapped) = self.notes {
41            r = r.push_json(json!({ "notes" : unwrapped }));
42        }
43        if let Some(ref unwrapped) = self.external_reference {
44            r = r.push_json(json!({ "external_reference" : unwrapped }));
45        }
46        if let Some(ref unwrapped) = self.hours {
47            r = r.push_json(json!({ "hours" : unwrapped }));
48        }
49        r = self.client.authenticate(r);
50        let res = r.send().await.unwrap().error_for_status();
51        match res {
52            Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
53            Err(res) => {
54                let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
55                Err(anyhow::anyhow!("{:?}", text))
56            }
57        }
58    }
59    pub fn user_id(mut self, user_id: i64) -> Self {
60        self.user_id = Some(user_id);
61        self
62    }
63    pub fn project_id(mut self, project_id: i64) -> Self {
64        self.project_id = Some(project_id);
65        self
66    }
67    pub fn task_id(mut self, task_id: i64) -> Self {
68        self.task_id = Some(task_id);
69        self
70    }
71    pub fn spent_date(mut self, spent_date: &str) -> Self {
72        self.spent_date = Some(spent_date.to_owned());
73        self
74    }
75    pub fn started_time(mut self, started_time: &str) -> Self {
76        self.started_time = Some(started_time.to_owned());
77        self
78    }
79    pub fn ended_time(mut self, ended_time: &str) -> Self {
80        self.ended_time = Some(ended_time.to_owned());
81        self
82    }
83    pub fn notes(mut self, notes: &str) -> Self {
84        self.notes = Some(notes.to_owned());
85        self
86    }
87    pub fn external_reference(mut self, external_reference: serde_json::Value) -> Self {
88        self.external_reference = Some(external_reference);
89        self
90    }
91    pub fn hours(mut self, hours: f64) -> Self {
92        self.hours = Some(hours);
93        self
94    }
95}