-
Notifications
You must be signed in to change notification settings - Fork 726
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
base: v1.14
Are you sure you want to change the base?
Conversation
Signed-off-by: Hannah Hunter <[email protected]>
Signed-off-by: Hannah Hunter <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example didn't work for me when I ran it, @hhunter-ms were you able to get it to run? I modified it a bit and I'm able to see messages on the subscriber side now, but feel free to ignore if the original version works.
import requests | ||
import json | ||
|
||
# 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 | ||
} | ||
} | ||
|
||
# Send the subscription request | ||
response = requests.post(SUBSCRIBE_URL, json=subscription) | ||
|
||
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 | ||
for message in messages: | ||
print(f"Received message: {message}") | ||
return '', 200 | ||
|
||
if __name__ == '__main__': | ||
app.run(port=5000) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import requests | |
import json | |
# 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 | |
} | |
} | |
# Send the subscription request | |
response = requests.post(SUBSCRIBE_URL, json=subscription) | |
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 | |
for message in messages: | |
print(f"Received message: {message}") | |
return '', 200 | |
if __name__ == '__main__': | |
app.run(port=5000) | |
import json | |
from flask import Flask, request, jsonify | |
app = Flask(__name__) | |
@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) | |
# Define the endpoint to handle incoming messages | |
@app.route('/checkout', methods=['POST']) | |
def checkout(): | |
messages = request.json | |
print(messages) | |
for message in messages: | |
print(f"Received message: {message}") | |
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'} | |
if __name__ == '__main__': | |
app.run(port=5000) | |
Stale PR, paging all reviewers |
Description
Add non-SDK python example for bulk subscribe
Issue reference
PR will close: #3321