-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlexaDeviceAddressClient.js
More file actions
98 lines (85 loc) · 3.24 KB
/
AlexaDeviceAddressClient.js
File metadata and controls
98 lines (85 loc) · 3.24 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
'use strict';
const Https = require('https');
/**
* This is a small wrapper client for the Alexa Address API.
*/
class AlexaDeviceAddressClient {
/**
* Retrieve an instance of the Address API client.
* @param apiEndpoint the endpoint of the Alexa APIs.
* @param deviceId the device ID being targeted.
* @param consentToken valid consent token.
*/
constructor(apiEndpoint, deviceId, consentToken) {
console.log("Creating AlexaAddressClient instance.");
this.deviceId = deviceId;
this.consentToken = consentToken;
this.endpoint = apiEndpoint.replace(/^https?:\/\//i, "");
}
/**
* This will make a request to the Address API using the device ID and
* consent token provided when the Address Client was initialized.
* This will retrieve the full address of a device.
* @return {Promise} promise for the request in flight.
*/
getFullAddress() {
const options = this.__getRequestOptions(`/v1/devices/${this.deviceId}/settings/address`);
return new Promise((fulfill, reject) => {
this.__handleDeviceAddressApiRequest(options, fulfill, reject);
});
}
/**
* This will make a request to the Address API using the device ID and
* consent token provided when the Address Client was initialized.
* This will retrieve the country and postal code of a device.
* @return {Promise} promise for the request in flight.
*/
getCountryAndPostalCode() {
const options = this.__getRequestOptions(
`/v1/devices/${this.deviceId}/settings/address/countryAndPostalCode`);
return new Promise((fulfill, reject) => {
this.__handleDeviceAddressApiRequest(options, fulfill, reject);
});
}
/**
* This is a helper method that makes requests to the Address API and handles the response
* in a generic manner. It will also resolve promise methods.
* @param requestOptions
* @param fulfill
* @param reject
* @private
*/
__handleDeviceAddressApiRequest(requestOptions, fulfill, reject) {
Https.get(requestOptions, (response) => {
console.log(`Device Address API responded with a status code of : ${response.statusCode}`);
response.on('data', (data) => {
let responsePayloadObject = JSON.parse(data);
const deviceAddressResponse = {
statusCode: response.statusCode,
address: responsePayloadObject
};
fulfill(deviceAddressResponse);
});
}).on('error', (e) => {
console.error(e);
reject();
});
}
/**
* Private helper method for retrieving request options.
* @param path the path that you want to hit against the API provided by the skill event.
* @return {{hostname: string, path: *, method: string, headers: {Authorization: string}}}
* @private
*/
__getRequestOptions(path) {
return {
hostname: this.endpoint,
path: path,
method: 'GET',
'headers': {
'Authorization': 'Bearer ' + this.consentToken
}
};
}
}
module.exports = AlexaDeviceAddressClient;