From a7e578aae42c871ddd60cdec5f8c3ce732394db6 Mon Sep 17 00:00:00 2001 From: Hannah Hunter <94493363+hhunter-ms@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:13:23 -0500 Subject: [PATCH] Update daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md Co-authored-by: Elena Kolevska Signed-off-by: Hannah Hunter <94493363+hhunter-ms@users.noreply.github.com> --- .../building-blocks/pubsub/pubsub-bulk.md | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md index 4623508001c..5131d9080d4 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md +++ b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md @@ -474,49 +474,40 @@ public class BulkMessageController : ControllerBase Currently, you can only bulk subscribe in Python using an HTTP client. ```python -import requests import json +from flask import Flask, request, jsonify -# Define the Dapr sidecar URL -DAPR_URL = "http://localhost:3500/v1.0" - -# Define the subscription endpoint -SUBSCRIBE_URL = f"{DAPR_URL}/subscribe" - -# Define the bulk subscribe configuration -subscription = { - "pubsubname": "order-pub-sub", - "topic": "orders", - "route": "/checkout", - "bulkSubscribe": { - "enabled": True, - "maxMessagesCount": 100, - "maxAwaitDurationMs": 40 - } -} +app = Flask(__name__) -# Send the subscription request -response = requests.post(SUBSCRIBE_URL, json=subscription) +@app.route('/dapr/subscribe', methods=['GET']) +def subscribe(): + # Define the bulk subscribe configuration + subscriptions = [{ + "pubsubname": "pubsub", + "topic": "TOPIC_A", + "route": "/checkout", + "bulkSubscribe": { + "enabled": True, + "maxMessagesCount": 3, + "maxAwaitDurationMs": 40 + } + }] + print('Dapr pub/sub is subscribed to: ' + json.dumps(subscriptions)) + return jsonify(subscriptions) -if response.status_code == 200: - print("Bulk subscription created successfully!") -else: - print(f"Failed to create bulk subscription: {response.status_code} - {response.text}") # Define the endpoint to handle incoming messages -from flask import Flask, request - -app = Flask(__name__) - @app.route('/checkout', methods=['POST']) def checkout(): messages = request.json + print(messages) for message in messages: print(f"Received message: {message}") - return '', 200 + return json.dumps({'success': True}), 200, {'ContentType': 'application/json'} if __name__ == '__main__': app.run(port=5000) + ``` {{% /codetab %}}