Python SQL with NoSQL

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsPython SQL with NoSQL
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<pre>
+
'''''NOTE: DreamFactory Python scripting requires 'bunch' package to be installed on server. ''''' <code>sudo pip install bunch</code>
# script db._table.contact.get.post_process
+
  
# for each record in the MySQL db service, query the MongoDB service to get the Twitter handle for that contact
+
This script runs after a GET on the service db at db/_table/contact.  For each record in the response, it queries MongoDB to get another field, the Twitter handle for the contact. It adds that to the response as a field named 'from_mongo_twitter'. For this change to take effect, you have to enable modification of response in the Admin Console script editor. Check the box 'Allow script to modify request (pre-process) or response (post-process)'.
  
import httplib;
+
<source lang=python>
import json;
+
content = event.response.content;
  
content = event['response']['content'];
+
if content.resource != "":
host = event['request']['headers']['host'][0];
+
     records = content.resource;
headers = {
+
    'x-dreamfactory-api-key':platform['session']['api_key'],
+
    'x-dreamfactory-session-token':platform['session']['session_token']
+
    };
+
   
+
connection = httplib.HTTPConnection(host);
+
 
+
if content['resource'] != "":
+
     records = content['resource'];
+
 
     for (i, record) in enumerate(records):
 
     for (i, record) in enumerate(records):
 
         # filter by email
 
         # filter by email
         filter = "filter=" + "email=" + record['email'];
+
         filter = "filter=" + "email=" + record.email;
       
+
 
         # get matching record from MongoDB service
 
         # get matching record from MongoDB service
         connection.request('GET', '/api/v2/mongodb/_table/contact?'+filter, '', headers);
+
         result = platform.api.get('/mongodb/_table/contact?'+filter);
         response = connection.getresponse();
+
         # convert json string -> dict -> bunch
         result = json.loads(response.read());
+
         data = bunchify(json.loads(result.read()));
 
          
 
          
 
         # from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be  
 
         # from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be  
         record['from_mongo_twitter'] = result['resource'][0]['twitter'].encode('utf-8');
+
         record.from_mongo_twitter = data.resource[0].twitter.encode('utf-8');
 
         records[i] = record;
 
         records[i] = record;
  
event['response']['content']['resource'] = records;
 
  
# set this flag if you change the response content
+
event.response.content = records;
# you can also set event['response']['status_code'] and event['response']['content_type']
+
</source>
event['response']['content_changed'] = True;
+
</pre>
+

Latest revision as of 20:52, 2 August 2016

NOTE: DreamFactory Python scripting requires 'bunch' package to be installed on server. sudo pip install bunch

This script runs after a GET on the service db at db/_table/contact. For each record in the response, it queries MongoDB to get another field, the Twitter handle for the contact. It adds that to the response as a field named 'from_mongo_twitter'. For this change to take effect, you have to enable modification of response in the Admin Console script editor. Check the box 'Allow script to modify request (pre-process) or response (post-process)'.

content = event.response.content;
 
if content.resource != "":
    records = content.resource;
    for (i, record) in enumerate(records):
        # filter by email
        filter = "filter=" + "email=" + record.email;
 
        # get matching record from MongoDB service
        result = platform.api.get('/mongodb/_table/contact?'+filter);
        # convert json string -> dict -> bunch
        data = bunchify(json.loads(result.read()));
 
        # from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be 
        record.from_mongo_twitter = data.resource[0].twitter.encode('utf-8');
        records[i] = record;
 
 
event.response.content = records;