Skip to content

Commit 34efdef

Browse files
author
Ulrich Lissé
committed
Extract time entry entity
1 parent 81402d7 commit 34efdef

File tree

2 files changed

+110
-105
lines changed

2 files changed

+110
-105
lines changed

mite/api.go

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package mite
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"net/http"
7-
"net/url"
8-
"time"
95
)
106

117
const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)"
@@ -17,28 +13,6 @@ type MiteApi interface {
1713
TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error)
1814
}
1915

20-
type TimeEntry struct {
21-
Id string
22-
Note string
23-
Duration time.Duration
24-
Date time.Time
25-
ProjectName string
26-
ServiceName string
27-
}
28-
29-
type Direction int
30-
31-
const (
32-
DirectionAsc = Direction(0)
33-
DirectionDesc = Direction(1)
34-
)
35-
36-
type TimeEntryParameters struct {
37-
From *time.Time
38-
To *time.Time
39-
Direction *Direction
40-
}
41-
4216
type defaultApi struct {
4317
url string
4418
key string
@@ -48,82 +22,3 @@ type defaultApi struct {
4822
func NewMiteApi(url string, key string) MiteApi {
4923
return &defaultApi{url: url, key: key, client: &http.Client{}}
5024
}
51-
52-
func (a *defaultApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) {
53-
values := url.Values{}
54-
if params != nil {
55-
if params.From != nil {
56-
values.Add("from", params.From.Format(layout))
57-
}
58-
if params.To != nil {
59-
values.Add("to", params.To.Format(layout))
60-
}
61-
if params.Direction != nil {
62-
switch *params.Direction {
63-
case DirectionAsc:
64-
values.Add("direction", "asc")
65-
case DirectionDesc:
66-
values.Add("direction", "desc")
67-
}
68-
}
69-
}
70-
71-
u, err := url.Parse(fmt.Sprintf("%s/%s", a.url, "time_entries.json"))
72-
if err != nil {
73-
return nil, err
74-
}
75-
u.RawQuery = values.Encode()
76-
77-
req, err := http.NewRequest("GET", u.String(), nil)
78-
if err != nil {
79-
return nil, err
80-
}
81-
req.Header.Add("X-MiteApiKey", a.key)
82-
req.Header.Add("User-Agent", userAgent)
83-
84-
res, err := a.client.Do(req)
85-
if err != nil {
86-
return nil, err
87-
}
88-
defer func() { _ = res.Body.Close() }()
89-
90-
ter := []TimeEntryResponse{}
91-
err = json.NewDecoder(res.Body).Decode(&ter)
92-
if err != nil {
93-
return nil, err
94-
}
95-
96-
timeEntries := []TimeEntry{}
97-
for _, te := range ter {
98-
timeEntries = append(timeEntries, te.ToTimeEntry())
99-
}
100-
101-
return timeEntries, nil
102-
}
103-
104-
type TimeEntryResponse struct {
105-
TimeEntry struct {
106-
Id int `json:"id"`
107-
Note string `json:"note"`
108-
Minutes int `json:"minutes"`
109-
Date string `json:"date_at"`
110-
ProjectName string `json:"project_name"`
111-
ServiceName string `json:"service_name"`
112-
} `json:"time_entry"`
113-
}
114-
115-
func (r TimeEntryResponse) ToTimeEntry() TimeEntry {
116-
date, err := time.Parse(layout, r.TimeEntry.Date)
117-
if err != nil {
118-
panic(err)
119-
}
120-
121-
return TimeEntry{
122-
Id: fmt.Sprintf("%d", r.TimeEntry.Id),
123-
Note: r.TimeEntry.Note,
124-
Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute,
125-
Date: date,
126-
ProjectName: r.TimeEntry.ProjectName,
127-
ServiceName: r.TimeEntry.ServiceName,
128-
}
129-
}

mite/time_entry.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package mite
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"time"
9+
)
10+
11+
type TimeEntry struct {
12+
Id string
13+
Note string
14+
Duration time.Duration
15+
Date time.Time
16+
ProjectName string
17+
ServiceName string
18+
}
19+
20+
type Direction int
21+
22+
const (
23+
DirectionAsc = Direction(0)
24+
DirectionDesc = Direction(1)
25+
)
26+
27+
type TimeEntryParameters struct {
28+
From *time.Time
29+
To *time.Time
30+
Direction *Direction
31+
}
32+
33+
func (a *defaultApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) {
34+
values := url.Values{}
35+
if params != nil {
36+
if params.From != nil {
37+
values.Add("from", params.From.Format(layout))
38+
}
39+
if params.To != nil {
40+
values.Add("to", params.To.Format(layout))
41+
}
42+
if params.Direction != nil {
43+
switch *params.Direction {
44+
case DirectionAsc:
45+
values.Add("direction", "asc")
46+
case DirectionDesc:
47+
values.Add("direction", "desc")
48+
}
49+
}
50+
}
51+
52+
u, err := url.Parse(fmt.Sprintf("%s/%s", a.url, "time_entries.json"))
53+
if err != nil {
54+
return nil, err
55+
}
56+
u.RawQuery = values.Encode()
57+
58+
req, err := http.NewRequest("GET", u.String(), nil)
59+
if err != nil {
60+
return nil, err
61+
}
62+
req.Header.Add("X-MiteApiKey", a.key)
63+
req.Header.Add("User-Agent", userAgent)
64+
65+
res, err := a.client.Do(req)
66+
if err != nil {
67+
return nil, err
68+
}
69+
defer func() { _ = res.Body.Close() }()
70+
71+
ter := []TimeEntryResponse{}
72+
err = json.NewDecoder(res.Body).Decode(&ter)
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
timeEntries := []TimeEntry{}
78+
for _, te := range ter {
79+
timeEntries = append(timeEntries, te.ToTimeEntry())
80+
}
81+
82+
return timeEntries, nil
83+
}
84+
85+
type TimeEntryResponse struct {
86+
TimeEntry struct {
87+
Id int `json:"id"`
88+
Note string `json:"note"`
89+
Minutes int `json:"minutes"`
90+
Date string `json:"date_at"`
91+
ProjectName string `json:"project_name"`
92+
ServiceName string `json:"service_name"`
93+
} `json:"time_entry"`
94+
}
95+
96+
func (r TimeEntryResponse) ToTimeEntry() TimeEntry {
97+
date, err := time.Parse(layout, r.TimeEntry.Date)
98+
if err != nil {
99+
panic(err)
100+
}
101+
102+
return TimeEntry{
103+
Id: fmt.Sprintf("%d", r.TimeEntry.Id),
104+
Note: r.TimeEntry.Note,
105+
Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute,
106+
Date: date,
107+
ProjectName: r.TimeEntry.ProjectName,
108+
ServiceName: r.TimeEntry.ServiceName,
109+
}
110+
}

0 commit comments

Comments
 (0)