<commit_before>from flask import Flask, render_template, request, flash, session, redirect, url_for, jsonify, Response
from sqlalchemy.sql import func
import urllib
import logging
import json
import boto3
import sys
import os.path
from pymongo import MongoClient
import hashlib
import time
import simplejson as json

sys.path.append(os.path.abspath(__file__ + '../../..'))
import mongo_credentials
mongo_url = 'mongodb://%s:%s@ds<EMAIL>.mongolab.com:33915/project2' % (mongo_credentials.DB_USERNAME, mongo_credentials.DB_PASSWORD)
mongo = MongoClient(mongo_url)
response_queue_url = {}  # map from client_id(which is also the id of response queue) to queueUrl
response_cache = {}
sqs = boto3.client('sqs')
app = Flask(__name__)
queue_finance_name = 'finance'
queue_k12_name = 'k12'
db = mongo.get_default_database()
response_queues = db.response_queues
logging.basicConfig(filename='gateway.log',level=logging.INFO,format='%(asctime)s --- %(message)s')


def create_request_queues():
    queue_finance = sqs.create_queue(QueueName = queue_finance_name)
    queue_k12 = sqs.create_queue(QueueName = queue_k12_name)

def delete_request_queues():
    response = sqs.delete_queue(QueueUrl = 'https://queue.amazonaws.com/031864143155/' + queue_finance_name)
    response = sqs.delete_queue(QueueUrl = 'https://queue.amazonaws.com/031864143155/' + queue_k12_name)

def create_response_queue(client_id):
    logging.info("in create_response_queue")
    queue = sqs.create_queue(QueueName = client_id)
    try:
        sqs.add_permission(QueueUrl = queue['QueueUrl'], Label = 'AllOp', AWSAccountIds = ['559115960312', '308367428478'], Actions = ['*'])
    except Error as e:
        print e
    response_queues.insert_one({'client_id': client_id, 'url': queue['QueueUrl']})
    return queue['QueueUrl']

def create_request_id():
    hash = hashlib.sha1()
    hash.update(str(time.time()))
    return hash.hexdigest()

def get_response_queue_url(client_id):
    if client_id in response_queue_url:  # if in cache
        return response_queue_url[client_id]

    queue_info = response_queues.find_one({'client_id': client_id})  # if in mongo
    url = ''
    if queue_info:
        url =  queue_info['url']
    else:  # client id not registered yet
        url = create_response_queue(client_id)
        response_queue_url[client_id] = url  # add the new mapping to cache
    return url

def send_to_request_queue(qName, op, response_queue_url, body, service_url):
    request_id = create_request_id()
    queue = sqs.get_queue_url(QueueName = qName)
    response = sqs.send_message(QueueUrl = queue['QueueUrl'], MessageBody = 'boto3', MessageAttributes = {
            'op': {
                'StringValue': op,
                'DataType': 'String'
            },
            'response_queue_url': {
                'StringValue': response_queue_url,
                'DataType': 'String'
            },
            'request_id': {
                'StringValue': request_id,
                'DataType': 'String'
            },
            'body': {
                'StringValue': body,
                'DataType': 'String'
            },
            'service_url': {
                'StringValue': service_url,
                'DataType': 'String'
            }
        })
    return response, request_id

### put the finance microservice request into corresponding sqs
@app.route('/public/finance', methods = ['GET'])
def get_finance_info():
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    response, request_id = send_to_request_queue(queue_finance_name, 'GET', url, 'None', '/private/finance')
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)

@app.route('/public/finance/<studentid>', methods = ['GET'])
def get_finance_info_by_studentid(studentid):
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    response, request_id = send_to_request_queue(queue_finance_name, 'GET', url, 'None', '/private/finance/' + studentid)
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)


@app.route("/public/finance", methods = ['POST'])
def post_finance_info():
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    content = request.get_json(force = True)
    str_json = json.dumps(content)
    response, request_id = send_to_request_queue(queue_finance_name, 'POST', url, str_json, '/private/finance')
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)

@app.route("/public/finance/<studentid>", methods = ['PUT'])
def update_finance_info(studentid):
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    content = request.get_json(force = True)
    str_json = json.dumps(content)
    response, request_id = send_to_request_queue(queue_finance_name, 'POST', url, str_json, '/private/finance/' + studentid)
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)

@app.route('/public/finance', methods=['DELETE'])
def delete_finance_info():<commit_msg>add post function for finance<commit_after>from flask import Flask, render_template, request, flash, session, redirect, url_for, jsonify, Response
from sqlalchemy.sql import func
import urllib
import logging
import json
import boto3
import sys
import os.path
from pymongo import MongoClient
import hashlib
import time
import simplejson as json

sys.path.append(os.path.abspath(__file__ + '../../..'))
import mongo_credentials
mongo_url = 'mongodb://%s:%s@<EMAIL>.mongolab.com:33915/project2' % (mongo_credentials.DB_USERNAME, mongo_credentials.DB_PASSWORD)
mongo = MongoClient(mongo_url)
response_queue_url = {}  # map from client_id(which is also the id of response queue) to queueUrl
response_cache = {}
sqs = boto3.client('sqs')
app = Flask(__name__)
queue_finance_name = 'finance'
queue_k12_name = 'k12'
db = mongo.get_default_database()
response_queues = db.response_queues
logging.basicConfig(filename='gateway.log',level=logging.INFO,format='%(asctime)s --- %(message)s')


def create_request_queues():
    queue_finance = sqs.create_queue(QueueName = queue_finance_name)
    queue_k12 = sqs.create_queue(QueueName = queue_k12_name)

def delete_request_queues():
    response = sqs.delete_queue(QueueUrl = 'https://queue.amazonaws.com/031864143155/' + queue_finance_name)
    response = sqs.delete_queue(QueueUrl = 'https://queue.amazonaws.com/031864143155/' + queue_k12_name)

def create_response_queue(client_id):
    logging.info("in create_response_queue")
    queue = sqs.create_queue(QueueName = client_id)
    try:
        sqs.add_permission(QueueUrl = queue['QueueUrl'], Label = 'AllOp', AWSAccountIds = ['559115960312', '308367428478'], Actions = ['*'])
    except Error as e:
        print e
    response_queues.insert_one({'client_id': client_id, 'url': queue['QueueUrl']})
    return queue['QueueUrl']

def create_request_id():
    hash = hashlib.sha1()
    hash.update(str(time.time()))
    return hash.hexdigest()

def get_response_queue_url(client_id):
    if client_id in response_queue_url:  # if in cache
        return response_queue_url[client_id]

    queue_info = response_queues.find_one({'client_id': client_id})  # if in mongo
    url = ''
    if queue_info:
        url =  queue_info['url']
    else:  # client id not registered yet
        url = create_response_queue(client_id)
        response_queue_url[client_id] = url  # add the new mapping to cache
    return url

def send_to_request_queue(qName, op, response_queue_url, body, service_url):
    request_id = create_request_id()
    queue = sqs.get_queue_url(QueueName = qName)
    response = sqs.send_message(QueueUrl = queue['QueueUrl'], MessageBody = 'boto3', MessageAttributes = {
            'op': {
                'StringValue': op,
                'DataType': 'String'
            },
            'response_queue_url': {
                'StringValue': response_queue_url,
                'DataType': 'String'
            },
            'request_id': {
                'StringValue': request_id,
                'DataType': 'String'
            },
            'body': {
                'StringValue': body,
                'DataType': 'String'
            },
            'service_url': {
                'StringValue': service_url,
                'DataType': 'String'
            }
        })
    return response, request_id

### put the finance microservice request into corresponding sqs
@app.route('/public/finance', methods = ['GET'])
def get_finance_info():
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    response, request_id = send_to_request_queue(queue_finance_name, 'GET', url, 'None', '/private/finance')
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)

@app.route('/public/finance/<studentid>', methods = ['GET'])
def get_finance_info_by_studentid(studentid):
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    response, request_id = send_to_request_queue(queue_finance_name, 'GET', url, 'None', '/private/finance/' + studentid)
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)


@app.route("/public/finance", methods = ['POST'])
def post_finance_info():
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    content = request.get_json(force = True)
    str_json = json.dumps(content)
    response, request_id = send_to_request_queue(queue_finance_name, 'POST', url, str_json, '/private/finance')
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)

@app.route("/public/finance/<studentid>/courses", methods = ['POST'])
def post_finance_info_by_studentid_courses(studentid):
    print "/public/finance/<student_id>/courses"
    print 'studentid: ' + str(studentid)
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    content = request.get_json(force = True)
    str_json = json.dumps(content)
    response, request_id = send_to_request_queue(queue_finance_name, 'POST', url, str_json, '/private/finance/' + studentid + '/courses')
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)

@app.route("/public/finance/<studentid>", methods = ['PUT'])
def update_finance_info(studentid):
    client_id = request.headers.get('client_id')
    url = get_response_queue_url(client_id)
    content = request.get_json(force = True)
    str_json = json.dumps(content)
    response, request_id = send_to_request_queue(queue_finance_name, 'POST', url, str_json, '/private/finance/' + studentid)
    return Response('{"_status": "SUCCESS", "_success": {"message":' + json.dumps(response) + ', "request_id":"' + request_id + '", "code": 200}}', mimetype='application/json', status=200)

@app.route('/public/finance', methods=['DELETE'])
def delete_finance_info():
{'fraction_non_alphanumeric': 0.14307406782520668, 'fraction_numerical': 0.016028344862493674, 'mean_word_length': 4.140936686903729, 'pattern_counts': {'":': 45, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3372697', 'n_tokens_mistral': 3965, 'n_tokens_neox': 3814, 'n_words': 745}