HueApiConfig.js
3.35 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
136
137
138
139
140
141
142
143
'use strict';
const ApiError = require('../ApiError');
/**
* @typedef {import('./http/RemoteApi')} RemoteApi
* @type {HueApiConfig}
*/
module.exports = class HueApiConfig {
constructor(config, transport, remoteApi) {
this._config = config;
this._remoteApi = remoteApi;
this._transport = transport;
this._isRemote = !!config.remote && !!remoteApi;
}
/**
* Is the connection to the hue bridge remote.
* @returns {boolean}
*/
get isRemote() {
return this._isRemote;
}
/**
* Gets the transport implementation that is used to conenct with the Hue Bridge
* @returns {Object}
*/
get transport() {
return this._transport;
}
/**
* Gets the remote API in use that was used to bootstrap the remote connection.
* @returns {RemoteApi}*
* @throws ApiError if the connection is local network.
*/
get remote() {
if (this.isRemote) {
return this._remoteApi;
} else {
throw new ApiError('This API has not been set up as a remote API');
}
}
/**
* Gets the current username used to connect/interact with the Hue Bridge.
* @returns {String} The bridge username.
*/
get username() {
return this._config.username;
}
/**
* Gets the client id for the remote OAuth connection.
* @returns {String} The clientId for the remote connection
* @throws ApiError if the connection is not remote.
*/
get clientId() {
this._requireRemote();
return this._config.clientId;
}
/**
* Gets the client secret for the remote OAuth connection.
* @returns {String} The client secret for the remote connection.
* @throws ApiError if the connection is not remote.
*/
get clientSecret() {
this._requireRemote();
return this._config.clientSecret;
}
/**
* The Base URL for communication with the bridge.
* @returns {String} The base URL for the hue bridge.
*/
get baseUrl() {
return this._config.baseUrl;
}
/**
* Gets the client key for the entertainment API/streaming endpoints
* @returns {String}
* @throws ApiError if the connection is not local network.
*/
get clientKey() {
this._requireLocal();
return this._config.clientkey;
}
/**
* Gets the current access token.
* @returns {String}
* @throws ApiError if the connection is not remote.
*/
get accessToken() {
this._requireRemote();
return this._remoteApi.accessToken;
}
/**
* Gets the expiry timestamp of the access token.
* @returns {number} The timestamp for the expiry or -1 if not known
*/
get accessTokenExpiry() {
this._requireRemote();
return this._remoteApi.accessTokenExpiry;
}
/**
* Gets the current refresh token.
* @returns {String}
* @throws ApiError if the connection is not remote.
*/
get refreshToken() {
this._requireRemote();
return this._remoteApi.refreshToken;
}
/**
* Gets the expiry timestamp of the refresh token.
* @returns {number} The timestamp for the expiry or -1 if not known
*/
get refreshTokenExpiry() {
this._requireRemote();
return this._remoteApi.refreshTokenExpiry;
}
_requireRemote() {
if (!this.isRemote) {
throw new ApiError('The function is only valid on a remote Hue API instance');
}
}
_requireLocal() {
if (this.isRemote) {
throw new ApiError('The function is only valid on a local Hue API instance');
}
}
};