configuration.js
2.39 KB
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
'use strict';
const ApiEndpoint = require('./endpoint')
, UsernamePlaceholder = require('../../../placeholders/UsernamePlaceholder')
, util = require('../../../util')
, ApiError = require('../../../ApiError')
, model = require('@peter-murray/hue-bridge-model').model
, instanceChecks = model.instanceChecks
;
module.exports = {
createUser: new ApiEndpoint()
.post()
.acceptJson()
.uri('/')
.payload(buildUserPayload)
.pureJson()
.postProcess(processCreateUser),
getConfiguration: new ApiEndpoint()
.get()
.acceptJson()
.uri('/<username>/config')
.pureJson()
.postProcess(createBridgeConfiguration),
updateConfiguration: new ApiEndpoint()
.put()
.acceptJson()
.uri('/<username>/config')
.payload(buildConfigurationPayload)
.pureJson()
.postProcess(util.wasSuccessful),
deleteUser: new ApiEndpoint()
.delete()
.acceptJson()
.uri('/<username>/config/whitelist/<userid>')
.placeholder(new UsernamePlaceholder('userid'))
.pureJson()
.postProcess(util.wasSuccessful),
getFullState: new ApiEndpoint()
.get()
.acceptJson()
.uri('/<username>')
.pureJson(),
getUnauthenticatedConfig: new ApiEndpoint()
.get()
.acceptJson()
.uri('/config')
.pureJson()
.postProcess(processUnauthenticatedData),
};
function processCreateUser(data) {
if (util.wasSuccessful(data)) {
return data[0].success;
} else {
throw new ApiError(`Failed to create new user: ${JSON.stringify(data)}`);
}
}
function createBridgeConfiguration(data) {
return model.createFromBridge('configuration', null, data);
}
function processUnauthenticatedData(data) {
return {
config: data
};
}
function buildUserPayload(data) {
//TODO utilize the type system
//TODO perform validation on the strings here
// applicationName 0..20
// deviceName 0...19
const body = {
devicetype: `${data.appName}#${data.deviceName}`
};
if (data.generateKey) {
body.generateclientkey = true;
}
return {
type: 'application/json',
body: body
};
}
function buildConfigurationPayload(parameters) {
const config = parameters.config;
let bridgeConfig;
if (instanceChecks.isBridgeConfigurationInstance(config)) {
bridgeConfig = config;
} else {
bridgeConfig = createBridgeConfiguration(config);
}
return {
type: 'application/json',
body: bridgeConfig.getHuePayload()
};
}