Skip to content

Commit

Permalink
Stream-line url handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 10, 2019
1 parent fd3939f commit 8c25d5a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 36 deletions.
50 changes: 24 additions & 26 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
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
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
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)
}
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

0 comments on commit 8c25d5a

Please sign in to comment.