11package mite
22
33import (
4- "encoding/json"
5- "fmt"
64 "net/http"
7- "net/url"
8- "time"
95)
106
117const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)"
@@ -17,40 +13,6 @@ type MiteApi interface {
1713 TimeEntries (params * TimeEntryParameters ) ([]TimeEntry , error )
1814}
1915
20- type Project struct {
21- Id string
22- Name string
23- Note string
24- }
25-
26- type Service struct {
27- Id string
28- Name string
29- Note string
30- }
31-
32- type TimeEntry struct {
33- Id string
34- Note string
35- Duration time.Duration
36- Date time.Time
37- ProjectName string
38- ServiceName string
39- }
40-
41- type Direction int
42-
43- const (
44- DirectionAsc = Direction (0 )
45- DirectionDesc = Direction (1 )
46- )
47-
48- type TimeEntryParameters struct {
49- From * time.Time
50- To * time.Time
51- Direction * Direction
52- }
53-
5416type defaultApi struct {
5517 url string
5618 key string
@@ -60,170 +22,3 @@ type defaultApi struct {
6022func NewMiteApi (url string , key string ) MiteApi {
6123 return & defaultApi {url : url , key : key , client : & http.Client {}}
6224}
63-
64- func (a * defaultApi ) Projects () ([]Project , error ) {
65- req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/%s" , a .url , "projects.json" ), nil )
66- if err != nil {
67- return nil , err
68- }
69- req .Header .Add ("X-MiteApiKey" , a .key )
70- req .Header .Add ("User-Agent" , userAgent )
71-
72- res , err := a .client .Do (req )
73- if err != nil {
74- return nil , err
75- }
76- defer func () { _ = res .Body .Close () }()
77-
78- prs := []ProjectResponse {}
79- err = json .NewDecoder (res .Body ).Decode (& prs )
80- if err != nil {
81- return nil , err
82- }
83-
84- projects := []Project {}
85- for _ , pr := range prs {
86- projects = append (projects , pr .ToProject ())
87- }
88-
89- return projects , nil
90- }
91-
92- type ProjectResponse struct {
93- Project struct {
94- Id int `json:"id"`
95- Name string `json:"name"`
96- Note string `json:"note"`
97- } `json:"project"`
98- }
99-
100- func (r ProjectResponse ) ToProject () Project {
101- return Project {
102- Id : fmt .Sprintf ("%d" , r .Project .Id ),
103- Name : r .Project .Name ,
104- Note : r .Project .Note ,
105- }
106- }
107-
108- func (a * defaultApi ) Services () ([]Service , error ) {
109- req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/%s" , a .url , "services.json" ), nil )
110- if err != nil {
111- return nil , err
112- }
113- req .Header .Add ("X-MiteApiKey" , a .key )
114- req .Header .Add ("User-Agent" , userAgent )
115-
116- res , err := a .client .Do (req )
117- if err != nil {
118- return nil , err
119- }
120- defer func () { _ = res .Body .Close () }()
121-
122- srs := []ServiceResponse {}
123- err = json .NewDecoder (res .Body ).Decode (& srs )
124- if err != nil {
125- return nil , err
126- }
127-
128- services := []Service {}
129- for _ , sr := range srs {
130- services = append (services , sr .ToService ())
131- }
132-
133- return services , nil
134- }
135-
136- type ServiceResponse struct {
137- Service struct {
138- Id int `json:"id"`
139- Name string `json:"name"`
140- Note string `json:"note"`
141- } `json:"service"`
142- }
143-
144- func (r ServiceResponse ) ToService () Service {
145- return Service {
146- Id : fmt .Sprintf ("%d" , r .Service .Id ),
147- Name : r .Service .Name ,
148- Note : r .Service .Note ,
149- }
150- }
151-
152- func (a * defaultApi ) TimeEntries (params * TimeEntryParameters ) ([]TimeEntry , error ) {
153- values := url.Values {}
154- if params != nil {
155- if params .From != nil {
156- values .Add ("from" , params .From .Format (layout ))
157- }
158- if params .To != nil {
159- values .Add ("to" , params .To .Format (layout ))
160- }
161- if params .Direction != nil {
162- switch * params .Direction {
163- case DirectionAsc :
164- values .Add ("direction" , "asc" )
165- case DirectionDesc :
166- values .Add ("direction" , "desc" )
167- }
168- }
169- }
170-
171- u , err := url .Parse (fmt .Sprintf ("%s/%s" , a .url , "time_entries.json" ))
172- if err != nil {
173- return nil , err
174- }
175- u .RawQuery = values .Encode ()
176-
177- req , err := http .NewRequest ("GET" , u .String (), nil )
178- if err != nil {
179- return nil , err
180- }
181- req .Header .Add ("X-MiteApiKey" , a .key )
182- req .Header .Add ("User-Agent" , userAgent )
183-
184- res , err := a .client .Do (req )
185- if err != nil {
186- return nil , err
187- }
188- defer func () { _ = res .Body .Close () }()
189-
190- ter := []TimeEntryResponse {}
191- err = json .NewDecoder (res .Body ).Decode (& ter )
192- if err != nil {
193- return nil , err
194- }
195-
196- timeEntries := []TimeEntry {}
197- for _ , te := range ter {
198- timeEntries = append (timeEntries , te .ToTimeEntry ())
199- }
200-
201- return timeEntries , nil
202- }
203-
204- type TimeEntryResponse struct {
205- TimeEntry struct {
206- Id int `json:"id"`
207- Note string `json:"note"`
208- Minutes int `json:"minutes"`
209- Date string `json:"date_at"`
210- ProjectName string `json:"project_name"`
211- ServiceName string `json:"service_name"`
212- } `json:"time_entry"`
213- }
214-
215- func (r TimeEntryResponse ) ToTimeEntry () TimeEntry {
216- date , err := time .Parse (layout , r .TimeEntry .Date )
217- if err != nil {
218- panic (err )
219- }
220-
221- return TimeEntry {
222- Id : fmt .Sprintf ("%d" , r .TimeEntry .Id ),
223- Note : r .TimeEntry .Note ,
224- Duration : time .Duration (r .TimeEntry .Minutes ) * time .Minute ,
225- Date : date ,
226- ProjectName : r .TimeEntry .ProjectName ,
227- ServiceName : r .TimeEntry .ServiceName ,
228- }
229- }
0 commit comments