Python Push notification workflow rule example

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsPython Push notification workflow rule example
Line 2: Line 2:
 
# POST /api/v2/db/_table/todo triggers script db._table.todo.post.post_process
 
# POST /api/v2/db/_table/todo triggers script db._table.todo.post.post_process
 
# This script runs AFTER records are written to the db.
 
# This script runs AFTER records are written to the db.
 
+
 
+
payload = event.request.payload;
import httplib;
+
 
+
if payload.resource != "":
payload = event['request']['payload'];
+
     for record in payload.resource:
host = event['request']['headers']['host'][0];
+
         msg = '{"Message":"A new Todo named '+record.name+' was just created!", "Subject":"New Todo Created"}';
headers = {
+
    'x-dreamfactory-api-key':platform['session']['api_key'],
+
    'x-dreamfactory-session-token':platform['session']['session_token']
+
    };
+
   
+
connection = httplib.HTTPConnection(host);
+
 
+
if payload['resource'] != "":
+
     for record in payload['resource']:
+
         msg = '{"Message":"A new Todo named '+record['name']+' was just created!", "Subject":"New Todo Created"}';
+
       
+
 
         # service name is 'push', push to SNS topic by name
 
         # service name is 'push', push to SNS topic by name
         connection.request('POST', '/api/v2/push/topic/arn:aws:sns:us-east-1:xxxxxxxxxx:new_todo', msg, headers);
+
         result = platform.api.post('/push/topic/arn:aws:sns:us-east-1:xxxxxxx:new_todo', msg);
        response = connection.getresponse();
+
       
+
 
         # output result to storage/logs/dreamfactory.log
 
         # output result to storage/logs/dreamfactory.log
         print response.read();
+
         print result.read();
 
</source>
 
</source>

Revision as of 05:50, 30 May 2016

# POST /api/v2/db/_table/todo triggers script db._table.todo.post.post_process
# This script runs AFTER records are written to the db.
 
payload = event.request.payload;
 
if payload.resource != "":
    for record in payload.resource:
        msg = '{"Message":"A new Todo named '+record.name+' was just created!", "Subject":"New Todo Created"}';
 
        # service name is 'push', push to SNS topic by name
        result = platform.api.post('/push/topic/arn:aws:sns:us-east-1:xxxxxxx:new_todo', msg);
 
        # output result to storage/logs/dreamfactory.log
        print result.read();