docs: update README with latest API changes (#347)
Signed-off-by: Lance Ball <lball@redhat.com>
This commit is contained in:
parent
76688c4c01
commit
138de37084
81
README.md
81
README.md
|
@ -29,66 +29,63 @@ npm install cloudevents
|
||||||
|
|
||||||
#### Receiving Events
|
#### Receiving Events
|
||||||
|
|
||||||
You can choose almost any popular web framework for port binding. Use a
|
You can choose any popular web framework for port binding. A `CloudEvent`
|
||||||
`Receiver` to process the incoming HTTP request. The receiver accepts
|
object can be created by simply providing the `HTTP` protocol binding
|
||||||
binary and structured events in either the 1.0 or 0.3 protocol formats.
|
the incoming headers and request body.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const app = require("express")();
|
const app = require("express")();
|
||||||
const {Receiver} = require("cloudevents");
|
const { HTTP } = require("cloudevents");
|
||||||
|
|
||||||
app.post("/", (req, res) => {
|
app.post("/", (req, res) => {
|
||||||
// body and headers come from an incoming HTTP request, e.g. express.js
|
// body and headers come from an incoming HTTP request, e.g. express.js
|
||||||
const receivedEvent = Receiver.accept(req.headers, req.body);
|
const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
|
||||||
console.log(receivedEvent);
|
console.log(receivedEvent);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Emitting Events
|
#### Emitting Events
|
||||||
|
|
||||||
You can send events over HTTP in either binary or structured format.
|
You can send events over HTTP in either binary or structured format
|
||||||
|
using the `HTTP` binding to create a `Message` which has properties
|
||||||
By default, the `Emitter` will emit events over HTTP POST using the
|
for `headers` and `body`.
|
||||||
binary transport protocol. The `Emitter` will examine the `specversion`
|
|
||||||
of the event being sent, and use the appropriate protocol version. To send
|
|
||||||
structured events, add `Protocol.HTTPStructured` as a parameter to
|
|
||||||
`emitter.send()`.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const { CloudEvent, Emitter, Protocol, Version } = require("cloudevents");
|
const axios = require('axios').default;
|
||||||
|
const { HTTP } = require("cloudevents");
|
||||||
|
|
||||||
// With only an endpoint URL, this creates a v1 emitter
|
|
||||||
const emitter = new Emitter({
|
const ce = new CloudEvent({ type, source, data })
|
||||||
url: "https://cloudevents.io/example"
|
const message = HTTP.binary(ce); // Or HTTP.structured(ce)
|
||||||
|
|
||||||
|
axios({
|
||||||
|
method: 'post',
|
||||||
|
url: '...',
|
||||||
|
data: message.body,
|
||||||
|
headers: message.headers,
|
||||||
});
|
});
|
||||||
const event = new CloudEvent({
|
```
|
||||||
type, source, data
|
|
||||||
|
You may also use the `emitterFor()` function as a convenience.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const axios = require('axios').default;
|
||||||
|
const { emitterFor, Mode } = require("cloudevents");
|
||||||
|
|
||||||
|
function sendWithAxios(message) {
|
||||||
|
// Do what you need with the message headers
|
||||||
|
// and body in this function, then send the
|
||||||
|
// event
|
||||||
|
axios({
|
||||||
|
method: 'post',
|
||||||
|
url: '...',
|
||||||
|
data: message.body,
|
||||||
|
headers: message.headers,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// By default, the emitter will send binary events
|
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
|
||||||
emitter.send(event).then((response) => {
|
emit(new CloudEvent({ type, source, data }));
|
||||||
// handle the response
|
|
||||||
}).catch(console.error);
|
|
||||||
|
|
||||||
// To send a structured event, just add that as an option
|
|
||||||
emitter.send(event, { protocol: Protocol.HTTPStructured })
|
|
||||||
.then((response) => {
|
|
||||||
// handle the response
|
|
||||||
}).catch(console.error);
|
|
||||||
|
|
||||||
// To send an event to an alternate URL, add that as an option
|
|
||||||
emitter.send(event, { url: "https://alternate.com/api" })
|
|
||||||
.then((response) => {
|
|
||||||
// handle the response
|
|
||||||
}).catch(console.error);
|
|
||||||
|
|
||||||
// Sending a v0.3 event works the same, If your event has a
|
|
||||||
// specversion property of Version.V03, then it will be sent
|
|
||||||
// using the 0.3 transport protocol
|
|
||||||
emitter.send(new CloudEvent({ specversion: Version.V03, source, type }))
|
|
||||||
.then((response) => {
|
|
||||||
// handle the response
|
|
||||||
}).catch(console.error);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## CloudEvent Objects
|
## CloudEvent Objects
|
||||||
|
|
Loading…
Reference in New Issue