forked from electricimp/imp-central-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Accounts.js
273 lines (252 loc) · 10.1 KB
/
Accounts.js
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// MIT License
//
// Copyright 2017 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const Entities = require('./util/Entities');
const ParamsChecker = require('./util/ParamsChecker');
const HttpHelper = require('./util/HttpHelper');
// This class provides access to Accounts impCentral API methods.
class Accounts extends Entities {
constructor() {
super();
this._loginKeys = new LoginKeys();
}
// Retrieves account information.
//
// Parameters:
// accountId : String ID of the Account to be retrieved, can be literal string 'me'
// in which case the users' own information will be returned.
//
// Returns: Promise that resolves when the Account information is successfully
// obtained, or rejects with an error
get(accountId) {
return super.get(accountId);
}
// Retrieves a list of Accounts associated with the logged-in account.
//
// Parameters:
// pageNumber : Number Optional pagination page number (starts at 1).
// If not specified, the default value is 1.
// pageSize : Number Optional pagination size - maximum number of items to return.
// If not specified, the default value is 20.
//
// Returns: Promise that resolves when the Products list is successfully
// obtained, or rejects with an error
list(pageNumber = null, pageSize = null) {
return super.list(null, pageNumber, pageSize);
}
// Retrieves a list of the Login Keys associated with the logged-in account.
//
// Parameters:
// pageNumber : Number Optional pagination page number (starts at 1).
// If not specified, the default value is 1.
// pageSize : Number Optional pagination size - maximum number of items to return.
// If not specified, the default value is 20.
//
// Returns: Promise that resolves when the Login Keys list is successfully
// obtained, or rejects with an error
listLoginKeys(pageNumber = null, pageSize = null) {
return this._loginKeys.list(null, pageNumber, pageSize);
}
// Creates a Login Key.
//
// Parameters:
// password : String the account password
// attributes : Object Key/Value attributes of the Login Key to be created.
// The valid keys are:
// 'description' (optional) - an optional free-form description
// of the Login Key
//
// Returns: Promise that resolves when the Login Key is successfully created,
// or rejects with an error
createLoginKey(password, attributes = null) {
const attrs = {
password : password
};
Object.assign(attrs, attributes);
return this._loginKeys.create(attrs);
}
// Retrieves a specific Login Key.
//
// Parameters:
// loginKeyId : String ID of the Login Key to be retrieved
//
// Returns: Promise that resolves when the Login Key is successfully obtained,
// or rejects with an error
getLoginKey(loginKeyId) {
return this._loginKeys.get(loginKeyId);
}
// Updates a specific Login Key.
//
// Parameters:
// loginKeyId : String ID of the Login Key to be updated
// password : String the account password
// attributes : Object Key/Value attributes of the Login Key that will be updated.
// The valid keys are:
// 'description' - an optional free-form description
// of the Login Key
//
// Returns: Promise that resolves when the Login Key is successfully updated,
// or rejects with an error
updateLoginKey(loginKeyId, password, attributes) {
const attrs = {
password : password
};
Object.assign(attrs, attributes);
return this._loginKeys.update(loginKeyId, attrs);
}
// Deletes a specific Login Key.
//
// Parameters:
// loginKeyId : String ID of the Login Key to be updated
// password : String the account password
//
// Returns: Promise that resolves when the Login Key is successfully deleted,
// or rejects with an error
deleteLoginKey(loginKeyId, password) {
const attrs = {
password : password
};
return this._loginKeys.delete(loginKeyId, attrs);
}
_getPath(accountId = null) {
if (accountId) {
return '/accounts/' + accountId;
}
return '/accounts';
}
// Returns Account resource type
static get _TYPE() {
return 'account';
}
}
class LoginKeys extends Entities {
constructor() {
super();
this._validCreateAttributes = {
description : false
};
this._validUpdateAttributes = {
description : false
};
}
// Creates a Login Key.
//
// Parameters:
// attributes : Object Key/Value attributes of the Login Key to be created.
// The valid keys are:
// 'description' (optional) - an optional free-form description
// of the Login Key
// 'password' - the account password
//
// Returns: Promise that resolves when the Login Key is successfully created,
// or rejects with an error
create(attributes) {
const password = attributes.password;
delete attributes.password;
const error = ParamsChecker.validateNonEmpty(password, 'password');
if (error) {
return Promise.reject(error);
}
const body = {
data : {
type : LoginKeys._TYPE,
attributes : attributes
}
};
return super.create(attributes, body, this._getAdditionalHeaders(password));
}
// Retrieves a specific Login Key.
//
// Parameters:
// loginKeyId : String ID of the Login Key to be retrieved
//
// Returns: Promise that resolves when the Login Key is successfully obtained,
// or rejects with an error
get(loginKeyId) {
return super.get(loginKeyId);
}
// Updates a specific Login Key.
//
// Parameters:
// loginKeyId : String ID of the Login Key to be updated
// attributes : Object Key/Value attributes of the Login Key that will be updated.
// The valid keys are:
// 'description' - an optional free-form description
// of the Login Key
// 'password' - the account password
//
// Returns: Promise that resolves when the Login Key is successfully updated,
// or rejects with an error
update(loginKeyId, attributes) {
const password = attributes.password;
delete attributes.password;
const error = ParamsChecker.validateNonEmpty(password, 'password');
if (error) {
return Promise.reject(error);
}
const body = {
data : {
type : LoginKeys._TYPE,
id : loginKeyId,
attributes : attributes
}
};
return super.update(loginKeyId, attributes, body, this._getAdditionalHeaders(password));
}
// Deletes a specific Login Key.
//
// Parameters:
// loginKeyId : String ID of the Login Key to be updated
// attributes : Object Key/Value attributes for the Login Key deleting.
// The valid keys are:
// 'password' - the account password
//
// Returns: Promise that resolves when the Login Key is successfully deleted,
// or rejects with an error
delete(loginKeyId, attributes) {
const password = attributes.password;
const error = ParamsChecker.validateNonEmpty(password, 'password');
if (error) {
return Promise.reject(error);
}
return super.delete(loginKeyId, this._getAdditionalHeaders(password));
}
_getAdditionalHeaders(password) {
return {
'X-Electricimp-Password' : password
};
}
_getPath(loginKeyId = null) {
let path = '/accounts/me/login_keys';
if (loginKeyId) {
return path + '/' + loginKeyId;
}
return path;
}
// Returns Login Key resource type
static get _TYPE() {
return 'login_key';
}
}
module.exports = Accounts;