|
15 | 15 | import sys |
16 | 16 |
|
17 | 17 | 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 | +) |
20 | 24 |
|
21 | 25 |
|
22 | 26 | def send_binary_cloud_event(url): |
23 | 27 | # This data defines a binary cloudevent |
24 | 28 | attributes = { |
| 29 | + "id": "123", |
| 30 | + "specversion": "1.0", |
25 | 31 | "type": "com.example.sampletype1", |
26 | 32 | "source": "https://example.com/event-producer", |
27 | 33 | } |
28 | 34 | data = {"message": "Hello World!"} |
29 | 35 |
|
30 | 36 | event = CloudEvent(attributes, data) |
31 | | - headers, body = to_binary(event) |
| 37 | + http_message = to_binary_event(event) |
32 | 38 |
|
33 | 39 | # 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()}") |
36 | 42 |
|
37 | 43 |
|
38 | 44 | def send_structured_cloud_event(url): |
39 | | - # This data defines a binary cloudevent |
| 45 | + # This data defines a structured cloudevent |
40 | 46 | attributes = { |
| 47 | + "id": "123", |
| 48 | + "specversion": "1.0", |
41 | 49 | "type": "com.example.sampletype2", |
42 | 50 | "source": "https://example.com/event-producer", |
43 | 51 | } |
44 | 52 | data = {"message": "Hello World!"} |
45 | 53 |
|
46 | 54 | event = CloudEvent(attributes, data) |
47 | | - headers, body = to_structured(event) |
| 55 | + http_message = to_structured_event(event) |
48 | 56 |
|
49 | 57 | # 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()}") |
52 | 60 |
|
53 | 61 |
|
54 | 62 | if __name__ == "__main__": |
55 | 63 | # expects a url from command line. |
56 | | - # e.g. python3 client.py http://localhost:3000/ |
| 64 | + # e.g. python client.py http://localhost:3000/ |
57 | 65 | 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>") |
59 | 67 |
|
60 | 68 | url = sys.argv[1] |
61 | 69 | send_binary_cloud_event(url) |
|
0 commit comments