Skip to content
Open
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
6 changes: 3 additions & 3 deletions samples/http-image-cloudevents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions samples/http-image-cloudevents/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

from cloudevents.core.v1.event import CloudEvent

and then the http specific imports, otherwise you're importing an import from http.py module.

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
Expand All @@ -27,23 +31,27 @@
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",
}

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",
}
Expand All @@ -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__":
Expand Down
13 changes: 6 additions & 7 deletions samples/http-image-cloudevents/image_sample_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,27 @@

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__)


@app.route("/", methods=["POST"])
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


Expand Down
49 changes: 28 additions & 21 deletions samples/http-image-cloudevents/image_sample_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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",
}
Expand All @@ -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",
}
Expand All @@ -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",
}
Expand All @@ -104,28 +109,30 @@ 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}"


def test_server_binary(client):
# Create cloudevent
attributes = {
"id": "123",
"specversion": "1.0",
"type": "com.example.string",
"source": "https://example.com/event-producer",
}

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}"

Expand Down
2 changes: 1 addition & 1 deletion samples/http-image-cloudevents/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ flask
requests
Pillow
pytest
cloudevents
cloudevents==2.0.0a1
6 changes: 3 additions & 3 deletions samples/http-json-cloudevents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions samples/http-json-cloudevents/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CloudEvents controller URL>")
sys.exit("Usage: python client.py <CloudEvents controller URL>")

url = sys.argv[1]
send_binary_cloud_event(url)
Expand Down
11 changes: 6 additions & 5 deletions samples/http-json-cloudevents/json_sample_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading