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 2 commits into
base: v1.14
Choose a base branch
from

Conversation

hhunter-ms
Copy link
Collaborator

Description

Add non-SDK python example for bulk subscribe

Issue reference

PR will close: #3321

@hhunter-ms hhunter-ms requested review from a team as code owners October 28, 2024 19:32
@hhunter-ms hhunter-ms self-assigned this Oct 28, 2024
Signed-off-by: Hannah Hunter <[email protected]>
Copy link
Contributor

@elena-kolevska elena-kolevska left a 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.

Comment on lines +477 to +519
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Copy link

Stale PR, paging all reviewers

@github-actions github-actions bot added the stale label Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bulk Subscribe] Need Python non-SDK code snippet
2 participants