Skip to content

Commit

Permalink
Decompose mite api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 4, 2019
1 parent adec9dd commit 94bed46
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 32 additions & 11 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,15 +74,15 @@ 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()

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion mite/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion mite/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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))
}

0 comments on commit 94bed46

Please sign in to comment.