From 34efdef17f52bcf607b5796a34e03b8b06678e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20Liss=C3=A9?= Date: Tue, 2 Apr 2019 17:08:30 +0200 Subject: [PATCH] Extract time entry entity --- mite/api.go | 105 ------------------------------------------- mite/time_entry.go | 110 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 105 deletions(-) create mode 100644 mite/time_entry.go diff --git a/mite/api.go b/mite/api.go index f9af283..32ce6ef 100644 --- a/mite/api.go +++ b/mite/api.go @@ -1,11 +1,7 @@ package mite import ( - "encoding/json" - "fmt" "net/http" - "net/url" - "time" ) const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)" @@ -17,28 +13,6 @@ type MiteApi interface { TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) } -type TimeEntry struct { - Id string - Note string - Duration time.Duration - Date time.Time - ProjectName string - ServiceName string -} - -type Direction int - -const ( - DirectionAsc = Direction(0) - DirectionDesc = Direction(1) -) - -type TimeEntryParameters struct { - From *time.Time - To *time.Time - Direction *Direction -} - type defaultApi struct { url string key string @@ -48,82 +22,3 @@ type defaultApi struct { func NewMiteApi(url string, key string) MiteApi { return &defaultApi{url: url, key: key, client: &http.Client{}} } - -func (a *defaultApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) { - values := url.Values{} - if params != nil { - if params.From != nil { - values.Add("from", params.From.Format(layout)) - } - if params.To != nil { - values.Add("to", params.To.Format(layout)) - } - if params.Direction != nil { - switch *params.Direction { - case DirectionAsc: - values.Add("direction", "asc") - case DirectionDesc: - values.Add("direction", "desc") - } - } - } - - u, err := url.Parse(fmt.Sprintf("%s/%s", a.url, "time_entries.json")) - if err != nil { - return nil, err - } - u.RawQuery = values.Encode() - - req, err := http.NewRequest("GET", u.String(), nil) - if err != nil { - return nil, err - } - req.Header.Add("X-MiteApiKey", a.key) - req.Header.Add("User-Agent", userAgent) - - res, err := a.client.Do(req) - if err != nil { - return nil, err - } - defer func() { _ = res.Body.Close() }() - - ter := []TimeEntryResponse{} - err = json.NewDecoder(res.Body).Decode(&ter) - if err != nil { - return nil, err - } - - timeEntries := []TimeEntry{} - for _, te := range ter { - timeEntries = append(timeEntries, te.ToTimeEntry()) - } - - return timeEntries, nil -} - -type TimeEntryResponse struct { - TimeEntry struct { - Id int `json:"id"` - Note string `json:"note"` - Minutes int `json:"minutes"` - Date string `json:"date_at"` - ProjectName string `json:"project_name"` - ServiceName string `json:"service_name"` - } `json:"time_entry"` -} - -func (r TimeEntryResponse) ToTimeEntry() TimeEntry { - date, err := time.Parse(layout, r.TimeEntry.Date) - if err != nil { - panic(err) - } - - return TimeEntry{ - Id: fmt.Sprintf("%d", r.TimeEntry.Id), - Note: r.TimeEntry.Note, - Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute, - Date: date, - ProjectName: r.TimeEntry.ProjectName, - ServiceName: r.TimeEntry.ServiceName, - } -} diff --git a/mite/time_entry.go b/mite/time_entry.go new file mode 100644 index 0000000..b2eb75b --- /dev/null +++ b/mite/time_entry.go @@ -0,0 +1,110 @@ +package mite + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + "time" +) + +type TimeEntry struct { + Id string + Note string + Duration time.Duration + Date time.Time + ProjectName string + ServiceName string +} + +type Direction int + +const ( + DirectionAsc = Direction(0) + DirectionDesc = Direction(1) +) + +type TimeEntryParameters struct { + From *time.Time + To *time.Time + Direction *Direction +} + +func (a *defaultApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) { + values := url.Values{} + if params != nil { + if params.From != nil { + values.Add("from", params.From.Format(layout)) + } + if params.To != nil { + values.Add("to", params.To.Format(layout)) + } + if params.Direction != nil { + switch *params.Direction { + case DirectionAsc: + values.Add("direction", "asc") + case DirectionDesc: + values.Add("direction", "desc") + } + } + } + + u, err := url.Parse(fmt.Sprintf("%s/%s", a.url, "time_entries.json")) + if err != nil { + return nil, err + } + u.RawQuery = values.Encode() + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return nil, err + } + req.Header.Add("X-MiteApiKey", a.key) + req.Header.Add("User-Agent", userAgent) + + res, err := a.client.Do(req) + if err != nil { + return nil, err + } + defer func() { _ = res.Body.Close() }() + + ter := []TimeEntryResponse{} + err = json.NewDecoder(res.Body).Decode(&ter) + if err != nil { + return nil, err + } + + timeEntries := []TimeEntry{} + for _, te := range ter { + timeEntries = append(timeEntries, te.ToTimeEntry()) + } + + return timeEntries, nil +} + +type TimeEntryResponse struct { + TimeEntry struct { + Id int `json:"id"` + Note string `json:"note"` + Minutes int `json:"minutes"` + Date string `json:"date_at"` + ProjectName string `json:"project_name"` + ServiceName string `json:"service_name"` + } `json:"time_entry"` +} + +func (r TimeEntryResponse) ToTimeEntry() TimeEntry { + date, err := time.Parse(layout, r.TimeEntry.Date) + if err != nil { + panic(err) + } + + return TimeEntry{ + Id: fmt.Sprintf("%d", r.TimeEntry.Id), + Note: r.TimeEntry.Note, + Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute, + Date: date, + ProjectName: r.TimeEntry.ProjectName, + ServiceName: r.TimeEntry.ServiceName, + } +}