-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapi_user.go
230 lines (211 loc) · 6.02 KB
/
api_user.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Treasure Data API client for Go
//
// Copyright (C) 2014 Treasure Data, Inc.
//
// 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 td_client
import (
"fmt"
"net/url"
"time"
)
// AuthenticateResult is result of authenticate API
type AuthenticateResult struct {
Name string
APIKey string
}
var authenticateSchema = map[string]interface{}{
"name": "",
"apikey": "",
}
// ListUsersResultElement represents an item of the result of ListUsers API
type ListUsersResultElement struct {
ID int
FirstName string
LastName string
Email string
Phone string
GravatarURL string
Administrator bool
CreatedAt time.Time
UpdatedAt time.Time
Name string
AccountOwner bool
Organization string
Roles []string
}
// ListUsersResult is a collection of ListUsersResultElement
type ListUsersResult []ListUsersResultElement
var listUsersSchema = map[string]interface{}{
"users": []map[string]interface{}{
map[string]interface{}{
"id": 0,
"first_name": Optional{"", ""},
"last_name": Optional{"", ""},
"email": "",
"phone": Optional{"", ""},
"gravatar_url": "",
"administrator": false,
"created_at": time.Time{},
"updated_at": time.Time{},
"name": "",
"account_owner": false,
"organization": Optional{"", ""},
"roles": []string{},
},
},
}
// ListAPIKeysResult represents the result of ListAPIKeys API
type ListAPIKeysResult struct {
APIKeys []string
}
var listAPIKeysSchema = map[string]interface{}{
"apikeys": []string{},
}
// AddAPIKeyResult represents the result of AddAPIKey API
type AddAPIKeyResult struct {
APIKey string
}
var addAPIKeySchema = map[string]interface{}{
"apikey": "",
}
func (client *TDClient) Authenticate(email, password string) (*AuthenticateResult, error) {
params := url.Values{}
params.Set("user", email)
params.Set("password", password)
resp, err := client.post("/v3/user/authenticate", params)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "Authentication failed", nil)
}
js, err := client.checkedJson(resp, authenticateSchema)
if err != nil {
return nil, err
}
return &AuthenticateResult{
Name: js["name"].(string),
APIKey: js["apikey"].(string),
}, nil
}
func (client *TDClient) ListUsers() (*ListUsersResult, error) {
resp, err := client.get("/v3/user/list", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "List users failed", nil)
}
js, err := client.checkedJson(resp, listUsersSchema)
if err != nil {
return nil, err
}
users := js["users"].([]map[string]interface{})
retval := make(ListUsersResult, len(users))
for i, v := range users {
retval[i] = ListUsersResultElement{
ID: v["id"].(int),
FirstName: v["first_name"].(string),
LastName: v["last_name"].(string),
Email: v["email"].(string),
Phone: v["phone"].(string),
GravatarURL: v["gravatar_url"].(string),
Administrator: v["administrator"].(bool),
CreatedAt: v["created_at"].(time.Time),
UpdatedAt: v["updated_at"].(time.Time),
Name: v["name"].(string),
AccountOwner: v["account_owner"].(bool),
Organization: v["organization"].(string),
Roles: v["roles"].([]string),
}
}
return &retval, nil
}
func (client *TDClient) ListAPIKeys(email string) (*ListAPIKeysResult, error) {
resp, err := client.get(fmt.Sprintf("/v3/user/apikey/list/%s", url.QueryEscape(email)), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "List apikey failed", nil)
}
js, err := client.checkedJson(resp, listAPIKeysSchema)
if err != nil {
return nil, err
}
return &ListAPIKeysResult{
APIKeys: js["apikeys"].([]string),
}, nil
}
func (client *TDClient) AddUser(name, org, email, password string) error {
params := url.Values{}
params.Set("organization", org)
params.Set("email", email)
params.Set("password", password)
resp, err := client.post(fmt.Sprintf("/v3/user/add/%s", url.QueryEscape(name)), params)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return client.buildError(resp, -1, "add user failed", nil)
}
return nil
}
func (client *TDClient) RemoveUser(email string) error {
resp, err := client.post(fmt.Sprintf("/v3/user/remove/%s", url.QueryEscape(email)), nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return client.buildError(resp, -1, "remove user failed", nil)
}
return nil
}
func (client *TDClient) AddAPIKey(email string) (*AddAPIKeyResult, error) {
resp, err := client.post(fmt.Sprintf("/v3/user/apikey/add/%s", url.QueryEscape(email)), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "add apikey failed", nil)
}
js, err := client.checkedJson(resp, addAPIKeySchema)
if err != nil {
return nil, err
}
return &AddAPIKeyResult{
APIKey: js["apikey"].(string),
}, nil
}
func (client *TDClient) RemoveAPIKey(email, apikey string) error {
params := url.Values{}
params.Set("apikey", apikey)
resp, err := client.post(fmt.Sprintf("/v3/user/apikey/remove/%s", url.QueryEscape(email)), params)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return client.buildError(resp, -1, "remove apikey failed", nil)
}
return nil
}