/
users.go
113 lines (86 loc) · 2.81 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
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
// Copyright 2020 Herman Slatman
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"strconv"
"github.com/hslatman/balena-sdk-go/pkg/models"
"github.com/m7shapan/njson"
"github.com/tidwall/gjson"
)
type UsersResource struct {
client *Client
endpoint string
modifiers *ODataModifiers
// TODO: context, configuration
}
func NewUsersResource(c *Client) *UsersResource {
return &UsersResource{
client: c,
endpoint: string(usersEndpoint),
modifiers: NewODataModifiers(c),
}
}
func (c *Client) Users() *UsersResource {
return NewUsersResource(c)
}
func (c *Client) WhoAmI() (models.WhoAmI, error) {
w := models.WhoAmI{}
url := "https://api.balena-cloud.com/user/v1" // This endpoint uses a different format than all other v5 APIs
resp, err := c.get(url+string(whoamiEndpoint), nil)
if err != nil {
return w, err
}
if err := njson.Unmarshal(resp.Body(), &w); err != nil {
return w, err
}
return w, nil
}
func (r *UsersResource) AssociatedWithApplication(applicationID int) *UsersResource {
// TODO: this could end up in errors when more actions are applied fluently
r.endpoint = r.endpoint + "__is_member_of__application"
r.modifiers.AddFilter("is_member_of__application%20eq%20'" + strconv.Itoa(applicationID) + "'")
r.modifiers.AddExpand("user($select=id,username,actor),application_membership_role($select=id,name,actor)")
// TODO: the values returned are also a bit weird, because there's a list and in there there's an object ...
return r
}
func (r *UsersResource) Select(s string) *UsersResource {
r.modifiers.AddSelect(s)
return r
}
func (r *UsersResource) Filter(f string) *UsersResource {
r.modifiers.AddFilter(f)
return r
}
func (r *UsersResource) Get() (map[int]models.User, error) {
users := make(map[int]models.User)
resp, err := r.client.get(r.endpoint, r.modifiers)
if err != nil {
return users, err
}
data := gjson.GetBytes(resp.Body(), "d") // get data; a list of results
for _, u := range data.Array() {
user := models.User{}
if err := njson.Unmarshal([]byte(u.Raw), &user); err != nil {
return users, err // TODO: don't do early return, but just skip this one and aggregate error?
}
users[user.ID] = user
}
return users, nil
}
func (r *UsersResource) FindByID(userID int) *UserResource {
return NewUserResource(
r.client,
userID,
)
}