Created
August 27, 2018 07:56
-
-
Save Mhs-220/83be621f7cd2b041343ebab82bf49025 to your computer and use it in GitHub Desktop.
A simple handler for aws sns in marketplace for flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@mod_auth.route('/handle_sns', methods = ['GET', 'POST', 'PUT']) | |
def handle_sns(): | |
try: | |
notification = json.loads(request.data) | |
notification_type = notification["Type"] | |
if notification_type == 'SubscriptionConfirmation': | |
if 'SubscribeURL' in notification: | |
url = notification['SubscribeURL'] | |
print("Click for subscribe -> %s" % url) | |
# There is another state for unsubscribe confirmation, | |
# but it's not necessary in my case | |
if notification_type == 'Notification': | |
message = json.loads(notification["Message"]) | |
action = message["action"] | |
customer_identifier = message["customer-identifier"] | |
# I use SNS for subscribe or unsubscribe user from marketplace, | |
# so i just need to check specific action, maybe you're need | |
# from sns is much diffrent from mine. | |
user = User.query.filter_by( | |
aws_id = customer_identifier, | |
).first() | |
if action == "unsubscribe-success": | |
user.is_subscribed = False | |
user.save() | |
elif action == "subscribe-success": | |
user.is_subscribed = True | |
user.save() | |
elif action == "subscribe-fail": | |
user.is_subscribed = False | |
user.save() | |
else: | |
# Do what do you want with your notifications | |
return jsonify({'status': 'OK'}) | |
except Exception as e: | |
app.logger.info(e) | |
return jsonify({'status': 'Something went wrong'}), 500 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment