From 94bed46327f3e52b6977f38c8df52345e9bc41fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20Liss=C3=A9?= Date: Thu, 4 Apr 2019 12:45:50 +0200 Subject: [PATCH] Decompose mite api --- main.go | 2 +- mite/api.go | 43 ++++++++++++++++++++++++++++++++----------- mite/project.go | 2 +- mite/service.go | 2 +- mite/time_entry.go | 10 +++++----- 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index b2c3789..32195a7 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ func main() { _, _ = fmt.Fprintln(os.Stderr, err) } c := config.NewConfig(configFileName, homeDirectory, configType) - api := mite.NewMiteApi(c.GetApiUrl(), c.GetApiKey()) + api := mite.NewApi(c.GetApiUrl(), c.GetApiKey()) err = cmd.HandleCommands(c, api) if err != nil { diff --git a/mite/api.go b/mite/api.go index 786abea..d03aa84 100644 --- a/mite/api.go +++ b/mite/api.go @@ -11,27 +11,48 @@ import ( const contentType = "application/json" const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)" -type Api interface { +type AccountApi interface{} + +type TimeEntryApi interface { TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) TimeEntry(id string) (*TimeEntry, error) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) EditTimeEntry(id string, command *TimeEntryCommand) error DeleteTimeEntry(id string) error +} + +type CustomerApi interface{} + +type ProjectApi interface { Projects() ([]*Project, error) +} + +type ServiceApi interface { Services() ([]*Service, error) } -type miteApi struct { +type UserApi interface{} + +type Api interface { + AccountApi + TimeEntryApi + CustomerApi + ProjectApi + ServiceApi + UserApi +} + +type api struct { base string key string client *http.Client } -func NewMiteApi(base string, key string) Api { - return &miteApi{base: base, key: key, client: &http.Client{}} +func NewApi(base string, key string) Api { + return &api{base: base, key: key, client: &http.Client{}} } -func (a *miteApi) get(resource string, result interface{}) error { +func (a *api) get(resource string, result interface{}) error { req, err := http.NewRequest(http.MethodGet, a.url(resource), nil) if err != nil { return err @@ -53,7 +74,7 @@ func (a *miteApi) get(resource string, result interface{}) error { return json.NewDecoder(res.Body).Decode(result) } -func (a *miteApi) getParametrized(resource string, values url.Values, result interface{}) error { +func (a *api) getParametrized(resource string, values url.Values, result interface{}) error { u := &url.URL{} u.Path = resource u.RawQuery = values.Encode() @@ -61,7 +82,7 @@ func (a *miteApi) getParametrized(resource string, values url.Values, result int return a.get(u.String(), result) } -func (a *miteApi) post(resource string, body interface{}, result interface{}) error { +func (a *api) post(resource string, body interface{}, result interface{}) error { b, err := json.Marshal(body) if err != nil { return err @@ -89,7 +110,7 @@ func (a *miteApi) post(resource string, body interface{}, result interface{}) er return json.NewDecoder(res.Body).Decode(result) } -func (a *miteApi) patch(resource string, body interface{}) error { +func (a *api) patch(resource string, body interface{}) error { b, err := json.Marshal(body) if err != nil { return err @@ -117,7 +138,7 @@ func (a *miteApi) patch(resource string, body interface{}) error { return nil } -func (a *miteApi) delete(resource string) error { +func (a *api) delete(resource string) error { req, err := http.NewRequest(http.MethodDelete, a.url(resource), nil) if err != nil { return err @@ -139,11 +160,11 @@ func (a *miteApi) delete(resource string) error { return nil } -func (a *miteApi) url(resource string) string { +func (a *api) url(resource string) string { return fmt.Sprintf("%s/%s", a.base, resource) } -func (a *miteApi) check(res *http.Response) error { +func (a *api) check(res *http.Response) error { if res.StatusCode < 400 { return nil } diff --git a/mite/project.go b/mite/project.go index 745b991..0f31656 100644 --- a/mite/project.go +++ b/mite/project.go @@ -26,7 +26,7 @@ func (r *projectResponse) ToProject() *Project { } } -func (a *miteApi) Projects() ([]*Project, error) { +func (a *api) Projects() ([]*Project, error) { var prs []projectResponse err := a.get("projects.json", &prs) if err != nil { diff --git a/mite/service.go b/mite/service.go index 10dbdf2..6b77659 100644 --- a/mite/service.go +++ b/mite/service.go @@ -26,7 +26,7 @@ func (r *serviceResponse) ToService() *Service { } } -func (a *miteApi) Services() ([]*Service, error) { +func (a *api) Services() ([]*Service, error) { var srs []serviceResponse err := a.get("services.json", &srs) if err != nil { diff --git a/mite/time_entry.go b/mite/time_entry.go index 3815f4e..c455604 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -112,7 +112,7 @@ func (r *timeEntryResponse) ToTimeEntry() *TimeEntry { } } -func (a *miteApi) TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) { +func (a *api) TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) { var ter []timeEntryResponse err := a.getParametrized("time_entries.json", query.toValues(), &ter) if err != nil { @@ -127,7 +127,7 @@ func (a *miteApi) TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) { return timeEntries, nil } -func (a *miteApi) TimeEntry(id string) (*TimeEntry, error) { +func (a *api) TimeEntry(id string) (*TimeEntry, error) { ter := timeEntryResponse{} err := a.get(fmt.Sprintf("/time_entries/%s.json", id), &ter) if err != nil { @@ -137,7 +137,7 @@ func (a *miteApi) TimeEntry(id string) (*TimeEntry, error) { return ter.ToTimeEntry(), nil } -func (a *miteApi) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) { +func (a *api) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) { ter := timeEntryResponse{} err := a.post("/time_entries.json", command.toRequest(), &ter) if err != nil { @@ -147,10 +147,10 @@ func (a *miteApi) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) return ter.ToTimeEntry(), nil } -func (a *miteApi) EditTimeEntry(id string, command *TimeEntryCommand) error { +func (a *api) EditTimeEntry(id string, command *TimeEntryCommand) error { return a.patch(fmt.Sprintf("/time_entries/%s.json", id), command.toRequest()) } -func (a *miteApi) DeleteTimeEntry(id string) error { +func (a *api) DeleteTimeEntry(id string) error { return a.delete(fmt.Sprintf("/time_entries/%s.json", id)) }