Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 2, 2019
2 parents dd33469 + 7d4a395 commit d25c900
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 59 deletions.
33 changes: 31 additions & 2 deletions mite/api.go
Original file line number Diff line number Diff line change
@@ -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)"
Expand All @@ -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)
}
19 changes: 2 additions & 17 deletions mite/project.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package mite

import (
"encoding/json"
"fmt"
"net/http"
)

type Project struct {
Expand All @@ -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
}
Expand Down
19 changes: 2 additions & 17 deletions mite/service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package mite

import (
"encoding/json"
"fmt"
"net/http"
)

type Service struct {
Expand All @@ -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
}
Expand Down
25 changes: 2 additions & 23 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package mite

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit d25c900

Please sign in to comment.