diff --git a/samples/http-image-cloudevents/README.md b/samples/http-image-cloudevents/README.md index adec0340..92c1d8a0 100644 --- a/samples/http-image-cloudevents/README.md +++ b/samples/http-image-cloudevents/README.md @@ -3,20 +3,20 @@ Install dependencies: ```sh -pip3 install -r requirements.txt +pip install -r requirements.txt ``` Start server: ```sh -python3 image_sample_server.py +python image_sample_server.py ``` In a new shell, run the client code which sends a structured and binary cloudevent to your local server: ```sh -python3 client.py http://localhost:3000/ +python client.py http://localhost:3000/ ``` ## Test diff --git a/samples/http-image-cloudevents/client.py b/samples/http-image-cloudevents/client.py index a61303f1..fc18b4e5 100644 --- a/samples/http-image-cloudevents/client.py +++ b/samples/http-image-cloudevents/client.py @@ -15,8 +15,12 @@ import sys import requests -from cloudevents_v1.conversion import to_binary, to_structured -from cloudevents_v1.http import CloudEvent + +from cloudevents.core.bindings.http import ( + CloudEvent, + to_binary_event, + to_structured_event, +) resp = requests.get( "https://raw.githubusercontent.com/cncf/artwork/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png" # noqa @@ -27,6 +31,8 @@ def send_binary_cloud_event(url: str): # Create cloudevent attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.string", "source": "https://example.com/event-producer", } @@ -34,16 +40,18 @@ def send_binary_cloud_event(url: str): event = CloudEvent(attributes, image_bytes) # Create cloudevent HTTP headers and content - headers, body = to_binary(event) + http_message = to_binary_event(event) # Send cloudevent - requests.post(url, headers=headers, data=body) - print(f"Sent {event['id']} of type {event['type']}") + requests.post(url, headers=http_message.headers, data=http_message.body) + print(f"Sent {event.get_id()} of type {event.get_type()}") def send_structured_cloud_event(url: str): # Create cloudevent attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.base64", "source": "https://example.com/event-producer", } @@ -54,11 +62,11 @@ def send_structured_cloud_event(url: str): # Note that to_structured will create a data_base64 data field in # specversion 1.0 (default specversion) if given # an event whose data field is of type bytes. - headers, body = to_structured(event) + http_message = to_structured_event(event) # Send cloudevent - requests.post(url, headers=headers, data=body) - print(f"Sent {event['id']} of type {event['type']}") + requests.post(url, headers=http_message.headers, data=http_message.body) + print(f"Sent {event.get_id()} of type {event.get_type()}") if __name__ == "__main__": diff --git a/samples/http-image-cloudevents/image_sample_server.py b/samples/http-image-cloudevents/image_sample_server.py index ead3e596..900d5c0a 100644 --- a/samples/http-image-cloudevents/image_sample_server.py +++ b/samples/http-image-cloudevents/image_sample_server.py @@ -14,10 +14,11 @@ import io -from cloudevents_v1.http import from_http from flask import Flask, request from PIL import Image +from cloudevents.core.bindings.http import HTTPMessage, from_http_event + app = Flask(__name__) @@ -25,17 +26,15 @@ def home(): # Create a CloudEvent. # data_unmarshaller will cast event.data into an io.BytesIO object - event = from_http( - request.headers, - request.get_data(), - data_unmarshaller=lambda x: io.BytesIO(x), + event = from_http_event( + HTTPMessage(headers=dict(request.headers), body=request.get_data()) ) # Create image from cloudevent data - image = Image.open(event.data) + image = Image.open(io.BytesIO(event.get_data())) # Print - print(f"Found event {event['id']} with image of size {image.size}") + print(f"Found event {event.get_id()} with image of size {image.size}") return f"Found image of size {image.size}", 200 diff --git a/samples/http-image-cloudevents/image_sample_test.py b/samples/http-image-cloudevents/image_sample_test.py index 33895c69..f9f3be6e 100644 --- a/samples/http-image-cloudevents/image_sample_test.py +++ b/samples/http-image-cloudevents/image_sample_test.py @@ -18,11 +18,16 @@ import pytest from client import image_bytes -from cloudevents_v1.conversion import to_binary, to_structured -from cloudevents_v1.http import CloudEvent, from_http from image_sample_server import app from PIL import Image +from cloudevents.core.bindings.http import ( + CloudEvent, + from_http_event, + to_binary_event, + to_structured_event, +) + image_fileobj = io.BytesIO(image_bytes) image_expected_shape = (1880, 363) @@ -36,6 +41,8 @@ def client(): def test_create_binary_image(): # Create image and turn image into bytes attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.string", "source": "https://example.com/event-producer", } @@ -44,25 +51,24 @@ def test_create_binary_image(): event = CloudEvent(attributes, image_bytes) # Create http headers/body content - headers, body = to_binary(event) + http_message = to_binary_event(event) # Unmarshall CloudEvent and re-create image - reconstruct_event = from_http( - headers, body, data_unmarshaller=lambda x: io.BytesIO(x) - ) + reconstruct_event = from_http_event(http_message) - # reconstruct_event.data is an io.BytesIO object due to data_unmarshaller - restore_image = Image.open(reconstruct_event.data) + restore_image = Image.open(io.BytesIO(reconstruct_event.get_data())) assert restore_image.size == image_expected_shape # # Test cloudevent extension from http fields and data - assert isinstance(body, bytes) - assert body == image_bytes + assert isinstance(http_message.body, bytes) + assert http_message.body == image_bytes def test_create_structured_image(): # Create image and turn image into bytes attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.string", "source": "https://example.com/event-producer", } @@ -71,29 +77,28 @@ def test_create_structured_image(): event = CloudEvent(attributes, image_bytes) # Create http headers/body content - headers, body = to_structured(event) + http_message = to_structured_event(event) # Structured has cloudevent attributes marshalled inside the body. For this # reason we must load the byte object to create the python dict containing # the cloudevent attributes - data = json.loads(body) + data = json.loads(http_message.body.decode()) # Test cloudevent extension from http fields and data assert isinstance(data, dict) assert base64.b64decode(data["data_base64"]) == image_bytes # Unmarshall CloudEvent and re-create image - reconstruct_event = from_http( - headers, body, data_unmarshaller=lambda x: io.BytesIO(x) - ) + reconstruct_event = from_http_event(http_message) - # reconstruct_event.data is an io.BytesIO object due to data_unmarshaller - restore_image = Image.open(reconstruct_event.data) + restore_image = Image.open(io.BytesIO(reconstruct_event.get_data())) assert restore_image.size == image_expected_shape def test_server_structured(client): attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.base64", "source": "https://example.com/event-producer", } @@ -104,10 +109,10 @@ def test_server_structured(client): # Note that to_structured will create a data_base64 data field in # specversion 1.0 (default specversion) if given # an event whose data field is of type bytes. - headers, body = to_structured(event) + http_message = to_structured_event(event) # Send cloudevent - r = client.post("/", headers=headers, data=body) + r = client.post("/", headers=http_message.headers, data=http_message.body) assert r.status_code == 200 assert r.data.decode() == f"Found image of size {image_expected_shape}" @@ -115,6 +120,8 @@ def test_server_structured(client): def test_server_binary(client): # Create cloudevent attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.string", "source": "https://example.com/event-producer", } @@ -122,10 +129,10 @@ def test_server_binary(client): event = CloudEvent(attributes, image_bytes) # Create cloudevent HTTP headers and content - headers, body = to_binary(event) + http_message = to_binary_event(event) # Send cloudevent - r = client.post("/", headers=headers, data=body) + r = client.post("/", headers=http_message.headers, data=http_message.body) assert r.status_code == 200 assert r.data.decode() == f"Found image of size {image_expected_shape}" diff --git a/samples/http-image-cloudevents/requirements.txt b/samples/http-image-cloudevents/requirements.txt index 197e001a..3b576569 100644 --- a/samples/http-image-cloudevents/requirements.txt +++ b/samples/http-image-cloudevents/requirements.txt @@ -2,4 +2,4 @@ flask requests Pillow pytest -cloudevents +cloudevents==2.0.0a1 diff --git a/samples/http-json-cloudevents/README.md b/samples/http-json-cloudevents/README.md index 38447da0..02ea42a6 100644 --- a/samples/http-json-cloudevents/README.md +++ b/samples/http-json-cloudevents/README.md @@ -3,20 +3,20 @@ Install dependencies: ```sh -pip3 install -r requirements.txt +pip install -r requirements.txt ``` Start server: ```sh -python3 json_sample_server.py +python json_sample_server.py ``` In a new shell, run the client code which sends a structured and binary cloudevent to your local server: ```sh -python3 client.py http://localhost:3000/ +python client.py http://localhost:3000/ ``` ## Test diff --git a/samples/http-json-cloudevents/client.py b/samples/http-json-cloudevents/client.py index f68f27b3..45c59137 100644 --- a/samples/http-json-cloudevents/client.py +++ b/samples/http-json-cloudevents/client.py @@ -15,47 +15,55 @@ import sys import requests -from cloudevents_v1.conversion import to_binary, to_structured -from cloudevents_v1.http import CloudEvent + +from cloudevents.core.bindings.http import ( + CloudEvent, + to_binary_event, + to_structured_event, +) def send_binary_cloud_event(url): # This data defines a binary cloudevent attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.sampletype1", "source": "https://example.com/event-producer", } data = {"message": "Hello World!"} event = CloudEvent(attributes, data) - headers, body = to_binary(event) + http_message = to_binary_event(event) # send and print event - requests.post(url, headers=headers, data=body) - print(f"Sent {event['id']} from {event['source']} with {event.data}") + requests.post(url, headers=http_message.headers, data=http_message.body) + print(f"Sent {event.get_id()} from {event.get_source()} with {event.get_data()}") def send_structured_cloud_event(url): - # This data defines a binary cloudevent + # This data defines a structured cloudevent attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.sampletype2", "source": "https://example.com/event-producer", } data = {"message": "Hello World!"} event = CloudEvent(attributes, data) - headers, body = to_structured(event) + http_message = to_structured_event(event) # send and print event - requests.post(url, headers=headers, data=body) - print(f"Sent {event['id']} from {event['source']} with {event.data}") + requests.post(url, headers=http_message.headers, data=http_message.body) + print(f"Sent {event.get_id()} from {event.get_source()} with {event.get_data()}") if __name__ == "__main__": # expects a url from command line. - # e.g. python3 client.py http://localhost:3000/ + # e.g. python client.py http://localhost:3000/ if len(sys.argv) < 2: - sys.exit("Usage: python with_requests.py ") + sys.exit("Usage: python client.py ") url = sys.argv[1] send_binary_cloud_event(url) diff --git a/samples/http-json-cloudevents/json_sample_server.py b/samples/http-json-cloudevents/json_sample_server.py index 9fa1a6d1..507d09ba 100644 --- a/samples/http-json-cloudevents/json_sample_server.py +++ b/samples/http-json-cloudevents/json_sample_server.py @@ -12,22 +12,23 @@ # License for the specific language governing permissions and limitations # under the License. -from cloudevents_v1.http import from_http from flask import Flask, request +from cloudevents.core.bindings.http import HTTPMessage, from_http_event + app = Flask(__name__) -# create an endpoint at http://localhost:/3000/ +# create an endpoint at http://localhost:3000/ @app.route("/", methods=["POST"]) def home(): # create a CloudEvent - event = from_http(request.headers, request.get_data()) + event = from_http_event(HTTPMessage(dict(request.headers), request.get_data())) # you can access cloudevent fields as seen below print( - f"Found {event['id']} from {event['source']} with type " - f"{event['type']} and specversion {event['specversion']}" + f"Found {event.get_id()} from {event.get_source()} with type " + f"{event.get_type()} and specversion {event.get_specversion()}" ) return "", 204 diff --git a/samples/http-json-cloudevents/json_sample_test.py b/samples/http-json-cloudevents/json_sample_test.py index 612aade0..5713afd3 100644 --- a/samples/http-json-cloudevents/json_sample_test.py +++ b/samples/http-json-cloudevents/json_sample_test.py @@ -13,10 +13,14 @@ # under the License. import pytest -from cloudevents_v1.conversion import to_binary, to_structured -from cloudevents_v1.http import CloudEvent from json_sample_server import app +from cloudevents.core.bindings.http import ( + CloudEvent, + to_binary_event, + to_structured_event, +) + @pytest.fixture def client(): @@ -27,28 +31,32 @@ def client(): def test_binary_request(client): # This data defines a binary cloudevent attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.sampletype1", "source": "https://example.com/event-producer", } data = {"message": "Hello World!"} event = CloudEvent(attributes, data) - headers, body = to_binary(event) + http_message = to_binary_event(event) - r = client.post("/", headers=headers, data=body) + r = client.post("/", headers=http_message.headers, data=http_message.body) assert r.status_code == 204 def test_structured_request(client): - # This data defines a binary cloudevent + # This data defines a structured cloudevent attributes = { + "id": "123", + "specversion": "1.0", "type": "com.example.sampletype2", "source": "https://example.com/event-producer", } data = {"message": "Hello World!"} event = CloudEvent(attributes, data) - headers, body = to_structured(event) + http_message = to_structured_event(event) - r = client.post("/", headers=headers, data=body) + r = client.post("/", headers=http_message.headers, data=http_message.body) assert r.status_code == 204 diff --git a/samples/http-json-cloudevents/requirements.txt b/samples/http-json-cloudevents/requirements.txt index 1f69ece3..aa7de594 100644 --- a/samples/http-json-cloudevents/requirements.txt +++ b/samples/http-json-cloudevents/requirements.txt @@ -1,4 +1,4 @@ flask requests pytest -cloudevents +cloudevents==2.0.0a1