Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-SDK Python code example for Bulk Subscribe #4407

Open
wants to merge 1 commit into
base: v1.14
Choose a base branch
from
Open
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
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
Loading