diff --git a/mite/api.go b/mite/api.go index 32ce6ef..a3f85fd 100644 --- a/mite/api.go +++ b/mite/api.go @@ -1,7 +1,10 @@ package mite import ( + "encoding/json" + "fmt" "net/http" + "net/url" ) const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)" @@ -13,12 +16,38 @@ type MiteApi interface { TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) } -type defaultApi struct { +type miteApi struct { url string key string client *http.Client } func NewMiteApi(url string, key string) MiteApi { - return &defaultApi{url: url, key: key, client: &http.Client{}} + return &miteApi{url: url, key: key, client: &http.Client{}} +} + +func (a *miteApi) get(resource string, result interface{}) error { + req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", a.url, resource), nil) + if err != nil { + return err + } + req.Header.Add("X-MiteApiKey", a.key) + req.Header.Add("User-Agent", userAgent) + + res, err := a.client.Do(req) + if err != nil { + return err + } + + defer func() { _ = res.Body.Close() }() + + return json.NewDecoder(res.Body).Decode(result) +} + +func (a *miteApi) getParametrized(resource string, values url.Values, result interface{}) error { + u := &url.URL{} + u.Path = resource + u.RawQuery = values.Encode() + + return a.get(u.String(), result) } diff --git a/mite/project.go b/mite/project.go index 45909cb..1983f1e 100644 --- a/mite/project.go +++ b/mite/project.go @@ -1,9 +1,7 @@ package mite import ( - "encoding/json" "fmt" - "net/http" ) type Project struct { @@ -12,22 +10,9 @@ type Project struct { Note string } -func (a *defaultApi) Projects() ([]Project, error) { - req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", a.url, "projects.json"), 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() }() - +func (a *miteApi) Projects() ([]Project, error) { prs := []ProjectResponse{} - err = json.NewDecoder(res.Body).Decode(&prs) + err := a.get("projects.json", &prs) if err != nil { return nil, err } diff --git a/mite/service.go b/mite/service.go index 8b622bd..5841a2f 100644 --- a/mite/service.go +++ b/mite/service.go @@ -1,9 +1,7 @@ package mite import ( - "encoding/json" "fmt" - "net/http" ) type Service struct { @@ -12,22 +10,9 @@ type Service struct { Note string } -func (a *defaultApi) Services() ([]Service, error) { - req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", a.url, "services.json"), 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() }() - +func (a *miteApi) Services() ([]Service, error) { srs := []ServiceResponse{} - err = json.NewDecoder(res.Body).Decode(&srs) + err := a.get("services.json", &srs) if err != nil { return nil, err } diff --git a/mite/time_entry.go b/mite/time_entry.go index b2eb75b..937f658 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -1,9 +1,7 @@ package mite import ( - "encoding/json" "fmt" - "net/http" "net/url" "time" ) @@ -30,7 +28,7 @@ type TimeEntryParameters struct { Direction *Direction } -func (a *defaultApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) { +func (a *miteApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) { values := url.Values{} if params != nil { if params.From != nil { @@ -49,27 +47,8 @@ func (a *defaultApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, erro } } - 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) + err := a.getParametrized("time_entries.json", values, &ter) if err != nil { return nil, err }