Python SQL with NoSQL

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsPython SQL with NoSQL
# script

# for each record in the MySQL db service, query the MongoDB service to get the Twitter handle for that contact

import httplib;
import json;

content = event['response']['content'];
host = event['request']['headers']['host'][0];
headers = {
connection = httplib.HTTPConnection(host);

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
        connection.request('GET', '/api/v2/mongodb/_table/contact?'+filter, '', headers);
        response = connection.getresponse();
        result = json.loads(;
        # 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');
        records[i] = record;

event['response']['content']['resource'] = records;

# set this flag if you change the response content
# you can also set event['response']['status_code'] and event['response']['content_type']
event['response']['content_changed'] = True;