Skip to content

Commit ac6cf27

Browse files
authored
Merge pull request #15 from playground-julia/julia/adhereToSpec
Change binary http bindings to adhere to spec
2 parents a770b19 + b604542 commit ac6cf27

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

lib/bindings/http/binary_0_1.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var axios = require("axios");
22

33
function HTTPBinary(configuration){
4-
this.config = configuration;
4+
this.config = JSON.parse(JSON.stringify(configuration));
55

66
this.config["headers"] = {
7-
"Content-Type":"application/cloudevents+json; charset=utf-8"
7+
"Content-Type":"application/json; charset=utf-8"
88
};
99
}
1010

@@ -16,25 +16,40 @@ HTTPBinary.prototype.emit = function(cloudevent){
1616
// Always set stuff in _config
1717
var _headers = _config["headers"];
1818

19+
// OPTIONAL CONTENT TYPE ATTRIBUTE
1920
if(cloudevent.getContenttype()) {
2021
_headers["Content-Type"] = cloudevent.getContenttype();
2122
}
2223

24+
// REQUIRED ATTRIBUTES
2325
_headers["CE-EventType"] = cloudevent.getType();
24-
if(cloudevent.getEventTypeVersion()) {
25-
_headers["CE-EventTypeVersion"] = cloudevent.getEventTypeVersion();
26-
}
2726
_headers["CE-CloudEventsVersion"] = cloudevent.getSpecversion();
2827
_headers["CE-Source"] = cloudevent.getSource();
2928
_headers["CE-EventID"] = cloudevent.getId();
29+
30+
// OPTIONAL ATTRIBUTES
31+
if(cloudevent.getEventTypeVersion()) {
32+
_headers["CE-EventTypeVersion"] = cloudevent.getEventTypeVersion();
33+
}
3034
if(cloudevent.getTime()) {
3135
_headers["CE-EventTime"] = cloudevent.getTime();
3236
}
33-
_headers["CE-SchemaURL"] = cloudevent.getSchemaurl();
37+
if(cloudevent.getSchemaurl()) {
38+
_headers["CE-SchemaURL"] = cloudevent.getSchemaurl();
39+
}
3440

3541
// Set the cloudevent payload
3642
_config["data"] = cloudevent.format().data;
3743

44+
// EXTENSION CONTEXT ATTRIBUTES
45+
var exts = cloudevent.getExtensions();
46+
for(var ext in exts){
47+
if({}.hasOwnProperty.call(exts, ext)){
48+
let capsExt = ext.charAt(0).toUpperCase() + ext.slice(1)
49+
_headers["CE-X-" + capsExt] = exts[ext];
50+
}
51+
}
52+
3853
// Return the Promise
3954
return axios.request(_config);
4055
};

lib/bindings/http/binary_0_2.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ var axios = require("axios");
22
var empty = require("is-empty");
33

44
function HTTPBinary(configuration){
5-
this.config = configuration;
5+
this.config = JSON.parse(JSON.stringify(configuration));
66

77
this.config["headers"] = {
8-
"Content-Type":"application/cloudevents+json; charset=utf-8"
8+
"Content-Type":"application/json; charset=utf-8"
99
};
1010
}
1111

@@ -17,21 +17,29 @@ HTTPBinary.prototype.emit = function(cloudevent){
1717
// Always set stuff in _config
1818
var _headers = _config["headers"];
1919

20-
_headers["Content-Type"] = cloudevent.getContenttype();
20+
// OPTIONAL CONTENT TYPE ATTRIBUTE
21+
if (cloudevent.getContenttype()) {
22+
_headers["Content-Type"] = cloudevent.getContenttype();
23+
}
2124

25+
// REQUIRED ATTRIBUTES
2226
_headers["ce-type"] = cloudevent.getType();
2327
_headers["ce-specversion"] = cloudevent.getSpecversion();
2428
_headers["ce-source"] = cloudevent.getSource();
2529
_headers["ce-id"] = cloudevent.getId();
26-
if(cloudevent.getTime()) {
30+
31+
// OPTIONAL ATTRIBUTES
32+
if (cloudevent.getTime()) {
2733
_headers["ce-time"] = cloudevent.getTime();
2834
}
29-
_headers["ce-schemaurl"] = cloudevent.getSchemaurl();
35+
if (cloudevent.getSchemaurl()) {
36+
_headers["ce-schemaurl"] = cloudevent.getSchemaurl();
37+
}
3038

3139
// Set the cloudevent payload
3240
_config["data"] = cloudevent.format().data;
3341

34-
// Have extensions?
42+
// EXTENSION CONTEXT ATTRIBUTES
3543
var exts = cloudevent.getExtensions();
3644
for(var ext in exts){
3745
if({}.hasOwnProperty.call(exts, ext)){

lib/bindings/http/structured_0_1.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var axios = require("axios");
22

33
function HTTPStructured(configuration){
4-
this.config = configuration;
4+
this.config = JSON.parse(JSON.stringify(configuration));
55

66
this.config["headers"] = {
77
"Content-Type":"application/cloudevents+json; charset=utf-8"
@@ -13,6 +13,7 @@ HTTPStructured.prototype.emit = function(cloudevent){
1313
// Create new request object
1414
var _config = JSON.parse(JSON.stringify(this.config));
1515

16+
1617
// Set the cloudevent payload
1718
_config["data"] = cloudevent.format();
1819

lib/bindings/http/structured_0_2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var axios = require("axios");
22

33
function HTTPStructured(configuration){
4-
this.config = configuration;
4+
this.config = JSON.parse(JSON.stringify(configuration));
55

66
this.config["headers"] = {
77
"Content-Type":"application/cloudevents+json; charset=utf-8"

0 commit comments

Comments
 (0)