Skip to content

Commit

Permalink
Add error return value to NewApi()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 10, 2019
1 parent 519c8f6 commit fd3939f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 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
4 changes: 2 additions & 2 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type api struct {
client *http.Client
}

func NewApi(base string, key string, version string) Api {
func NewApi(base string, key string, version string) (Api, error) {
ua := fmt.Sprintf(userAgentTemplate, version)
return &api{base: base, key: key, userAgent: ua, client: &http.Client{}}
return &api{base: base, key: key, userAgent: ua, client: &http.Client{}}, nil
}

func (a *api) get(resource string, result interface{}) error {
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
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
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
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 fd3939f

Please sign in to comment.