harvest_api/request/
update_time_entry.rs1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4pub struct UpdateTimeEntryRequest<'a> {
8 pub(crate) client: &'a HarvestClient,
9 pub time_entry_id: String,
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 hours: Option<f64>,
16 pub notes: Option<String>,
17 pub external_reference: Option<serde_json::Value>,
18}
19impl<'a> UpdateTimeEntryRequest<'a> {
20 pub async fn send(self) -> anyhow::Result<TimeEntry> {
21 let mut r = self
22 .client
23 .client
24 .patch(
25 &format!(
26 "/time_entries/{time_entry_id}", time_entry_id = self.time_entry_id
27 ),
28 );
29 if let Some(ref unwrapped) = self.project_id {
30 r = r.push_json(json!({ "project_id" : unwrapped }));
31 }
32 if let Some(ref unwrapped) = self.task_id {
33 r = r.push_json(json!({ "task_id" : unwrapped }));
34 }
35 if let Some(ref unwrapped) = self.spent_date {
36 r = r.push_json(json!({ "spent_date" : unwrapped }));
37 }
38 if let Some(ref unwrapped) = self.started_time {
39 r = r.push_json(json!({ "started_time" : unwrapped }));
40 }
41 if let Some(ref unwrapped) = self.ended_time {
42 r = r.push_json(json!({ "ended_time" : unwrapped }));
43 }
44 if let Some(ref unwrapped) = self.hours {
45 r = r.push_json(json!({ "hours" : unwrapped }));
46 }
47 if let Some(ref unwrapped) = self.notes {
48 r = r.push_json(json!({ "notes" : unwrapped }));
49 }
50 if let Some(ref unwrapped) = self.external_reference {
51 r = r.push_json(json!({ "external_reference" : unwrapped }));
52 }
53 r = self.client.authenticate(r);
54 let res = r.send().await.unwrap().error_for_status();
55 match res {
56 Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
57 Err(res) => {
58 let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
59 Err(anyhow::anyhow!("{:?}", text))
60 }
61 }
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 hours(mut self, hours: f64) -> Self {
84 self.hours = Some(hours);
85 self
86 }
87 pub fn notes(mut self, notes: &str) -> Self {
88 self.notes = Some(notes.to_owned());
89 self
90 }
91 pub fn external_reference(mut self, external_reference: serde_json::Value) -> Self {
92 self.external_reference = Some(external_reference);
93 self
94 }
95}