UNPKG

homebridge-hatch-baby-rest-volume

Version:
99 lines (98 loc) 4.13 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RestClient = exports.requestWithRetry = exports.apiPath = void 0; const got_1 = __importDefault(require("got")); const util_1 = require("./util"); const apiBaseUrl = 'https://data.hatchbaby.com/', defaultRequestOptions = { http2: true, responseType: 'json', method: 'GET', }; function apiPath(path) { return apiBaseUrl + path; } exports.apiPath = apiPath; function requestWithRetry(options) { return __awaiter(this, void 0, void 0, function* () { try { const response = (yield got_1.default(Object.assign(Object.assign({}, defaultRequestOptions), options))); return response.body; } catch (e) { if (!e.response) { util_1.logError(`Failed to reach Hatch Baby server at ${options.url}. ${e.message}. Trying again in 5 seconds...`); yield util_1.delay(5000); return requestWithRetry(options); } throw e; } }); } exports.requestWithRetry = requestWithRetry; class RestClient { constructor(authOptions) { this.authOptions = authOptions; this.loginPromise = this.logIn(); } logIn() { return __awaiter(this, void 0, void 0, function* () { try { const resp = yield requestWithRetry({ url: apiPath('public/v1/login'), json: { email: this.authOptions.email, password: this.authOptions.password, }, method: 'POST', }); return resp; } catch (requestError) { const errorMessage = 'Failed to fetch oauth token from Hatch Baby. Verify that your email and password are correct.'; util_1.logError(requestError.response || requestError); util_1.logError(errorMessage); throw new Error(errorMessage); } }); } refreshAuth() { this.loginPromise = this.logIn(); } request(options) { return __awaiter(this, void 0, void 0, function* () { try { const loginResponse = yield this.loginPromise, headers = Object.assign(Object.assign({}, options.headers), { 'X-HatchBaby-Auth': loginResponse.token }), response = yield requestWithRetry(Object.assign(Object.assign({}, options), { headers })); return response.payload; } catch (e) { const response = e.response || {}, { url } = options; if (response.status === 401) { this.refreshAuth(); return this.request(options); } if (response.status === 404 && url.startsWith(apiBaseUrl)) { util_1.logError('404 from endpoint ' + url); throw new Error('Not found with response: ' + JSON.stringify(response.data)); } util_1.logError(`Request to ${url} failed`); throw e; } }); } getAccount() { return this.loginPromise.then((l) => l.payload); } } exports.RestClient = RestClient;