UNPKG

dl

Version:

DreamLab Libs

145 lines (118 loc) 4.1 kB
var Class = require("core").Class; var Request = require("core").http.Request; var JsonRpcRequest = require("core").jsonrpc.JsonRpcRequest; var BinaryData = require("core").data.BinaryData; /** * @class OpalRequest * @extends UrlRequest * @requires Class * @requires UrlRequest * */ var OpalRequest = function () { /** @ignore */ this.Extends = Request; /** * @private * @property {String} _remoteMethod */ //this._remoteMethod = null; /** @ignore */ this.initialize = function (data) { this.parent(); this._remoteMethod = null; if (data) { if (data.hasOwnProperty('url') && data.hasOwnProperty('params') && data.hasOwnProperty('method')) { this._singleRequest(data); } else if(data.hasOwnProperty('url')) { this._batchRequest(data); } else { throw OpalRequest.Exception.INCOMPLETE_PARAMS; } } }; this._setConnection = function(data){ if (data.url.substring(0, 8) == 'https://') { this.setPort(443); this.setHeader('host', data.url.replace('https://', '').replace('/', '')); } else { this.setPort(80); this.setHeader('host', data.url.replace('http://', '').replace('/', '')); } if (process.env['OPAL_LOCATION']) { this.setConnectionHost(process.env['OPAL_LOCATION']); } else { this.setConnectionHost(data.url); } this.setRemoteMethod(data.method); this.setMethod(Request.POST); if (data.hasOwnProperty('application')) { this.setApplication(data.application); } else if (process.env['OPAL_IDENTITY']) { this.setApplication(process.env['OPAL_IDENTITY']); } else { this.setApplication('localhost.front.onetapi.pl'); } }; this._batchRequest = function (data) { this._setConnection(data); this.setMethod(Request.POST); }; this.setBatchBody = function (data) { var bdata = new BinaryData(data.toString(), BinaryData.Encoding.TEXT, BinaryData.CharacterEncoding.UTF8); this.setBody(bdata); }; this._singleRequest = function (data) { this._setConnection(data); this.setRemoteMethod(data.method); this.setMethod(Request.POST); var odata = new JsonRpcRequest({ 'id': this.getRemoteMethod() + '-' + +new Date(), 'method': this.getRemoteMethod(), 'params': data.params }).toString(); var bData = new BinaryData(odata, BinaryData.Encoding.TEXT, BinaryData.CharacterEncoding.UTF8); this.setBody(bData); this.setHeader('content-type', 'application/json-rpc'); this.setHeader('content-length', bData.length()); }; /** * sets the application ident string * @param {String} app * @returns {OpalRequest} */ this.setApplication = function (app) { this.setHeader('x-onet-app', app, true); return this; }; /** * returns application ident string * @returns {OpalRequest} */ this.getApplication = function () { return this.getHeader('x-onet-app'); }; /** * sets method of communicating with a service * @param {String} method * @returns {OpalRequest} */ this.setRemoteMethod = function (method) { this._remoteMethod = method; return this; }; /** * returns method of communicating with a service * * @returns {OpalRequest} */ this.getRemoteMethod = function () { return this._remoteMethod; }; }; OpalRequest = new Class(new OpalRequest()); OpalRequest.Exception = {}; OpalRequest.Exception.INCOMPLETE_PARAMS = 'Incomplete params given'; exports.OpalRequest = OpalRequest;