UNPKG

node-hue-api

Version:
38 lines (37 loc) 1.6 kB
import { Transport } from './Transport'; import { Api } from '../Api'; import { request, create } from './HttpClientFetch'; import { getHttpUrl } from './urlUtil'; const SUPPRESS_WARNING = process.env.NODE_HUE_API_USE_INSECURE_CONNECTION != null; export class LocalInsecureBootstrap { constructor(hostname, rateLimits, port) { this.baseUrl = getHttpUrl(hostname, port || 80); this.hostname = hostname; this.rateLimits = rateLimits; } connect(username, clientkey, timeout) { const baseUrl = this.baseUrl, rateLimits = this.rateLimits; 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 request({ method: 'GET', url: `${baseUrl.href}api/config` }) .then(() => { const apiBaseUrl = `${baseUrl.href}api`, fetchConfig = { baseURL: apiBaseUrl, timeout: getTimeout(timeout) }, transport = new Transport(create(fetchConfig), rateLimits.transportRateLimit, username), config = { remote: false, baseUrl: apiBaseUrl, bridgeName: this.hostname, clientKey: clientkey, username: username, }; return new Api(config, transport, rateLimits); }); } } function getTimeout(timeout) { return timeout || 20000; }