resourcelinks.js
3.49 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const ApiEndpoint = require('./endpoint')
, ResourceLinkPlaceholder = require('../../../placeholders/ResourceLinkPlaceholder')
, model = require('@peter-murray/hue-bridge-model').model
, instanceChecks = model.instanceChecks
, ApiError = require('../../../ApiError')
, util = require('../../../util')
;
const RESOURCELINK_PLACEHOLDER = new ResourceLinkPlaceholder();
module.exports = {
getAll: new ApiEndpoint()
.get()
.uri('/<username>/resourcelinks')
.acceptJson()
.pureJson()
.postProcess(buildResourceLinkResults),
getResourceLink: new ApiEndpoint()
.get()
.uri('/<username>/resourcelinks/<id>')
.placeholder(RESOURCELINK_PLACEHOLDER)
.acceptJson()
.pureJson()
.postProcess(buildResourceLink),
createResourceLink: new ApiEndpoint()
.post()
.uri('/<username>/resourcelinks')
.payload(createResourceLinkPayload)
.acceptJson()
.pureJson()
.postProcess(buildCreateResourceLinkResult),
updateResourceLink: new ApiEndpoint()
.put()
.uri('/<username>/resourcelinks/<id>')
.placeholder(RESOURCELINK_PLACEHOLDER)
.payload(buildResourceLinkUpdatePayload)
.acceptJson()
.pureJson()
.postProcess(util.extractUpdatedAttributes),
deleteResourceLink: new ApiEndpoint()
.delete()
.uri('/<username>/resourcelinks/<id>')
.placeholder(RESOURCELINK_PLACEHOLDER)
.acceptJson()
.pureJson()
.postProcess(util.wasSuccessful)
};
function buildResourceLinkResults(data) {
const resourceLinks = [];
if (data) {
Object.keys(data).forEach(id => {
resourceLinks.push(model.createFromBridge('resourcelink', id, data[id]));
});
}
return resourceLinks;
}
function buildResourceLink(data, requestParameters) {
if (data) {
const id = RESOURCELINK_PLACEHOLDER.getValue(requestParameters);
return model.createFromBridge('resourcelink', id, data);
}
return null;
}
function buildCreateResourceLinkResult(result) {
const hueErrors = util.parseErrors(result); //TODO not sure if this still gets called as the request handles some of this
if (hueErrors) {
throw new ApiError(`Error creating resourcelink: ${hueErrors[0].description}`, hueErrors[0]);
}
return {id: Number(result[0].success.id)};
}
function buildResourceLinkUpdatePayload(parameters) {
const resourceLink = parameters.resourceLink;
if (!resourceLink) {
throw new ApiError('No ResourceLink provided');
} else if (!instanceChecks.isResourceLinkInstance(resourceLink)) {
throw new ApiError('Must provide a valid ResourceLink object');
}
const body = buildResourceLinkBody(resourceLink);
// Cannot change the owner, type or recycle values
delete body.type;
delete body.recycle;
delete body.owner;
return {
type: 'application/json',
body: body
};
}
function createResourceLinkPayload(parameters) {
const resourceLink = parameters.resourceLink;
if (!resourceLink) {
throw new ApiError('No ResourceLink provided');
} else if (!instanceChecks.isResourceLinkInstance(resourceLink)) {
throw new ApiError('Must provide a valid ResourceLink object');
}
const body = buildResourceLinkBody(resourceLink);
if (!body.links || body.links.length === 0) {
throw new ApiError('You must provide a ResourceLink with some links defined');
}
return {
type: 'application/json',
body: body
};
}
function buildResourceLinkBody(resourceLink) {
const data = resourceLink.getHuePayload();
// Cannot have an ID in the create payload
delete data.id;
return data;
}