-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathusers.go
82 lines (60 loc) · 1.78 KB
/
users.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
package quip
import (
"strings"
"github.com/mitchellh/mapstructure"
)
type User struct {
Id string
Name string
Affinity float64
ChatThreadId string `mapstructure:"chat_thread_id"`
DesktopFolderId string `mapstructure:"desktop_folder_id"`
ArchiveFolderId string `mapstructure:"archive_folder_id"`
}
type GetUserParams struct {
Id string
}
type GetUsersParams struct {
Ids []string
}
func (q *Client) GetUser(params *GetUserParams) *User {
required(params.Id, "Id is required for /users/id")
resp := q.getJson(apiUrlResource("users/"+params.Id), map[string]string{})
parsed := parseJsonObject(resp)
return hydrateUser(parsed)
}
func (q *Client) GetUsers(params *GetUsersParams) []*User {
required(params.Ids, "Ids is required for /users/ids")
resp := q.getJson(apiUrlResource("users/"+strings.Join(params.Ids, ",")), map[string]string{})
parsed := parseJsonObject(resp)
return hydrateUsersMap(parsed)
}
func (q *Client) GetContacts() []*User {
resp := q.getJson(apiUrlResource("users/contacts"), map[string]string{})
parsed := parseJsonArray(resp)
return hydrateUsersArray(parsed)
}
func (q *Client) GetAuthenticatedUser() *User {
resp := q.getJson(apiUrlResource("users/current"), map[string]string{})
parsed := parseJsonObject(resp)
return hydrateUser(parsed)
}
func hydrateUser(resp interface{}) *User {
var user User
mapstructure.Decode(resp, &user)
return &user
}
func hydrateUsersMap(resp map[string]interface{}) []*User {
users := make([]*User, 0, len(resp))
for _, body := range resp {
users = append(users, hydrateUser(body))
}
return users
}
func hydrateUsersArray(resp []interface{}) []*User {
users := make([]*User, 0, len(resp))
for _, body := range resp {
users = append(users, hydrateUser(body))
}
return users
}