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 10, 2019
2 parents 50f1bb1 + 0750531 commit 5ea3d32
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 63 deletions.
5 changes: 4 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func NewApplication(fullConfigPath string) (*Application, error) {
}

c := config.NewConfig(fullConfigPath)
api := mite.NewApi(c.GetApiUrl(), c.GetApiKey(), version)
api, err := mite.NewApi(c.GetApiUrl(), c.GetApiKey(), version)
if err != nil {
return nil, err
}

if c.GetApiUrl() == "" {
_, _ = fmt.Fprintln(os.Stderr, "please configure your API url by executing: 'mite config api.url=<your mite api url>'")
Expand Down
71 changes: 37 additions & 34 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import (
"net/url"
)

const contentType = "application/json"
const userAgentTemplate = "mite-go/%s (+github.com/leanovate/mite-go)"
const (
headerContentType = "Content-Type"
headerUserAgent = "User-Agent"
headerXMiteApiKey = "X-MiteApiKey"
mediaTypeApplicationJson = "application/json"
userAgentTemplate = "mite-go/%s (+github.com/leanovate/mite-go)"
)

type Api interface {
domain.AccountApi
Expand All @@ -23,25 +28,31 @@ type Api interface {
}

type api struct {
base string
key string
userAgent string
client *http.Client
base *url.URL
key string
agent string
client *http.Client
}

func NewApi(base string, key string, version string) Api {
ua := fmt.Sprintf(userAgentTemplate, version)
return &api{base: base, key: key, userAgent: ua, client: &http.Client{}}
func NewApi(miteUrl string, miteKey string, clientVersion string) (Api, error) {
base, err := url.Parse(miteUrl)
if err != nil {
return nil, err
}

userAgent := fmt.Sprintf(userAgentTemplate, clientVersion)

return &api{base: base, key: miteKey, agent: userAgent, client: &http.Client{}}, nil
}

func (a *api) get(resource string, result interface{}) error {
req, err := http.NewRequest(http.MethodGet, a.url(resource), nil)
func (a *api) get(resource string, query url.Values, result interface{}) error {
req, err := http.NewRequest(http.MethodGet, a.encode(resource, query), nil)
if err != nil {
return err
}

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

res, err := a.client.Do(req)
if err != nil {
Expand All @@ -56,28 +67,20 @@ func (a *api) get(resource string, result interface{}) error {
return json.NewDecoder(res.Body).Decode(result)
}

func (a *api) 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)
}

func (a *api) post(resource string, body interface{}, result interface{}) error {
b, err := json.Marshal(body)
if err != nil {
return err
}

req, err := http.NewRequest(http.MethodPost, a.url(resource), bytes.NewBuffer(b))
req, err := http.NewRequest(http.MethodPost, a.encode(resource, nil), bytes.NewBuffer(b))
if err != nil {
return err
}

req.Header.Add("Content-Type", contentType)
req.Header.Add("User-Agent", a.userAgent)
req.Header.Add("X-MiteApiKey", a.key)
req.Header.Add(headerContentType, mediaTypeApplicationJson)
req.Header.Add(headerUserAgent, a.agent)
req.Header.Add(headerXMiteApiKey, a.key)

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

req, err := http.NewRequest(http.MethodPatch, a.url(resource), bytes.NewBuffer(b))
req, err := http.NewRequest(http.MethodPatch, a.encode(resource, nil), bytes.NewBuffer(b))
if err != nil {
return err
}

req.Header.Add("Content-Type", contentType)
req.Header.Add("User-Agent", a.userAgent)
req.Header.Add("X-MiteApiKey", a.key)
req.Header.Add(headerContentType, mediaTypeApplicationJson)
req.Header.Add(headerUserAgent, a.agent)
req.Header.Add(headerXMiteApiKey, a.key)

res, err := a.client.Do(req)
if err != nil {
Expand All @@ -129,13 +132,13 @@ func (a *api) patch(resource string, body interface{}, result interface{}) error
}

func (a *api) delete(resource string, result interface{}) error {
req, err := http.NewRequest(http.MethodDelete, a.url(resource), nil)
req, err := http.NewRequest(http.MethodDelete, a.encode(resource, nil), nil)
if err != nil {
return err
}

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

res, err := a.client.Do(req)
if err != nil {
Expand All @@ -154,8 +157,8 @@ func (a *api) delete(resource string, result interface{}) error {
return nil
}

func (a *api) url(resource string) string {
return fmt.Sprintf("%s/%s", a.base, resource)
func (a *api) encode(resource string, query url.Values) string {
return a.base.ResolveReference(&url.URL{Path: resource, RawQuery: query.Encode()}).String()
}

func (a *api) check(res *http.Response) error {
Expand Down
8 changes: 5 additions & 3 deletions mite/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"encoding/json"
)

const testApiKey = "f00bar"
const testClientVersion = "vX"
const testUserAgent = "mite-go/" + testClientVersion + " (+github.com/leanovate/mite-go)"
const (
testApiKey = "key"
testClientVersion = "test"
testUserAgent = "mite-go/" + testClientVersion + " (+github.com/leanovate/mite-go)"
)

type recorder struct {
method string
Expand Down
2 changes: 1 addition & 1 deletion mite/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *projectResponse) toProject() *domain.Project {

func (a *api) Projects() ([]*domain.Project, error) {
var prs []projectResponse
err := a.get("projects.json", &prs)
err := a.get("/projects.json", nil, &prs)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion mite/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func TestApi_Projects(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
projects, err := api.Projects()
Expand Down
2 changes: 1 addition & 1 deletion mite/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *serviceResponse) toService() *domain.Service {

func (a *api) Services() ([]*domain.Service, error) {
var srs []serviceResponse
err := a.get("services.json", &srs)
err := a.get("/services.json", nil, &srs)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion mite/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func TestApi_Services(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
services, err := api.Services()
Expand Down
10 changes: 5 additions & 5 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (r *timeEntryResponse) toTimeEntry() *domain.TimeEntry {

func (a *api) TimeEntries(query *domain.TimeEntryQuery) ([]*domain.TimeEntry, error) {
var ter []timeEntryResponse
err := a.getParametrized("time_entries.json", fromQuery(query), &ter)
err := a.get("/time_entries.json", fromQuery(query), &ter)
if err != nil {
return nil, err
}
Expand All @@ -121,7 +121,7 @@ func (a *api) TimeEntries(query *domain.TimeEntryQuery) ([]*domain.TimeEntry, er

func (a *api) TimeEntry(id domain.TimeEntryId) (*domain.TimeEntry, error) {
ter := timeEntryResponse{}
err := a.get(fmt.Sprintf("time_entries/%s.json", id), &ter)
err := a.get(fmt.Sprintf("/time_entries/%s.json", id), nil, &ter)
if err != nil {
return nil, err
}
Expand All @@ -131,7 +131,7 @@ func (a *api) TimeEntry(id domain.TimeEntryId) (*domain.TimeEntry, error) {

func (a *api) CreateTimeEntry(command *domain.TimeEntryCommand) (*domain.TimeEntry, error) {
ter := timeEntryResponse{}
err := a.post("time_entries.json", fromCommand(command), &ter)
err := a.post("/time_entries.json", fromCommand(command), &ter)
if err != nil {
return nil, err
}
Expand All @@ -140,9 +140,9 @@ func (a *api) CreateTimeEntry(command *domain.TimeEntryCommand) (*domain.TimeEnt
}

func (a *api) EditTimeEntry(id domain.TimeEntryId, command *domain.TimeEntryCommand) error {
return a.patch(fmt.Sprintf("time_entries/%s.json", id), fromCommand(command), nil)
return a.patch(fmt.Sprintf("/time_entries/%s.json", id), fromCommand(command), nil)
}

func (a *api) DeleteTimeEntry(id domain.TimeEntryId) error {
return a.delete(fmt.Sprintf("time_entries/%s.json", id), nil)
return a.delete(fmt.Sprintf("/time_entries/%s.json", id), nil)
}
22 changes: 14 additions & 8 deletions mite/time_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func TestApi_TimeEntries(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
timeEntries, err := api.TimeEntries(nil)
Expand Down Expand Up @@ -113,7 +114,8 @@ func TestApi_TimeEntries_WithQuery(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
today := domain.Today()
Expand Down Expand Up @@ -150,7 +152,8 @@ func TestApi_TimeEntry(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
timeEntry, err := api.TimeEntry(timeEntryObject.Id)
Expand Down Expand Up @@ -186,7 +189,8 @@ func TestApi_CreateTimeEntry(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
command := &domain.TimeEntryCommand{
Expand Down Expand Up @@ -229,7 +233,8 @@ func TestApi_EditTimeEntry(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
command := &domain.TimeEntryCommand{
Expand All @@ -239,7 +244,7 @@ func TestApi_EditTimeEntry(t *testing.T) {
ProjectId: timeEntryObject.ProjectId,
ServiceId: timeEntryObject.ServiceId,
}
err := api.EditTimeEntry(timeEntryObject.Id, command)
err = api.EditTimeEntry(timeEntryObject.Id, command)

// then
assert.Nil(t, err)
Expand All @@ -265,10 +270,11 @@ func TestApi_DeleteTimeEntry(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
err := api.DeleteTimeEntry(timeEntryObject.Id)
err = api.DeleteTimeEntry(timeEntryObject.Id)

// then
assert.Nil(t, err)
Expand Down
6 changes: 3 additions & 3 deletions mite/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (r *trackerResponse) toStoppedTimeEntry() *domain.StoppedTimeEntry {

func (a *api) Tracker() (*domain.TrackingTimeEntry, error) {
tr := trackerResponse{}
err := a.get("tracker.json", &tr)
err := a.get("/tracker.json", nil, &tr)
if err != nil {
return nil, err
}
Expand All @@ -55,7 +55,7 @@ func (a *api) Tracker() (*domain.TrackingTimeEntry, error) {

func (a *api) StartTracker(id domain.TimeEntryId) (*domain.TrackingTimeEntry, *domain.StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.patch(fmt.Sprintf("tracker/%s.json", id), nil, tr)
err := a.patch(fmt.Sprintf("/tracker/%s.json", id), nil, tr)
if err != nil {
return nil, nil, err
}
Expand All @@ -65,7 +65,7 @@ func (a *api) StartTracker(id domain.TimeEntryId) (*domain.TrackingTimeEntry, *d

func (a *api) StopTracker(id domain.TimeEntryId) (*domain.StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.delete(fmt.Sprintf("tracker/%s.json", id), tr)
err := a.delete(fmt.Sprintf("/tracker/%s.json", id), tr)
if err != nil {
return nil, err
}
Expand Down
15 changes: 10 additions & 5 deletions mite/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func TestApi_Tracker(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
tracking, err := api.Tracker()
Expand Down Expand Up @@ -104,7 +105,8 @@ func TestApi_Tracker_Empty(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
tracking, err := api.Tracker()
Expand Down Expand Up @@ -135,7 +137,8 @@ func TestApi_StartTracker(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
tracking, stopped, err := api.StartTracker(trackingTimeEntryObject.Id)
Expand Down Expand Up @@ -167,7 +170,8 @@ func TestApi_StartTracker_Running(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
tracking, stopped, err := api.StartTracker(trackingTimeEntryObject.Id)
Expand Down Expand Up @@ -199,7 +203,8 @@ func TestApi_StopTracker(t *testing.T) {

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)
api, err := mite.NewApi(srv.URL, testApiKey, testClientVersion)
assert.Nil(t, err)

// when
stopped, err := api.StopTracker(stoppedTimeEntryObject.Id)
Expand Down

0 comments on commit 5ea3d32

Please sign in to comment.