UNPKG

homey-api

Version:
75 lines (60 loc) 1.62 kB
'use strict'; const API = require('./API'); const APIError = require('./APIError'); const Util = require('./Util'); class HomeyCloudAPI extends API { static SPECIFICATION = { host: 'homeycloud.net', basePath: '/', operations: {}, }; static JSDOC_DESCRIPTION = 'Use this API to find the closest Homey Cloud region.'; static JSDOC_PRIVATE = true; static JSDOC_PARAMS = ` @param {string} opts.region - e.g. \`eu-central-1\` `; /** * Get the closest Homey Cloud URL * * @returns {object} result * @returns {string} result.region - e.g. `eu-central-1` */ static async getClosestRegion() { const res = await Util.fetch(`https://${HomeyCloudAPI.SPECIFICATION.host}`); if (!res.ok) { throw new Error(res.statusText || 'Unknown Error'); } const { region } = await res.json(); return { region }; } constructor({ region, ...props }) { if (!region) { throw new Error('Missing Region'); } super({ baseUrl: `https://${region}.${HomeyCloudAPI.SPECIFICATION.host}`, ...props, }); this.region = region; } /** * Get the system's status * * @param {object} opts * @param {string} opts.secret * @returns {Promise<object>} result */ async getSystemStatus({ secret }) { const res = await Util.fetch(`${this.baseUrl}/api/system/status`, { headers: { 'X-Homey-Secret': secret, }, }); const json = await res.json(); if (!res.ok) { throw new APIError(json.error || res.statusText, res.status); } return json; } } module.exports = HomeyCloudAPI;