Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update examples to use latest sdk changes #282

Merged
merged 3 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions examples/express-ex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ curl -X POST \
-H'ce-source:https://github.com/cloudevents/spec/pull/123' \
-H'ce-id:45c83279-c8a1-4db6-a703-b3768db93887' \
-H'ce-time:2019-11-06T11:17:00Z' \
-H'ce-my-extension:extension value' \
-H'ce-myextension:extension value' \
http://localhost:3000/
```

Expand Down Expand Up @@ -110,6 +110,17 @@ curl -X POST \
http://localhost:3000/
```

__A Structured One with Base64 Event Data__

> Payload [example](../payload/v03/structured-event-2.json)

```bash
curl -X POST \
-d'@../payload/v03/structured-event-2.json' \
-H'Content-Type:application/cloudevents+json' \
http://localhost:3000/
```

__A Binary One__

```bash
Expand All @@ -135,7 +146,7 @@ curl -X POST \
-H'ce-source:https://github.com/cloudevents/spec/pull/123' \
-H'ce-id:45c83279-c8a1-4db6-a703-b3768db93887' \
-H'ce-time:2019-06-21T17:31:00Z' \
-H'ce-my-extension:extension value' \
-H'ce-myextension:extension value' \
http://localhost:3000/
```

Expand Down
4 changes: 2 additions & 2 deletions examples/express-ex/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */

const express = require("express");
const { Receiver } = require("cloudevents-sdk");
const { Receiver } = require("cloudevents");

const app = express();
const receiver = new Receiver();
Expand All @@ -25,7 +25,7 @@ app.post("/", function (req, res) {
console.log("BODY", req.body);

try {
const event = receiver.accept(req.headers, req.body);
const event = Receiver.accept(req.headers, req.body);
console.log(`Accepted event: ${event}`);
res.status(201).json(event);
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion examples/express-ex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "fabiojose@gmail.com",
"license": "Apache-2.0",
"dependencies": {
"cloudevents-sdk": "~2.0.2",
"cloudevents": "~3.0.0",
"express": "^4.17.1"
}
}
2 changes: 1 addition & 1 deletion examples/payload/v03/structured-event-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"data":{
"much":"wow"
},
"my-extension" : {
"myextension" : {
"some" : "thing"
}
}
2 changes: 1 addition & 1 deletion examples/payload/v03/structured-event-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"datacontenttype":"application/json",
"datacontentencoding":"base64",
"data":"eyJtdWNoIjoid293In0=",
"my-extension" : {
"myextension" : {
"some" : "thing"
}
}
2 changes: 1 addition & 1 deletion examples/payload/v1/structured-event-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"data":{
"much":"wow"
},
"my-extension" : "something"
"myextension" : "something"
}
2 changes: 1 addition & 1 deletion examples/payload/v1/structured-event-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"time":"2019-11-06T11:08:00Z",
"datacontenttype":"application/json",
"data_base64":"eyJtdWNoIjoid293In0=",
"my-extension" : "something"
"myextension" : "something"
}
4 changes: 3 additions & 1 deletion examples/typescript-ex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
},
"devDependencies": {
"@types/node": "^8.9.0",
"cloudevents-sdk": "~2.0.2",
"gts": "^1.1.0",
"typescript": "~3.9.5"
},
"dependencies": {
"cloudevents": "~3.0.1"
}
}
15 changes: 6 additions & 9 deletions examples/typescript-ex/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { CloudEvent, CloudEventV1, Receiver } from "cloudevents-sdk";

export function doSomeStuff() {
const receiver = new Receiver();
import { CloudEvent, CloudEventV1, Receiver } from "cloudevents";

export function doSomeStuff(): void {
const myevent: CloudEventV1 = new CloudEvent({
source: "/source",
type: "type",
datacontenttype: "text/plain",
dataschema: "https://d.schema.com/my.json",
subject: "cha.json",
data: "my-data",
extension1: "some extension data"
});
myevent.extension1 = "some extension data";

console.log("My structured event:", myevent);

Expand All @@ -23,7 +21,7 @@ export function doSomeStuff() {

// Typically used with an incoming HTTP request where myevent.format() is the actual
// body of the HTTP
console.log("Received structured event:", receiver.accept(headers, myevent));
console.log("Received structured event:", Receiver.accept(headers, myevent));

// ------ receiver binary
const data = {
Expand All @@ -40,10 +38,9 @@ export function doSomeStuff() {
"ce-extension1": "extension1",
};

console.log("My binary event:", receiver.accept(attributes, data));
console.log("My binary event extensions:", receiver.accept(attributes, data));
console.log("My binary event:", Receiver.accept(attributes, data));
console.log("My binary event extensions:", Receiver.accept(attributes, data));

return true;
}

doSomeStuff();
2 changes: 1 addition & 1 deletion examples/websocket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ responds with a CloudEvent containing the body of the Weather API response as th
event data.

You will need to change one line in the `server.js` file and provide your Open
Weather API key.
Weather API key. You can also create a environment variable `OPEN_WEATHER_API_KEY` and store your key there.

To start the server, run `node server.js`.

Expand Down
2 changes: 1 addition & 1 deletion examples/websocket/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const readline = require("readline");
const WebSocket = require("ws");
const ws = new WebSocket("ws://localhost:8080");

const { CloudEvent } = require("cloudevents-sdk");
const { CloudEvent } = require("cloudevents");

const rl = readline.createInterface({
input: process.stdin,
Expand Down
4 changes: 2 additions & 2 deletions examples/websocket/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<title>CloudEvent Example</title>
<script src="../../_bundles/cloudevents-sdk.js"></script>
<script src="../../bundles/cloudevents-sdk.js"></script>
<script>
const CloudEvent = window['cloudevents-sdk'].CloudEvent;
const Version = window['cloudevents-sdk'].Version;
Expand Down Expand Up @@ -59,4 +59,4 @@ <h1>Weather By Zip Code</h1>
<p style="font-family: Arial, Helvetica, sans-serif;" id="summary">
</p>
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion examples/websocket/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"cloudevents-sdk": "^2.0.2",
"cloudevents": "~3.0.1",
"got": "^11.3.0",
"ws": "^7.3.0"
}
Expand Down
6 changes: 3 additions & 3 deletions examples/websocket/server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable no-console */
const got = require("got");

const { CloudEvent } = require("cloudevents-sdk");
const { CloudEvent } = require("cloudevents");
const WebSocket = require("ws");
const wss = new WebSocket.Server({ port: 8080 });

const api = "https://api.openweathermap.org/data/2.5/weather";
const key = "REPLACE WITH API KEY";
const key = process.env.OPEN_WEATHER_API_KEY || "REPLACE WITH API KEY";

console.log("WebSocket server started. Waiting for events.");

Expand All @@ -18,7 +18,7 @@ wss.on("connection", function connection(ws) {
fetch(event.data.zip)
.then((weather) => {
const response = new CloudEvent({
dataContentType: "application/json",
datacontenttype: "application/json",
type: "current.weather",
source: "/weather.server",
data: weather,
Expand Down