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,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- 
4216type  defaultApi  struct  {
4317	url     string 
4418	key     string 
@@ -48,82 +22,3 @@ type defaultApi struct {
4822func  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- }
0 commit comments