From 1a9c7976db0b0c8ae31123dcfbbff161336cb271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20Liss=C3=A9?= Date: Wed, 3 Apr 2019 13:32:15 +0200 Subject: [PATCH] Add EditTimeEntry() --- mite/api.go | 22 ++++++++++++++++++++++ mite/time_entry.go | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/mite/api.go b/mite/api.go index 7a7236a..7a4b774 100644 --- a/mite/api.go +++ b/mite/api.go @@ -15,6 +15,7 @@ type MiteApi interface { TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) TimeEntry(id string) (*TimeEntry, error) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) + EditTimeEntry(id string, command *TimeEntryCommand) error Projects() ([]*Project, error) Services() ([]*Service, error) } @@ -78,3 +79,24 @@ func (a *miteApi) post(resource string, body interface{}, result interface{}) er return json.NewDecoder(res.Body).Decode(result) } + +func (a *miteApi) patch(resource string, body interface{}) error { + b, err := json.Marshal(body) + if err != nil { + return err + } + + req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/%s", a.url, resource), bytes.NewBuffer(b)) + if err != nil { + return err + } + req.Header.Add("X-MiteApiKey", a.key) + req.Header.Add("User-Agent", userAgent) + req.Header.Add("Content-Type", "application/json") + + res, err := a.client.Do(req) + + defer func() { _ = res.Body.Close() }() + + return err +} diff --git a/mite/time_entry.go b/mite/time_entry.go index 9641d8c..6a205c5 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -139,3 +139,7 @@ func (a *miteApi) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) return ter.ToTimeEntry(), nil } + +func (a *miteApi) EditTimeEntry(id string, command *TimeEntryCommand) error { + return a.patch(fmt.Sprintf("/time_entries/%s.json", id), command.toRequest()) +}