Last active
August 21, 2020 18:49
-
-
Save lance/d71dbb2c26855dcfc746ede10c89f168 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { CloudEvent } = require('cloudevents'); | |
const axios = require('axios'); | |
// this could also be exported at the top level - shown this way to illustrate the namespace | |
const { HTTP } = require('cloudevents/messages'); | |
// user-provided send function conforming to an interface | |
// each protocol may have it's own interface depending on | |
// the message structure determined by the spec. For HTTP | |
// this is, of course, the headers and body | |
function sender(headers, body) { | |
return axios.post('https://receiver.example.com', { | |
headers: headers, | |
data: body | |
}); | |
} | |
// create a CloudEvent | |
const ce = new CloudEvent({ | |
source: '/com.redhat.time.server', | |
type: 'com.redhat.time', | |
data: Date.now() | |
}); | |
// create a timestamp message in binary format | |
const message = HTTP.asBinary(ce); | |
// send the message | |
HTTP.send(sender, message); | |
// This could all be inlined, of course | |
HTTP.send((headers, body) => { | |
return axios.post('https://receiver.example.com', { | |
headers: headers, | |
data: body | |
}); | |
}, HTTP.asBinary(new CloudEvent({ | |
source: '/com.redhat.time.server', | |
type: 'com.redhat.time', | |
data: Date.now() | |
}))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment