Skip to content

Commit 8b35ec0

Browse files
Merge branch 'release-1.15' into rc3
2 parents 1e3adbf + ba64f31 commit 8b35ec0

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

daprdocs/content/en/python-sdk-docs/python-client.md

+50
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,56 @@ with DaprClient() as d:
227227
resp = d.publish_event(pubsub_name='pubsub', topic_name='TOPIC_A', data='{"message":"Hello World"}')
228228
```
229229

230+
231+
Send [CloudEvents](https://cloudevents.io/) messages with a json payload:
232+
```python
233+
from dapr.clients import DaprClient
234+
import json
235+
236+
with DaprClient() as d:
237+
cloud_event = {
238+
'specversion': '1.0',
239+
'type': 'com.example.event',
240+
'source': 'my-service',
241+
'id': 'myid',
242+
'data': {'id': 1, 'message': 'hello world'},
243+
'datacontenttype': 'application/json',
244+
}
245+
246+
# Set the data content type to 'application/cloudevents+json'
247+
resp = d.publish_event(
248+
pubsub_name='pubsub',
249+
topic_name='TOPIC_CE',
250+
data=json.dumps(cloud_event),
251+
data_content_type='application/cloudevents+json',
252+
)
253+
```
254+
255+
Publish [CloudEvents](https://cloudevents.io/) messages with plain text payload:
256+
```python
257+
from dapr.clients import DaprClient
258+
import json
259+
260+
with DaprClient() as d:
261+
cloud_event = {
262+
'specversion': '1.0',
263+
'type': 'com.example.event',
264+
'source': 'my-service',
265+
'id': "myid",
266+
'data': 'hello world',
267+
'datacontenttype': 'text/plain',
268+
}
269+
270+
# Set the data content type to 'application/cloudevents+json'
271+
resp = d.publish_event(
272+
pubsub_name='pubsub',
273+
topic_name='TOPIC_CE',
274+
data=json.dumps(cloud_event),
275+
data_content_type='application/cloudevents+json',
276+
)
277+
```
278+
279+
230280
#### Subscribe to messages
231281

232282
```python

examples/pubsub-simple/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ expected_stdout_lines:
3737
- '== APP == Dead-Letter Subscriber received: id=7, message="hello world", content_type="application/json"'
3838
- '== APP == Dead-Letter Subscriber. Received via deadletter topic: TOPIC_D_DEAD'
3939
- '== APP == Dead-Letter Subscriber. Originally intended topic: TOPIC_D'
40+
- '== APP == Subscriber received: TOPIC_CE'
41+
- '== APP == Subscriber received a json cloud event: id=8, message="hello world", content_type="application/json"'
42+
- '== APP == Subscriber received: TOPIC_CE'
43+
- '== APP == Subscriber received plain text cloud event: hello world, content_type="text/plain"'
44+
4045
output_match_mode: substring
4146
background: true
4247
match_order: none
@@ -45,7 +50,7 @@ sleep: 3
4550

4651
```bash
4752
# 1. Start Subscriber (expose gRPC server receiver on port 50051)
48-
dapr run --app-id python-subscriber --app-protocol grpc --app-port 50051 python3 subscriber.py
53+
dapr run --app-id python-subscriber --app-protocol grpc --app-port 50051 -- python3 subscriber.py
4954
```
5055

5156
<!-- END_STEP -->
@@ -60,6 +65,10 @@ expected_stdout_lines:
6065
- "== APP == {'id': 3, 'message': 'hello world'}"
6166
- "== APP == {'id': 4, 'message': 'hello world'}"
6267
- "== APP == {'id': 5, 'message': 'hello world'}"
68+
- "== APP == {'id': 6, 'message': 'hello world'}"
69+
- "== APP == {'id': 7, 'message': 'hello world'}"
70+
- "== APP == {'specversion': '1.0', 'type': 'com.example.event', 'source': 'my-service', 'id': 'abc-8', 'data': {'id': 8, 'message': 'hello world'}, 'datacontenttype': 'application/json'}"
71+
- "== APP == {'specversion': '1.0', 'type': 'com.example.event', 'source': 'my-service', 'id': 'abc-10', 'data': 'hello world', 'datacontenttype': 'text/plain'}"
6372
background: true
6473
sleep: 15
6574
-->

examples/pubsub-simple/publisher.py

+48
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,51 @@
6666

6767
# Print the request
6868
print(req_data, flush=True)
69+
70+
## Send a cloud event with json data
71+
id = 8
72+
cloud_event = {
73+
'specversion': '1.0',
74+
'type': 'com.example.event',
75+
'source': 'my-service',
76+
'id': f'abc-{id}',
77+
'data': {'id': id, 'message': 'hello world'},
78+
'datacontenttype': 'application/json',
79+
}
80+
81+
# Set the data content type to 'application/cloudevents+json'
82+
resp = d.publish_event(
83+
pubsub_name='pubsub',
84+
topic_name='TOPIC_CE',
85+
data=json.dumps(cloud_event),
86+
data_content_type='application/cloudevents+json',
87+
)
88+
89+
# Print the request
90+
print(cloud_event, flush=True)
91+
92+
time.sleep(0.5)
93+
94+
# Send a cloud event with plain text data
95+
id = 10
96+
cloud_event = {
97+
'specversion': '1.0',
98+
'type': 'com.example.event',
99+
'source': 'my-service',
100+
'id': f'abc-{id}',
101+
'data': 'hello world',
102+
'datacontenttype': 'text/plain',
103+
}
104+
105+
# Set the data content type to 'application/cloudevents+json'
106+
resp = d.publish_event(
107+
pubsub_name='pubsub',
108+
topic_name='TOPIC_CE',
109+
data=json.dumps(cloud_event),
110+
data_content_type='application/cloudevents+json',
111+
)
112+
113+
# Print the request
114+
print(cloud_event, flush=True)
115+
116+
time.sleep(0.5)

examples/pubsub-simple/subscriber.py

+36
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,42 @@ def mytopic(event: v1.Event) -> TopicEventResponse:
4040
return TopicEventResponse('success')
4141

4242

43+
@app.subscribe(pubsub_name='pubsub', topic='TOPIC_CE')
44+
def receive_cloud_events(event: v1.Event) -> TopicEventResponse:
45+
print('Subscriber received: ' + event.Subject(), flush=True)
46+
47+
content_type = event.content_type
48+
data = event.Data()
49+
50+
try:
51+
if content_type == 'application/json':
52+
# Handle JSON data
53+
json_data = json.loads(data)
54+
print(
55+
f'Subscriber received a json cloud event: id={json_data["id"]}, message="{json_data["message"]}", '
56+
f'content_type="{event.content_type}"',
57+
flush=True,
58+
)
59+
elif content_type == 'text/plain':
60+
# Handle plain text data
61+
if isinstance(data, bytes):
62+
data = data.decode('utf-8')
63+
print(
64+
f'Subscriber received plain text cloud event: {data}, '
65+
f'content_type="{content_type}"',
66+
flush=True,
67+
)
68+
else:
69+
print(f'Received unknown content type: {content_type}', flush=True)
70+
return TopicEventResponse('fail')
71+
72+
except Exception as e:
73+
print('Failed to process event data:', e, flush=True)
74+
return TopicEventResponse('fail')
75+
76+
return TopicEventResponse('success')
77+
78+
4379
@app.subscribe(pubsub_name='pubsub', topic='TOPIC_D', dead_letter_topic='TOPIC_D_DEAD')
4480
def fail_and_send_to_dead_topic(event: v1.Event) -> TopicEventResponse:
4581
return TopicEventResponse('retry')

0 commit comments

Comments
 (0)