Skip to content

Commit 2b57924

Browse files
committed
chore: update http-image-cloudevents samples to use cloudevents v2
1 parent 7001341 commit 2b57924

File tree

5 files changed

+54
-40
lines changed

5 files changed

+54
-40
lines changed

samples/http-image-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 image_sample_server.py
12+
python image_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-image-cloudevents/client.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
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
resp = requests.get(
2226
"https://raw.githubusercontent.com/cncf/artwork/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png" # noqa
@@ -27,23 +31,27 @@
2731
def send_binary_cloud_event(url: str):
2832
# Create cloudevent
2933
attributes = {
34+
"id": "123",
35+
"specversion": "1.0",
3036
"type": "com.example.string",
3137
"source": "https://example.com/event-producer",
3238
}
3339

3440
event = CloudEvent(attributes, image_bytes)
3541

3642
# Create cloudevent HTTP headers and content
37-
headers, body = to_binary(event)
43+
http_message = to_binary_event(event)
3844

3945
# Send cloudevent
40-
requests.post(url, headers=headers, data=body)
41-
print(f"Sent {event['id']} of type {event['type']}")
46+
requests.post(url, headers=http_message.headers, data=http_message.body)
47+
print(f"Sent {event.get_id()} of type {event.get_type()}")
4248

4349

4450
def send_structured_cloud_event(url: str):
4551
# Create cloudevent
4652
attributes = {
53+
"id": "123",
54+
"specversion": "1.0",
4755
"type": "com.example.base64",
4856
"source": "https://example.com/event-producer",
4957
}
@@ -54,11 +62,11 @@ def send_structured_cloud_event(url: str):
5462
# Note that to_structured will create a data_base64 data field in
5563
# specversion 1.0 (default specversion) if given
5664
# an event whose data field is of type bytes.
57-
headers, body = to_structured(event)
65+
http_message = to_structured_event(event)
5866

5967
# Send cloudevent
60-
requests.post(url, headers=headers, data=body)
61-
print(f"Sent {event['id']} of type {event['type']}")
68+
requests.post(url, headers=http_message.headers, data=http_message.body)
69+
print(f"Sent {event.get_id()} of type {event.get_type()}")
6270

6371

6472
if __name__ == "__main__":

samples/http-image-cloudevents/image_sample_server.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,27 @@
1414

1515
import io
1616

17-
from cloudevents_v1.http import from_http
1817
from flask import Flask, request
1918
from PIL import Image
2019

20+
from cloudevents.core.bindings.http import HTTPMessage, from_http_event
21+
2122
app = Flask(__name__)
2223

2324

2425
@app.route("/", methods=["POST"])
2526
def home():
2627
# Create a CloudEvent.
2728
# data_unmarshaller will cast event.data into an io.BytesIO object
28-
event = from_http(
29-
request.headers,
30-
request.get_data(),
31-
data_unmarshaller=lambda x: io.BytesIO(x),
29+
event = from_http_event(
30+
HTTPMessage(headers=dict(request.headers), body=request.get_data())
3231
)
3332

3433
# Create image from cloudevent data
35-
image = Image.open(event.data)
34+
image = Image.open(io.BytesIO(event.get_data()))
3635

3736
# Print
38-
print(f"Found event {event['id']} with image of size {image.size}")
37+
print(f"Found event {event.get_id()} with image of size {image.size}")
3938
return f"Found image of size {image.size}", 200
4039

4140

samples/http-image-cloudevents/image_sample_test.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818

1919
import pytest
2020
from client import image_bytes
21-
from cloudevents_v1.conversion import to_binary, to_structured
22-
from cloudevents_v1.http import CloudEvent, from_http
2321
from image_sample_server import app
2422
from PIL import Image
2523

24+
from cloudevents.core.bindings.http import (
25+
CloudEvent,
26+
from_http_event,
27+
to_binary_event,
28+
to_structured_event,
29+
)
30+
2631
image_fileobj = io.BytesIO(image_bytes)
2732
image_expected_shape = (1880, 363)
2833

@@ -36,6 +41,8 @@ def client():
3641
def test_create_binary_image():
3742
# Create image and turn image into bytes
3843
attributes = {
44+
"id": "123",
45+
"specversion": "1.0",
3946
"type": "com.example.string",
4047
"source": "https://example.com/event-producer",
4148
}
@@ -44,25 +51,24 @@ def test_create_binary_image():
4451
event = CloudEvent(attributes, image_bytes)
4552

4653
# Create http headers/body content
47-
headers, body = to_binary(event)
54+
http_message = to_binary_event(event)
4855

4956
# Unmarshall CloudEvent and re-create image
50-
reconstruct_event = from_http(
51-
headers, body, data_unmarshaller=lambda x: io.BytesIO(x)
52-
)
57+
reconstruct_event = from_http_event(http_message)
5358

54-
# reconstruct_event.data is an io.BytesIO object due to data_unmarshaller
55-
restore_image = Image.open(reconstruct_event.data)
59+
restore_image = Image.open(io.BytesIO(reconstruct_event.get_data()))
5660
assert restore_image.size == image_expected_shape
5761

5862
# # Test cloudevent extension from http fields and data
59-
assert isinstance(body, bytes)
60-
assert body == image_bytes
63+
assert isinstance(http_message.body, bytes)
64+
assert http_message.body == image_bytes
6165

6266

6367
def test_create_structured_image():
6468
# Create image and turn image into bytes
6569
attributes = {
70+
"id": "123",
71+
"specversion": "1.0",
6672
"type": "com.example.string",
6773
"source": "https://example.com/event-producer",
6874
}
@@ -71,29 +77,28 @@ def test_create_structured_image():
7177
event = CloudEvent(attributes, image_bytes)
7278

7379
# Create http headers/body content
74-
headers, body = to_structured(event)
80+
http_message = to_structured_event(event)
7581

7682
# Structured has cloudevent attributes marshalled inside the body. For this
7783
# reason we must load the byte object to create the python dict containing
7884
# the cloudevent attributes
79-
data = json.loads(body)
85+
data = json.loads(http_message.body.decode())
8086

8187
# Test cloudevent extension from http fields and data
8288
assert isinstance(data, dict)
8389
assert base64.b64decode(data["data_base64"]) == image_bytes
8490

8591
# Unmarshall CloudEvent and re-create image
86-
reconstruct_event = from_http(
87-
headers, body, data_unmarshaller=lambda x: io.BytesIO(x)
88-
)
92+
reconstruct_event = from_http_event(http_message)
8993

90-
# reconstruct_event.data is an io.BytesIO object due to data_unmarshaller
91-
restore_image = Image.open(reconstruct_event.data)
94+
restore_image = Image.open(io.BytesIO(reconstruct_event.get_data()))
9295
assert restore_image.size == image_expected_shape
9396

9497

9598
def test_server_structured(client):
9699
attributes = {
100+
"id": "123",
101+
"specversion": "1.0",
97102
"type": "com.example.base64",
98103
"source": "https://example.com/event-producer",
99104
}
@@ -104,28 +109,30 @@ def test_server_structured(client):
104109
# Note that to_structured will create a data_base64 data field in
105110
# specversion 1.0 (default specversion) if given
106111
# an event whose data field is of type bytes.
107-
headers, body = to_structured(event)
112+
http_message = to_structured_event(event)
108113

109114
# Send cloudevent
110-
r = client.post("/", headers=headers, data=body)
115+
r = client.post("/", headers=http_message.headers, data=http_message.body)
111116
assert r.status_code == 200
112117
assert r.data.decode() == f"Found image of size {image_expected_shape}"
113118

114119

115120
def test_server_binary(client):
116121
# Create cloudevent
117122
attributes = {
123+
"id": "123",
124+
"specversion": "1.0",
118125
"type": "com.example.string",
119126
"source": "https://example.com/event-producer",
120127
}
121128

122129
event = CloudEvent(attributes, image_bytes)
123130

124131
# Create cloudevent HTTP headers and content
125-
headers, body = to_binary(event)
132+
http_message = to_binary_event(event)
126133

127134
# Send cloudevent
128-
r = client.post("/", headers=headers, data=body)
135+
r = client.post("/", headers=http_message.headers, data=http_message.body)
129136
assert r.status_code == 200
130137
assert r.data.decode() == f"Found image of size {image_expected_shape}"
131138

samples/http-image-cloudevents/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ flask
22
requests
33
Pillow
44
pytest
5-
cloudevents
5+
cloudevents==2.0.0a1

0 commit comments

Comments
 (0)