UNPKG

domotz-remote-pawn

Version:

Domotz Agent

90 lines (82 loc) 3.16 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ var httpRequest = function (message, resourceLocator) { var cid = message.correlationId; var options = message.payload; var myConsole = resourceLocator.log.decorateLogs(); /** * basic_auth is the historical field delegated to authentication: kept this * name for retro-compatibility. */ if (options.basic_auth) { // auth = payload.basic_auth.user + ':' + payload.basic_auth.password + '@'; options.auth = options.basic_auth; delete options.basic_auth; } var url = (options.protocol || 'http') + '://' + options.host + ':' + options.port + options.path; delete options.host; delete options.port; delete options.path; delete options.protocol; options.url = url; if (options.timeout === undefined) { options.timeout = 2000; // 2s of default timeout } return { execute: function (callback) { var responseCallback = function (error, response) { if (response === undefined) { myConsole.info('MESSAGE - (%s): Empty response, probable connection error - %s', cid, JSON.stringify(error)); response = {}; } else { myConsole.info('MESSAGE - (%s): Got response - Code: %s', cid, response.statusCode); } callback({ error: error, statusCode: response.statusCode, headers: response.headers, body: response.body, }); }; myConsole.info('MESSAGE - (%s): Sending request: %s', cid, url); resourceLocator.request(options, responseCallback); }, describe: function () { var match = resourceLocator.url.parse(url); return ( 'httpRequest - ' + 'Method: ' + options.method + '. ' + 'Protocol: ' + match.protocol + '. ' + 'Host: ' + match.hostname + '. ' + 'Port: ' + (match.port || 80) + '. ' + 'Path: ' + match.pathname + '.' ); // TODO: Print also query params (but excluding password) }, }; }; module.exports.command = httpRequest;