From 701b9c93bf240411c18689f09b99033edd6a3b63 Mon Sep 17 00:00:00 2001 From: idelgado Date: Fri, 16 Jun 2017 18:18:35 -0400 Subject: [PATCH 1/3] Allow for generic token, registration, and send-notification routes --- app.py | 33 ++++++++++++++++++--------------- requirements.txt | 1 + 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index e9785d2..d99e757 100644 --- a/app.py +++ b/app.py @@ -10,7 +10,11 @@ from twilio.jwt.access_token.grants import ( ) from dotenv import load_dotenv, find_dotenv from os.path import join, dirname +from inflection import underscore +# Convert keys to snake_case to conform with the python api definition contract +def snake_case_keys(somedict): + return dict(map(lambda (key, value): (underscore(key), value), somedict.items())) app = Flask(__name__) fake = Factory.create() @@ -50,8 +54,8 @@ def config(): TWILIO_SYNC_SERVICE_SID=os.environ['TWILIO_SYNC_SERVICE_SID'], ) -@app.route('/token') -def token(): +@app.route('/token', methods=['POST', 'GET']) +def token(identity = None): # get credentials for environment variables account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] @@ -59,9 +63,8 @@ def token(): sync_service_sid = os.environ['TWILIO_SYNC_SERVICE_SID'] chat_service_sid = os.environ['TWILIO_CHAT_SERVICE_SID'] - - # create a randomly generated username for the client - identity = fake.user_name() + # assign the provided identity or generate a new one + identity = identity or fake.user_name() # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity=identity) @@ -98,14 +101,13 @@ def register(): # Body content content = request.get_json() + content = snake_case_keys(content) + # Get a reference to the notification service service = client.notify.services(service_sid) # Create the binding - binding = service.bindings.create( - identity=content["identity"], - binding_type=content["BindingType"], - address=content["Address"]) + binding = service.bindings.create(**content) print(binding) @@ -126,12 +128,13 @@ def send_notification(): service = client.notify.services(service_sid) - # Create a notification for a given identity - identity = request.form.get('identity') - notification = service.notifications.create( - identity=identity, - body='Hello world!' - ) + # Get the request json or form data + content = request.get_json() if request.get_json() else request.form + + content = snake_case_keys(content) + + # Create a notification with the given form data + notification = service.notifications.create(**content) return jsonify(message="Notification created!") diff --git a/requirements.txt b/requirements.txt index be95c5b..522db54 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ Flask==0.10.1 fake-factory==0.5.3 twilio==6.2.0a1 python-dotenv==0.6.0 +inflection==0.3.1 From c01394d11706404f8c15778aa7e9573d7c04e3b0 Mon Sep 17 00:00:00 2001 From: idelgado Date: Fri, 16 Jun 2017 18:36:28 -0400 Subject: [PATCH 2/3] Revise comment --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index d99e757..e1c494d 100644 --- a/app.py +++ b/app.py @@ -12,7 +12,7 @@ from dotenv import load_dotenv, find_dotenv from os.path import join, dirname from inflection import underscore -# Convert keys to snake_case to conform with the python api definition contract +# Convert keys to snake_case to conform with the twilio-python api definition contract def snake_case_keys(somedict): return dict(map(lambda (key, value): (underscore(key), value), somedict.items())) From 30db56a2e982a15ac0f1c386d612dc04f67bf4da Mon Sep 17 00:00:00 2001 From: idelgado Date: Tue, 20 Jun 2017 15:49:50 -0400 Subject: [PATCH 3/3] Add Hello World to body --- static/notify/notify.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/static/notify/notify.js b/static/notify/notify.js index 7e7eb3a..2829b02 100644 --- a/static/notify/notify.js +++ b/static/notify/notify.js @@ -2,11 +2,12 @@ $(function() { $('#sendNotificationButton').on('click', function() { $.post('/send-notification', { - identity: $('#identityInput').val() + identity: $('#identityInput').val(), + body: "Hello, World!" }, function(response) { $('#identityInput').val(''); $('#message').html(response.message); console.log(response); }); }); -}); \ No newline at end of file +});