Skip to content

Commit

Permalink
Extract service entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 2, 2019
1 parent d1fc299 commit 81402d7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
50 changes: 0 additions & 50 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ type MiteApi interface {
TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error)
}

type Service struct {
Id string
Name string
Note string
}

type TimeEntry struct {
Id string
Note string
Expand Down Expand Up @@ -55,50 +49,6 @@ func NewMiteApi(url string, key string) MiteApi {
return &defaultApi{url: url, key: key, client: &http.Client{}}
}

func (a *defaultApi) Services() ([]Service, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", a.url, "services.json"), nil)
if err != nil {
return nil, err
}
req.Header.Add("X-MiteApiKey", a.key)
req.Header.Add("User-Agent", userAgent)

res, err := a.client.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = res.Body.Close() }()

srs := []ServiceResponse{}
err = json.NewDecoder(res.Body).Decode(&srs)
if err != nil {
return nil, err
}

services := []Service{}
for _, sr := range srs {
services = append(services, sr.ToService())
}

return services, nil
}

type ServiceResponse struct {
Service struct {
Id int `json:"id"`
Name string `json:"name"`
Note string `json:"note"`
} `json:"service"`
}

func (r ServiceResponse) ToService() Service {
return Service{
Id: fmt.Sprintf("%d", r.Service.Id),
Name: r.Service.Name,
Note: r.Service.Note,
}
}

func (a *defaultApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) {
values := url.Values{}
if params != nil {
Expand Down
57 changes: 57 additions & 0 deletions mite/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mite

import (
"encoding/json"
"fmt"
"net/http"
)

type Service struct {
Id string
Name string
Note string
}

func (a *defaultApi) Services() ([]Service, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", a.url, "services.json"), nil)
if err != nil {
return nil, err
}
req.Header.Add("X-MiteApiKey", a.key)
req.Header.Add("User-Agent", userAgent)

res, err := a.client.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = res.Body.Close() }()

srs := []ServiceResponse{}
err = json.NewDecoder(res.Body).Decode(&srs)
if err != nil {
return nil, err
}

services := []Service{}
for _, sr := range srs {
services = append(services, sr.ToService())
}

return services, nil
}

type ServiceResponse struct {
Service struct {
Id int `json:"id"`
Name string `json:"name"`
Note string `json:"note"`
} `json:"service"`
}

func (r ServiceResponse) ToService() Service {
return Service{
Id: fmt.Sprintf("%d", r.Service.Id),
Name: r.Service.Name,
Note: r.Service.Note,
}
}

0 comments on commit 81402d7

Please sign in to comment.