Skip to content

Commit

Permalink
add AI generated python code example
Browse files Browse the repository at this point in the history
Signed-off-by: Hannah Hunter <[email protected]>
  • Loading branch information
hhunter-ms committed Oct 28, 2024
1 parent 18bbd6d commit c8dc1a9
Showing 1 changed file with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,13 @@ Status | Description
`RETRY` | Message to be retried by Dapr
`DROP` | Warning is logged and message is dropped

Please refer [Expected HTTP Response for Bulk Subscribe]({{< ref pubsub_api.md >}}) for further insights on response.
Refer to [Expected HTTP Response for Bulk Subscribe]({{< ref pubsub_api.md >}}) for further insights on response.

### Example

Please refer following code samples for how to use Bulk Subscribe:

{{< tabs "Java" "JavaScript" ".NET" >}}
The following code examples demonstrate how to use Bulk Subscribe.

{{< tabs "Java" "JavaScript" ".NET" "Python" >}}
{{% codetab %}}

```java
Expand Down Expand Up @@ -471,7 +470,57 @@ public class BulkMessageController : ControllerBase

{{% /codetab %}}

{{% codetab %}}

```python
import requests
import json
from flask import Flask, request
app = Flask(__name__)
# Define the Dapr sidecar URL
DAPR_URL = "http://localhost:3500/v1.0"
# Define the bulk subscribe endpoint
BULK_SUBSCRIBE_ENDPOINT = f"{DAPR_URL}/subscribe/bulk"
# Define the subscription details
subscription = {
"pubsubname": "my-pubsub-name",
"topic": "topic-a",
"route": "/events",
"metadata": {
"bulkSubscribe": "true",
"maxMessagesCount": "100",
"maxAwaitDurationMs": "40"
}
}
# Register the subscription
response = requests.post(BULK_SUBSCRIBE_ENDPOINT, json=subscription)
if response.status_code == 200:
print("Bulk subscription registered successfully!")
else:
print(f"Failed to register bulk subscription: {response.status_code} - {response.text}")
# Define the event handler
@app.route('/events', methods=['POST'])
def handle_events():
events = request.json
for event in events:
print(f"Received event: {event}")
return '', 200
if __name__ == '__main__':
app.run(port=5000)
```

{{% /codetab %}}

{{< /tabs >}}

## How components handle publishing and subscribing to bulk messages

For event publish/subscribe, two kinds of network transfers are involved.
Expand Down

0 comments on commit c8dc1a9

Please sign in to comment.