-
Notifications
You must be signed in to change notification settings - Fork 10
/
basecamp.go
134 lines (122 loc) · 3.08 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
package main
import (
"fmt"
"github.com/toggl/go-basecamp"
)
type BasecampService struct {
workspaceID int
AccountID int
AccessToken string
}
func (s *BasecampService) Name() string {
return "basecamp"
}
func (s *BasecampService) WorkspaceID() int {
return s.workspaceID
}
func (s *BasecampService) keyFor(objectType string) string {
return fmt.Sprintf("basecamp:account:%d:%s", s.AccountID, objectType)
}
func (s *BasecampService) setAuthData(a *Authorization) {
s.AccessToken = a.AccessToken
}
func (s *BasecampService) setAccount(accountID int) {
s.AccountID = accountID
}
// Map basecamp accounts to local accounts
func (s *BasecampService) Accounts() ([]*Account, error) {
c := basecamp.Client{AccessToken: s.AccessToken}
foreignObjects, err := c.GetAccounts()
if err != nil {
return nil, err
}
var accounts []*Account
for _, object := range foreignObjects {
account := Account{
ID: object.Id,
Name: object.Name,
}
accounts = append(accounts, &account)
}
return accounts, nil
}
// Map basecamp people to local users
func (s *BasecampService) Users() ([]*User, error) {
c := basecamp.Client{AccessToken: s.AccessToken}
foreignObjects, err := c.GetPeople(s.AccountID)
if err != nil {
return nil, err
}
var users []*User
for _, object := range foreignObjects {
user := User{
ForeignID: object.Id,
Name: object.Name,
Email: object.Email,
}
users = append(users, &user)
}
return users, nil
}
// Map basecamp projects to projects
func (s *BasecampService) Projects() ([]*Project, error) {
c := basecamp.Client{AccessToken: s.AccessToken}
foreignObjects, err := c.GetProjects(s.AccountID)
if err != nil {
return nil, err
}
var projects []*Project
for _, object := range foreignObjects {
project := Project{
Active: true,
ForeignID: object.Id,
Name: object.Name,
}
projects = append(projects, &project)
}
return projects, nil
}
// Map basecamp todos to tasks
func (s *BasecampService) Tasks() ([]*Task, error) {
c := basecamp.Client{AccessToken: s.AccessToken}
foreignObjects, err := c.GetTodoLists(s.AccountID)
if err != nil {
return nil, err
}
var tasks []*Task
for _, object := range foreignObjects {
todoList, err := c.GetTodoList(s.AccountID, object.ProjectId, object.Id)
if err != nil {
return nil, err
}
for _, todo := range todoList.Todos.Remaining {
task := Task{
ForeignID: todo.Id,
Name: fmt.Sprintf("[%s] %s", object.Name, todo.Content),
Active: true,
foreignProjectID: object.ProjectId,
}
tasks = append(tasks, &task)
}
}
return tasks, nil
}
// Map basecamp todolists to tasks
func (s *BasecampService) TodoLists() ([]*Task, error) {
c := basecamp.Client{AccessToken: s.AccessToken}
foreignObjects, err := c.GetTodoLists(s.AccountID)
if err != nil {
return nil, err
}
var tasks []*Task
for _, object := range foreignObjects {
task := Task{
ForeignID: object.Id,
Name: object.Name,
Active: !object.Completed,
foreignProjectID: object.ProjectId,
}
tasks = append(tasks, &task)
}
return tasks, nil
}