Skip to content

Commit 4efb292

Browse files
committed
chore: update http-json-cloudevents examples to use cloudevents v2
1 parent 2b57924 commit 4efb292

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

samples/http-json-cloudevents/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
Install dependencies:
44

55
```sh
6-
pip3 install -r requirements.txt
6+
pip install -r requirements.txt
77
```
88

99
Start server:
1010

1111
```sh
12-
python3 json_sample_server.py
12+
python json_sample_server.py
1313
```
1414

1515
In a new shell, run the client code which sends a structured and binary
1616
cloudevent to your local server:
1717

1818
```sh
19-
python3 client.py http://localhost:3000/
19+
python client.py http://localhost:3000/
2020
```
2121

2222
## Test

samples/http-json-cloudevents/client.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,55 @@
1515
import sys
1616

1717
import requests
18-
from cloudevents_v1.conversion import to_binary, to_structured
19-
from cloudevents_v1.http import CloudEvent
18+
19+
from cloudevents.core.bindings.http import (
20+
CloudEvent,
21+
to_binary_event,
22+
to_structured_event,
23+
)
2024

2125

2226
def send_binary_cloud_event(url):
2327
# This data defines a binary cloudevent
2428
attributes = {
29+
"id": "123",
30+
"specversion": "1.0",
2531
"type": "com.example.sampletype1",
2632
"source": "https://example.com/event-producer",
2733
}
2834
data = {"message": "Hello World!"}
2935

3036
event = CloudEvent(attributes, data)
31-
headers, body = to_binary(event)
37+
http_message = to_binary_event(event)
3238

3339
# send and print event
34-
requests.post(url, headers=headers, data=body)
35-
print(f"Sent {event['id']} from {event['source']} with {event.data}")
40+
requests.post(url, headers=http_message.headers, data=http_message.body)
41+
print(f"Sent {event.get_id()} from {event.get_source()} with {event.get_data()}")
3642

3743

3844
def send_structured_cloud_event(url):
39-
# This data defines a binary cloudevent
45+
# This data defines a structured cloudevent
4046
attributes = {
47+
"id": "123",
48+
"specversion": "1.0",
4149
"type": "com.example.sampletype2",
4250
"source": "https://example.com/event-producer",
4351
}
4452
data = {"message": "Hello World!"}
4553

4654
event = CloudEvent(attributes, data)
47-
headers, body = to_structured(event)
55+
http_message = to_structured_event(event)
4856

4957
# send and print event
50-
requests.post(url, headers=headers, data=body)
51-
print(f"Sent {event['id']} from {event['source']} with {event.data}")
58+
requests.post(url, headers=http_message.headers, data=http_message.body)
59+
print(f"Sent {event.get_id()} from {event.get_source()} with {event.get_data()}")
5260

5361

5462
if __name__ == "__main__":
5563
# expects a url from command line.
56-
# e.g. python3 client.py http://localhost:3000/
64+
# e.g. python client.py http://localhost:3000/
5765
if len(sys.argv) < 2:
58-
sys.exit("Usage: python with_requests.py <CloudEvents controller URL>")
66+
sys.exit("Usage: python client.py <CloudEvents controller URL>")
5967

6068
url = sys.argv[1]
6169
send_binary_cloud_event(url)

samples/http-json-cloudevents/json_sample_server.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from cloudevents_v1.http import from_http
1615
from flask import Flask, request
1716

17+
from cloudevents.core.bindings.http import HTTPMessage, from_http_event
18+
1819
app = Flask(__name__)
1920

2021

21-
# create an endpoint at http://localhost:/3000/
22+
# create an endpoint at http://localhost:3000/
2223
@app.route("/", methods=["POST"])
2324
def home():
2425
# create a CloudEvent
25-
event = from_http(request.headers, request.get_data())
26+
event = from_http_event(HTTPMessage(dict(request.headers), request.get_data()))
2627

2728
# you can access cloudevent fields as seen below
2829
print(
29-
f"Found {event['id']} from {event['source']} with type "
30-
f"{event['type']} and specversion {event['specversion']}"
30+
f"Found {event.get_id()} from {event.get_source()} with type "
31+
f"{event.get_type()} and specversion {event.get_specversion()}"
3132
)
3233

3334
return "", 204

samples/http-json-cloudevents/json_sample_test.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
# under the License.
1414

1515
import pytest
16-
from cloudevents_v1.conversion import to_binary, to_structured
17-
from cloudevents_v1.http import CloudEvent
1816
from json_sample_server import app
1917

18+
from cloudevents.core.bindings.http import (
19+
CloudEvent,
20+
to_binary_event,
21+
to_structured_event,
22+
)
23+
2024

2125
@pytest.fixture
2226
def client():
@@ -27,28 +31,32 @@ def client():
2731
def test_binary_request(client):
2832
# This data defines a binary cloudevent
2933
attributes = {
34+
"id": "123",
35+
"specversion": "1.0",
3036
"type": "com.example.sampletype1",
3137
"source": "https://example.com/event-producer",
3238
}
3339
data = {"message": "Hello World!"}
3440

3541
event = CloudEvent(attributes, data)
36-
headers, body = to_binary(event)
42+
http_message = to_binary_event(event)
3743

38-
r = client.post("/", headers=headers, data=body)
44+
r = client.post("/", headers=http_message.headers, data=http_message.body)
3945
assert r.status_code == 204
4046

4147

4248
def test_structured_request(client):
43-
# This data defines a binary cloudevent
49+
# This data defines a structured cloudevent
4450
attributes = {
51+
"id": "123",
52+
"specversion": "1.0",
4553
"type": "com.example.sampletype2",
4654
"source": "https://example.com/event-producer",
4755
}
4856
data = {"message": "Hello World!"}
4957

5058
event = CloudEvent(attributes, data)
51-
headers, body = to_structured(event)
59+
http_message = to_structured_event(event)
5260

53-
r = client.post("/", headers=headers, data=body)
61+
r = client.post("/", headers=http_message.headers, data=http_message.body)
5462
assert r.status_code == 204
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
flask
22
requests
33
pytest
4-
cloudevents
4+
cloudevents==2.0.0a1

0 commit comments

Comments
 (0)