A generic structured http emitter for reusable purposes

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-07-29 14:33:59 -03:00
parent d42efce9fc
commit 145bd2973f
3 changed files with 34 additions and 19 deletions

View File

@ -13,6 +13,7 @@ module.exports = {
HEADER_CONTENT_TYPE : "content-type",
DEFAULT_CONTENT_TYPE : "application/json; charset=utf-8",
DEFAULT_CE_CONTENT_TYPE : "application/cloudevents+json; charset=utf-8",
BINARY_HEADERS_02 : {
TYPE : "ce-type",

View File

@ -0,0 +1,30 @@
var axios = require("axios");
const Constants = require("./constants.js");
function StructuredHTTPEmitter(configuration){
this.config = JSON.parse(JSON.stringify(configuration));
this.config[Constants.HEADERS] =
(!this.config[Constants.HEADERS]
? {}
: this.config[Constants.HEADERS]);
if(!this.config[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE]){
this.config[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] =
Constants.DEFAULT_CE_CONTENT_TYPE;
}
}
StructuredHTTPEmitter.prototype.emit = function(cloudevent) {
// Create new request object
var _config = JSON.parse(JSON.stringify(this.config));
// Set the cloudevent payload
_config[Constants.DATA_ATTRIBUTE] = cloudevent.format();
// Return the Promise
return axios.request(_config);
};
module.exports = StructuredHTTPEmitter;

View File

@ -1,29 +1,13 @@
var axios = require("axios");
const StructuredHTTPEmitter = require("./emitter_structured.js");
const Constants = require("./constants.js");
function HTTPStructured(configuration){
this.config = JSON.parse(JSON.stringify(configuration));
if(!this.config["headers"]){
this.config["headers"] = {};
}
this.config["headers"]
[Constants.HEADER_CONTENT_TYPE] =
Constants.MIME_CE_JSON + "; charset=" + Constants.CHARSET_DEFAULT;
this.emitter = new StructuredHTTPEmitter(configuration);
}
HTTPStructured.prototype.emit = function(cloudevent){
// Create new request object
var _config = JSON.parse(JSON.stringify(this.config));
// Set the cloudevent payload
_config["data"] = cloudevent.format();
// Return the Promise
return axios.request(_config);
return this.emitter.emit(cloudevent);
};
module.exports = HTTPStructured;