-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroles.go
100 lines (79 loc) · 2.08 KB
/
roles.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
package latitude
import "path"
const roleBasePath = "/roles"
// RoleService interface defines available role methods
type RoleService interface {
Get(string, *GetOptions) (*Role, *Response, error)
List(*ListOptions) ([]Role, *Response, error)
}
// RoleServiceOp implements RoleService
type RoleServiceOp struct {
client requestDoer
}
type AvailableRole string
const (
Owner AvailableRole = "owner"
Administrator AvailableRole = "administrator"
Collaborator AvailableRole = "collaborator"
Billing AvailableRole = "billing"
)
type RoleGetResponse struct {
Data RoleData `json:"data"`
Meta meta `json:"meta"`
}
type RoleData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes RoleAttributes `json:"attributes"`
}
type RoleAttributes struct {
Name string `json:"name"`
}
type RoleListResponse struct {
Data []RoleData `json:"data"`
Meta meta `json:"meta"`
}
type Role struct {
ID string `json:"id"`
Name string `json:"name"`
}
func NewFlatRole(rd RoleData) Role {
return Role{
ID: rd.ID,
Name: rd.Attributes.Name,
}
}
func NewFlatRoleList(rd []RoleData) []Role {
var res []Role
for _, role := range rd {
res = append(res, NewFlatRole(role))
}
return res
}
func (s *RoleServiceOp) Get(RoleID string, opts *GetOptions) (*Role, *Response, error) {
endpointPath := path.Join(roleBasePath, RoleID)
apiPathQuery := opts.WithQuery(endpointPath)
role := new(RoleGetResponse)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, role)
if err != nil {
return nil, resp, err
}
flatRole := NewFlatRole(role.Data)
return &flatRole, resp, err
}
func (s *RoleServiceOp) List(opts *ListOptions) ([]Role, *Response, error) {
apiPathQuery := opts.WithQuery(roleBasePath)
roles := []Role{}
for {
res := new(RoleListResponse)
resp, err := s.client.DoRequest("GET", apiPathQuery, nil, res)
if err != nil {
return nil, resp, err
}
roles = append(roles, NewFlatRoleList(res.Data)...)
if apiPathQuery = nextPage(res.Meta, opts); apiPathQuery != "" {
continue
}
return roles, resp, nil
}
}