LocalInsecureBootstrap.js
1.34 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
'use strict';
const url = require('url')
, httpClient = require('./HttpClientFetch')
, Transport = require('./Transport')
, Api = require('../Api')
;
const SUPPRESS_WARNING = process.env.NODE_HUE_API_USE_INSECURE_CONNECTION != null;
module.exports = class LocalInsecureBootstrap {
constructor(hostname, port) {
this._baseUrl = url.format({protocol: 'http', hostname: hostname, port: port || 80});
this._hostname = hostname;
}
get baseUrl() {
return this._baseUrl;
}
get hostname() {
return this._hostname;
}
connect(username, clientkey) {
const baseUrl = this.baseUrl;
if (!SUPPRESS_WARNING) {
console.log('WARNING: You are using this library in an insecure way!\n'
+ 'The Hue bridge supports HTTPS connections locally and it is highly recommended that you use an HTTPS\n'
+ 'method to communicate with the bridge.'
);
}
return httpClient.request({method: 'GET', url: `${baseUrl}/api/config`})
.then(() => {
const apiBaseUrl = `${baseUrl}/api`
, transport = new Transport(username, httpClient.create({baseURL: apiBaseUrl}))
, config = {
remote: false,
baseUrl: apiBaseUrl,
clientkey: clientkey,
username: username,
}
;
return new Api(config, transport);
});
}
};