-
Notifications
You must be signed in to change notification settings - Fork 2
/
basecamp.go
211 lines (192 loc) · 5.04 KB
/
basecamp.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
package basecamp
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const (
userAgent = "go-basecamp"
baseURL = "https://basecamp.com/%d/api/v1/%s"
authURL = "https://launchpad.37signals.com/authorization.json"
)
type (
Client struct {
AccessToken string
ModifiedSince *time.Time
}
Account struct {
Id int `json:"id"`
Name string `json:"name"`
Href string `json:"href"`
Product string `json:"product"`
}
Person struct {
Id int `json:"id"`
Name string `json:"name"`
Email string `json:"email_address"`
Admin bool `json:"admin"`
}
Project struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Archived bool `json:"archived"`
Starred bool `json:"starred"`
UpdatedAt time.Time `json:"updated_at"`
}
Todo struct {
Id int `json:"id"`
Content string `json:"content"`
DueAt string `json:"due_at"`
UpdatedAt time.Time `json:"updated_at"`
}
TodoList struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Completed bool `json:"completed"`
CompletedCount int `json:"completed_count"`
RemainingCount int `json:"remaining_count"`
ProjectId int `json:"project_id"`
UpdatedAt time.Time `json:"updated_at"`
Bucket struct {
Id int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
Todos struct {
Remaining []*Todo `json:"remaining"`
Completed []*Todo `json:"completed"`
}
}
)
func (c *Client) GetAccounts() ([]*Account, error) {
b, err := c.get(authURL)
if err != nil {
return nil, err
}
var authorizations map[string]interface{}
if err := json.Unmarshal(b, &authorizations); err != nil {
return nil, err
}
accounts, ok := authorizations["accounts"].([]interface{})
if !ok {
return nil, errors.New("'accounts' not found in response JSON")
}
var result []*Account
for _, data := range accounts {
values := data.(map[string]interface{})
account := &Account{
Id: int(values["id"].(float64)),
Name: values["name"].(string),
Href: values["href"].(string),
Product: values["product"].(string),
}
if account.Product != "bcx" {
continue
}
result = append(result, account)
}
return result, nil
}
func (c *Client) GetPeople(accountID int) ([]*Person, error) {
url := fmt.Sprintf(baseURL, accountID, "people.json")
b, err := c.get(url)
if err != nil {
return nil, err
}
var result []*Person
if err := json.Unmarshal(b, &result); err != nil {
return nil, err
}
return result, nil
}
func (c *Client) GetProjects(accountID int) ([]*Project, error) {
url := fmt.Sprintf(baseURL, accountID, "projects.json")
b, err := c.get(url)
if err != nil {
return nil, err
}
var result []*Project
if err := json.Unmarshal(b, &result); err != nil {
return nil, err
}
return result, nil
}
func (c *Client) GetTodoLists(accountID int) ([]*TodoList, error) {
return c.fetchTodoLists(accountID, "todolists.json")
}
func (c *Client) GetCompletedTodoLists(accountID int) ([]*TodoList, error) {
return c.fetchTodoLists(accountID, "todolists/completed.json")
}
func (c *Client) GetAllTodoLists(accountID int) ([]*TodoList, error) {
remaining, err := c.GetTodoLists(accountID)
if err != nil {
return nil, err
}
completed, err := c.GetCompletedTodoLists(accountID)
if err != nil {
return nil, err
}
return append(remaining, completed...), nil
}
func (c *Client) fetchTodoLists(accountID int, listURL string) ([]*TodoList, error) {
url := fmt.Sprintf(baseURL, accountID, listURL)
b, err := c.get(url)
if err != nil {
return nil, err
}
var result []*TodoList
if err := json.Unmarshal(b, &result); err != nil {
return nil, err
}
for _, todoList := range result {
if todoList.Bucket.Type == "Project" {
todoList.ProjectId = todoList.Bucket.Id
}
}
return result, nil
}
func (c *Client) GetTodoList(accountID, projectID, listID int) (*TodoList, error) {
url := fmt.Sprintf(baseURL, accountID, fmt.Sprintf("projects/%d/todolists/%d.json", projectID, listID))
b, err := c.get(url)
if err != nil {
return nil, err
}
var result *TodoList
if err := json.Unmarshal(b, &result); err != nil {
return nil, err
}
return result, nil
}
func (c *Client) get(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Authorization", "Bearer "+c.AccessToken)
if c.ModifiedSince != nil {
req.Header.Set("If-Modified-Since", c.ModifiedSince.Format(http.TimeFormat))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if http.StatusNotModified == resp.StatusCode {
return []byte("null"), nil
}
if http.StatusOK != resp.StatusCode {
return b, fmt.Errorf("%s failed with status code %d", url, resp.StatusCode)
}
return b, nil
}