Skip to content

Commit

Permalink
Refactor api internals
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 3, 2019
1 parent 7da2f57 commit f09e4fd
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"net/url"
)

const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)"
const layout = "2006-01-02"
const contentType = "application/json"
const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)"

type MiteApi interface {
TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error)
Expand All @@ -22,22 +23,23 @@ type MiteApi interface {
}

type miteApi struct {
url string
base string
key string
client *http.Client
}

func NewMiteApi(url string, key string) MiteApi {
return &miteApi{url: url, key: key, client: &http.Client{}}
func NewMiteApi(base string, key string) MiteApi {
return &miteApi{base: base, 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)
req, err := http.NewRequest(http.MethodGet, a.url(resource), nil)
if err != nil {
return err
}
req.Header.Add("X-MiteApiKey", a.key)

req.Header.Add("User-Agent", userAgent)
req.Header.Add("X-MiteApiKey", a.key)

res, err := a.client.Do(req)
if err != nil {
Expand Down Expand Up @@ -66,13 +68,14 @@ func (a *miteApi) post(resource string, body interface{}, result interface{}) er
return err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", a.url, resource), bytes.NewBuffer(b))
req, err := http.NewRequest(http.MethodPost, a.url(resource), bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Add("X-MiteApiKey", a.key)

req.Header.Add("Content-Type", contentType)
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-MiteApiKey", a.key)

res, err := a.client.Do(req)
if err != nil {
Expand All @@ -93,13 +96,14 @@ func (a *miteApi) patch(resource string, body interface{}) error {
return err
}

req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/%s", a.url, resource), bytes.NewBuffer(b))
req, err := http.NewRequest(http.MethodPatch, a.url(resource), bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Add("X-MiteApiKey", a.key)

req.Header.Add("Content-Type", contentType)
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-MiteApiKey", a.key)

res, err := a.client.Do(req)
if err != nil {
Expand All @@ -115,12 +119,13 @@ func (a *miteApi) patch(resource string, body interface{}) error {
}

func (a *miteApi) delete(resource string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/%s", a.url, resource), nil)
req, err := http.NewRequest(http.MethodDelete, a.url(resource), nil)
if err != nil {
return err
}
req.Header.Add("X-MiteApiKey", a.key)

req.Header.Add("User-Agent", userAgent)
req.Header.Add("X-MiteApiKey", a.key)

res, err := a.client.Do(req)
if err != nil {
Expand All @@ -135,6 +140,10 @@ func (a *miteApi) delete(resource string) error {
return nil
}

func (a *miteApi) url(resource string) string {
return fmt.Sprintf("%s/%s", a.base, resource)
}

func (a *miteApi) check(res *http.Response) error {
if res.StatusCode < 400 {
return nil
Expand Down

0 comments on commit f09e4fd

Please sign in to comment.