import https = require("https"); import http = require("http"); var config = require("./config"); import jamsglobal = require("./jamsglobal"); import jamslogging = require("./jamslogging"); import jamstoken = require("./jamstoken"); import querystring = require("querystring"); export class JAMSRequest { // #region Properties configuration: any; // #endregion // #region Constructor constructor() { this.configuration = new config.Config(); } // #endregion // #region Methods performRequestUrl(functionality: string, requestType: string, callback: any, dataValues: any = {}, customconfig = {}, debug: boolean = false) { this.configuration = customconfig === {} ? this.configuration : customconfig; var tokenObject = this.getToken(this.configuration); var token = tokenObject !== undefined ? tokenObject.token : ""; var log = new jamslogging.Logging({}, this.configuration); var global = new jamsglobal.Global(this.configuration.locale, this.configuration); var dataString = JSON.stringify(dataValues); var dataLength = dataString.length === undefined ? 0 : dataString.length; var url = require("url").parse(this.getServer(this.configuration, functionality, requestType, dataValues)); var protocol = url.protocol.replace(":", "") === "https" ? https : http; var body = ""; url.method = requestType.toUpperCase(); url.headers = { "Authorization": `Bearer ${token}` }; if (requestType.toUpperCase() === "GET") { url.headers = { "Authorization": `Bearer ${token}` }; } else { url.headers = { "Authorization": `Bearer ${token}`, "Content-Type": "application/json", "Content-Length": dataString.length } } if (debug) { log.debug(`${global.getText("requestdata") }: ${JSON.stringify(dataValues)}`); log.debug(`${global.getText("requestconfig") }: ${JSON.stringify(this.configuration) }`); } var request = protocol.request(url, response => { response.setEncoding("utf-8"); if (response.statusCode == 401) { //throw (response.statusMessage); log.error(response.statusMessage); } else { response.on("data", data => { if (debug) { log.debug(`${global.getText("requestdata") }: ${data}`); } body += data; }); response.on("end",() => { callback(body); if (debug) { log.debug(`${global.getText("resourcebody") }: ${body}`); } }); } }); request.on("error", e => { log.error(e.message, this.configuration.ignoreerror); }); if (dataLength > 0 && dataString !== "{}" && requestType.toUpperCase() !== "GET") { request.write(dataString); } request.end(); } // #endregion // #region Helper Methods getServer(config: any, functionality: string, requestType: string, data: any) { var url = config.server + "/" + functionality; var qs = querystring.stringify(data); if (data !== {} && requestType.toUpperCase() === "GET") { url += `?${qs}`; } return url; } getPath(functionality: string) { var config = this.configuration; return "/" + config.product + "/" + functionality.toUpperCase(); } getOptions(functionality: string, requestType: string, dataLength: any, token: string = ""): any { var config = this.configuration; var headers = this.getHeaders(requestType, dataLength, token); var port: string = config.port !== "" ? config.port : "0"; var options = {}; if (requestType.toUpperCase() === "GET") { if (headers !== {}) { options = { host: config.host, port: port, path: this.getPath(functionality), method: requestType, headers: headers } } else { options = { host: config.host, port: port, path: this.getPath(functionality), method: requestType } } } else { options = { host: config.host, port: port, path: this.getPath(functionality), method: requestType, headers: headers } } return options; } getHeaders(requestType: any, dataLength: any, accessToken: string = "") { var config = this.configuration; var headers: any = {}; var token = `Bearer ${accessToken}`; if (accessToken.length > 0) { headers = requestType.toUpperCase() !== "GET" ? { 'Content-Type': config.contenttype, 'Content-Length': dataLength, 'Authorization': token } : { 'Authorization': token }; } else { headers = requestType.toUpperCase() !== "GET" ? { 'Content-Type': config.contenttype, 'Content-Length': dataLength } : {}; } return headers; } getToken(config): any { var token = new jamstoken.Token(); var conf = config === {} ? new config.Config() : config; return token.getToken(conf); } getAddress(functionality: string): any { var config = this.configuration; var port: string = config.port !== "" ? ":" + config.port : ""; var address = config.host + port + this.getPath(functionality); return address; } populateQueryString(obj: any) { } // #endregion }