forked from toggl/go-freshbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freshbooks.go
272 lines (255 loc) · 6.67 KB
/
freshbooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package freshbooks
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"github.com/tambet/oauthplain"
"io/ioutil"
"net/http"
)
type (
Api struct {
apiUrl string
apiToken string
oAuthToken *oauthplain.Token
perPage int
users []User
tasks []Task
clients []Client
projects []Project
}
Request struct {
XMLName xml.Name `xml:"request"`
Method string `xml:"method,attr"`
PerPage int `xml:"per_page"`
Page int `xml:"page"`
}
TimeEntryRequest struct {
XMLName xml.Name `xml:"request"`
Method string `xml:"method,attr"`
TimeEntry TimeEntry `xml:"time_entry"`
}
Response struct {
Error string `xml:"error"`
Clients ClientList `xml:"clients"`
Projects ProjectList `xml:"projects"`
Tasks TaskList `xml:"tasks"`
Users UserList `xml:"staff_members"`
}
TimeEntryResponse struct {
Status string `xml:"status,attr"`
Error string `xml:"error"`
Code string `xml:"code"`
Field string `xml:"field"`
TimeEntryId int `xml:"time_entry_id"`
}
Pagination struct {
Page int `xml:"page,attr"`
Total int `xml:"total,attr"`
PerPage int `xml:"per_page,attr"`
}
ClientList struct {
Pagination
Clients []Client `xml:"client"`
}
ProjectList struct {
Pagination
Projects []Project `xml:"project"`
}
TaskList struct {
Pagination
Tasks []Task `xml:"task"`
}
UserList struct {
Pagination
Users []User `xml:"member"`
}
Client struct {
ClientId int `xml:"client_id"`
Name string `xml:"organization"`
}
Project struct {
ProjectId int `xml:"project_id"`
ClientId string `xml:"client_id"`
Name string `xml:"name"`
TaskIds []int `xml:"tasks>task>task_id"`
UserIds []int `xml:"staff>staff>staff_id"`
}
Task struct {
TaskId int `xml:"task_id"`
Name string `xml:"name"`
}
User struct {
UserId int `xml:"staff_id"`
Email string `xml:"email"`
FirstName string `xml:"first_name"`
LastName string `xml:"last_name"`
}
TimeEntry struct {
TimeEntryId int `xml:"time_entry_id"`
ProjectId int `xml:"project_id"` // Required
TaskId int `xml:"task_id"` // Required
UserId int `xml:"staff_id"` // Required
Date string `xml:"date"` // Required
Notes string `xml:"notes"`
Hours float64 `xml:"hours"`
}
)
func NewApi(account string, token interface{}) *Api {
url := fmt.Sprintf("https://%s.freshbooks.com/api/2.1/xml-in", account)
fb := Api{apiUrl: url, perPage: 25}
fb.users = make([]User, 0)
fb.tasks = make([]Task, 0)
fb.clients = make([]Client, 0)
fb.projects = make([]Project, 0)
switch token.(type) {
case string:
fb.apiToken = token.(string)
case *oauthplain.Token:
fb.oAuthToken = token.(*oauthplain.Token)
}
return &fb
}
func (this *Api) Clients() ([]Client, error) {
err := this.fetchClients(1)
return this.clients, err
}
func (this *Api) Projects() ([]Project, error) {
err := this.fetchProjects(1)
return this.projects, err
}
func (this *Api) Tasks() ([]Task, error) {
err := this.fetchTasks(1)
return this.tasks, err
}
func (this *Api) Users() ([]User, error) {
err := this.fetchUsers(1)
return this.users, err
}
func (this *Api) fetchClients(page int) error {
request := &Request{Method: "client.list", Page: page, PerPage: this.perPage}
result, err := this.makeRequest(request)
if err != nil {
return err
}
parsedInto := Response{}
if err := xml.Unmarshal(*result, &parsedInto); err != nil {
return err
}
if len(parsedInto.Error) > 0 {
return errors.New(parsedInto.Error)
}
this.clients = append(this.clients, parsedInto.Clients.Clients...)
if parsedInto.Clients.Total > parsedInto.Clients.PerPage*page {
return this.fetchClients(page + 1)
}
return nil
}
func (this *Api) fetchProjects(page int) error {
request := &Request{Method: "project.list", Page: page, PerPage: this.perPage}
result, err := this.makeRequest(request)
if err != nil {
return err
}
parsedInto := Response{}
if err := xml.Unmarshal(*result, &parsedInto); err != nil {
return (err)
}
if len(parsedInto.Error) > 0 {
return errors.New(parsedInto.Error)
}
this.projects = append(this.projects, parsedInto.Projects.Projects...)
if parsedInto.Projects.Total > parsedInto.Projects.PerPage*page {
return this.fetchProjects(page + 1)
}
return nil
}
func (this *Api) fetchTasks(page int) error {
request := &Request{Method: "task.list", Page: page, PerPage: this.perPage}
result, err := this.makeRequest(request)
if err != nil {
return err
}
parsedInto := Response{}
if err := xml.Unmarshal(*result, &parsedInto); err != nil {
return err
}
if len(parsedInto.Error) > 0 {
return errors.New(parsedInto.Error)
}
this.tasks = append(this.tasks, parsedInto.Tasks.Tasks...)
if parsedInto.Tasks.Total > parsedInto.Tasks.PerPage*page {
return this.fetchTasks(page + 1)
}
return nil
}
func (this *Api) fetchUsers(page int) error {
request := &Request{Method: "staff.list", Page: page, PerPage: this.perPage}
result, err := this.makeRequest(request)
if err != nil {
return err
}
parsedInto := Response{}
if err := xml.Unmarshal(*result, &parsedInto); err != nil {
return err
}
if len(parsedInto.Error) > 0 {
return errors.New(parsedInto.Error)
}
this.users = append(this.users, parsedInto.Users.Users...)
if parsedInto.Users.Total > parsedInto.Users.PerPage*page {
return this.fetchUsers(page + 1)
}
return nil
}
func (this *Api) SaveTimeEntry(timeEntry *TimeEntry) (int, error) {
var method string
if timeEntry.TimeEntryId != 0 {
method = "time_entry.update"
} else {
method = "time_entry.create"
}
request := &TimeEntryRequest{Method: method, TimeEntry: *timeEntry}
result, err := this.makeRequest(request)
if err != nil {
return 0, err
}
parsedInto := TimeEntryResponse{}
if err := xml.Unmarshal(*result, &parsedInto); err != nil {
return 0, err
}
if parsedInto.Status == "ok" {
return parsedInto.TimeEntryId, nil
}
return 0, errors.New(parsedInto.Error)
}
func (this *Api) makeRequest(request interface{}) (*[]byte, error) {
xmlRequest, err := xml.MarshalIndent(request, "", " ")
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", this.apiUrl, bytes.NewBuffer(xmlRequest))
if err != nil {
return nil, err
}
if this.apiToken != "" {
req.SetBasicAuth(this.apiToken, "X")
} else if this.oAuthToken != nil {
header := this.oAuthToken.AuthHeader()
req.Header.Set("Authorization", header)
}
response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, errors.New(response.Status)
}
result, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return &result, nil
}