Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 3, 2019
2 parents d9db780 + 0cf7a8d commit 3389da3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var entriesListCommand = &cobra.Command{
return
}

entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryParameters{
entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{
To: &to,
From: &from,
Direction: direction,
Expand Down
40 changes: 39 additions & 1 deletion mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)"
const layout = "2006-01-02"

type MiteApi interface {
TimeEntries(params *TimeEntryParameters) ([]*TimeEntry, error)
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
Projects() ([]*Project, error)
Services() ([]*Service, error)
}
Expand Down Expand Up @@ -78,3 +80,39 @@ 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 {
b, err := json.Marshal(body)
if err != nil {
return err
}

req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/%s", a.url, resource), bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Add("X-MiteApiKey", a.key)
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Content-Type", "application/json")

res, err := a.client.Do(req)

defer func() { _ = res.Body.Close() }()

return err
}

func (a *miteApi) delete(resource string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/%s", a.url, resource), nil)
if err != nil {
return err
}
req.Header.Add("X-MiteApiKey", a.key)
req.Header.Add("User-Agent", userAgent)

res, err := a.client.Do(req)

defer func() { _ = res.Body.Close() }()

return err
}
45 changes: 28 additions & 17 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,29 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
return r
}

// TODO: rename to TimeEntryQuery
type TimeEntryParameters struct {
type TimeEntryQuery struct {
From *time.Time
To *time.Time
Direction string
}

func (q *TimeEntryQuery) toValues() url.Values {
v := url.Values{}
if q != nil {
if q.From != nil {
v.Add("from", q.From.Format(layout))
}
if q.To != nil {
v.Add("to", q.To.Format(layout))
}
if q.Direction != "" {
v.Add("direction", q.Direction)
}
}

return v
}

type timeEntryRequest struct {
TimeEntry struct {
Date string `json:"date_at"`
Expand Down Expand Up @@ -89,22 +105,9 @@ func (r *timeEntryResponse) ToTimeEntry() *TimeEntry {
}
}

func (a *miteApi) TimeEntries(params *TimeEntryParameters) ([]*TimeEntry, error) {
values := url.Values{}
if params != nil {
if params.From != nil {
values.Add("from", params.From.Format(layout))
}
if params.To != nil {
values.Add("to", params.To.Format(layout))
}
if params.Direction != "" {
values.Add("direction", params.Direction)
}
}

func (a *miteApi) TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) {
ter := []timeEntryResponse{}
err := a.getParametrized("time_entries.json", values, &ter)
err := a.getParametrized("time_entries.json", query.toValues(), &ter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -136,3 +139,11 @@ func (a *miteApi) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error)

return ter.ToTimeEntry(), nil
}

func (a *miteApi) 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 {
return a.delete(fmt.Sprintf("/time_entries/%s.json", id))
}

0 comments on commit 3389da3

Please sign in to comment.