From 8c25d5ae5dd580e4e40636825110fcf6b344753c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20Liss=C3=A9?= Date: Wed, 10 Apr 2019 13:55:36 +0200 Subject: [PATCH] Stream-line url handling --- mite/api.go | 50 ++++++++++++++++++++++------------------------ mite/project.go | 2 +- mite/service.go | 2 +- mite/time_entry.go | 10 +++++----- mite/tracker.go | 6 +++--- 5 files changed, 34 insertions(+), 36 deletions(-) diff --git a/mite/api.go b/mite/api.go index e1f5412..71d171e 100644 --- a/mite/api.go +++ b/mite/api.go @@ -23,24 +23,30 @@ 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, error) { - ua := fmt.Sprintf(userAgentTemplate, version) - return &api{base: base, key: key, userAgent: ua, client: &http.Client{}}, nil +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("User-Agent", a.agent) req.Header.Add("X-MiteApiKey", a.key) res, err := a.client.Do(req) @@ -56,27 +62,19 @@ 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("User-Agent", a.agent) req.Header.Add("X-MiteApiKey", a.key) res, err := a.client.Do(req) @@ -102,13 +100,13 @@ 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("User-Agent", a.agent) req.Header.Add("X-MiteApiKey", a.key) res, err := a.client.Do(req) @@ -129,12 +127,12 @@ 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("User-Agent", a.agent) req.Header.Add("X-MiteApiKey", a.key) res, err := a.client.Do(req) @@ -154,8 +152,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 { diff --git a/mite/project.go b/mite/project.go index 00bf89f..8269aa0 100644 --- a/mite/project.go +++ b/mite/project.go @@ -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 } diff --git a/mite/service.go b/mite/service.go index 43ca9c8..ed03c01 100644 --- a/mite/service.go +++ b/mite/service.go @@ -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 } diff --git a/mite/time_entry.go b/mite/time_entry.go index 316a731..8062ec7 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -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 } @@ -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 } @@ -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 } @@ -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) } diff --git a/mite/tracker.go b/mite/tracker.go index e7c47f4..6310405 100644 --- a/mite/tracker.go +++ b/mite/tracker.go @@ -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 } @@ -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 } @@ -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 }