<commit_before># Copyright 2016, 2017 <NAME>. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from threading import Lock
from flask import Flask, Response, jsonify, request, json
from orders import *

# Create Flask application
app = Flask(__name__)

# Status Codes
HTTP_200_OK = 200
HTTP_201_CREATED = 201
HTTP_204_NO_CONTENT = 204
HTTP_400_BAD_REQUEST = 400
HTTP_404_NOT_FOUND = 404
HTTP_409_CONFLICT = 409

# Lock for thread-safe counter increment
lock = Lock()

######################################################################
# GET INDEX
######################################################################
@app.route('/')
def index():
    orders_url = request.base_url + "orders"
    return jsonify(name='Orders Demo REST API Service',
                   version='1.0',
                   url=orders_url
                   ), HTTP_200_OK

######################################################################
# LIST ALL Orders
######################################################################
@app.route('/orders', methods=['GET'])
def list_orders():
	results = orders.values()
	amount_paid = request.args.get('amount_paid')
	customer_name = request.args.get('customer_name')
	if amount_paid:
		results = []
		for key, value in orders.iteritems():
			if str(value['amount_paid']) == str(amount_paid):
				results.append(orders[key])
	elif customer_name:
		results = []
		for key, value in orders.iteritems():
			if str(value['customer_name']).lower() == str(customer_name).lower():
				results.append(orders[key])
				
	return reply(results, HTTP_200_OK)

######################################################################
# RETRIEVE A Order
######################################################################
@app.route('/orders/<int:id>', methods=['GET'])
def get_orders(id):
    if orders.has_key(id):
        message = orders[id]
        rc = HTTP_200_OK
    else:
        message = { 'error' : 'Order with id: %s was not found' % str(id) }
        rc = HTTP_404_NOT_FOUND

    return reply(message, rc)

######################################################################
# ADD A NEW Order
######################################################################
@app.route('/orders', methods=['POST'])
def create_orders():
    payload = request.get_json()
    if is_valid(payload):
        id = next_index()
        orders[id] = {'id': id, 'customer_name': payload['customer_name'], 'amount_paid': payload['amount_paid']}
        message = orders[id]
        rc = HTTP_201_CREATED
    else:
        message = { 'error' : 'Data is not valid' }
        rc = HTTP_400_BAD_REQUEST

    return reply(message, rc)

######################################################################
# UPDATE AN EXISTING Order
######################################################################
@app.route('/orders/<int:id>', methods=['PUT'])
def update_orders(id):
    if orders.has_key(id):
        payload = request.get_json()
        if is_valid(payload):
            orders[id] = {'id': id, 'customer_name': payload['customer_name'], 'amount_paid': payload['amount_paid']}
            message = orders[id]
            rc = HTTP_200_OK
        else:
            message = { 'error' : 'Order data was not valid' }
            rc = HTTP_400_BAD_REQUEST
    else:
        message = { 'error' : 'Order %s was not found' % id }
        rc = HTTP_404_NOT_FOUND

    return reply(message, rc)

######################################################################
# DELETE A Order
######################################################################
@app.route('/orders/<int:id>', methods=['DELETE'])
def delete_orders(id):
    del orders[id];
    return '', HTTP_204_NO_CONTENT

######################################################################
#  U T I L I T Y   F U N C T I O N S
######################################################################
def next_index():
    global current_order_id
    with lock:
        current_order_id += 1
    return current_order_id

def reply(message, rc):
    response = Response(json.dumps(message))
    response.headers['Content-Type'] = 'application/json'
    response.status_code = rc
    return response

def is_valid(data):
    valid = False<commit_msg>adding route to duplicate orders<commit_after># Copyright 2016, 2017 <NAME>. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from threading import Lock
from flask import Flask, Response, jsonify, request, json
from orders import *

# Create Flask application
app = Flask(__name__)

# Status Codes
HTTP_200_OK = 200
HTTP_201_CREATED = 201
HTTP_204_NO_CONTENT = 204
HTTP_400_BAD_REQUEST = 400
HTTP_404_NOT_FOUND = 404
HTTP_409_CONFLICT = 409

# Lock for thread-safe counter increment
lock = Lock()

######################################################################
# GET INDEX
######################################################################
@app.route('/')
def index():
    orders_url = request.base_url + "orders"
    return jsonify(name='Orders Demo REST API Service',
                   version='1.0',
                   url=orders_url
                   ), HTTP_200_OK

######################################################################
# LIST ALL Orders
######################################################################
@app.route('/orders', methods=['GET'])
def list_orders():
	results = orders.values()
	amount_paid = request.args.get('amount_paid')
	customer_name = request.args.get('customer_name')
	if amount_paid:
		results = []
		for key, value in orders.iteritems():
			if str(value['amount_paid']) == str(amount_paid):
				results.append(orders[key])
	elif customer_name:
		results = []
		for key, value in orders.iteritems():
			if str(value['customer_name']).lower() == str(customer_name).lower():
				results.append(orders[key])
				
	return reply(results, HTTP_200_OK)

######################################################################
# RETRIEVE A Order
######################################################################
@app.route('/orders/<int:id>', methods=['GET'])
def get_orders(id):
    if orders.has_key(id):
        message = orders[id]
        rc = HTTP_200_OK
    else:
        message = { 'error' : 'Order with id: %s was not found' % str(id) }
        rc = HTTP_404_NOT_FOUND

    return reply(message, rc)

######################################################################
# ADD A NEW Order
######################################################################
@app.route('/orders', methods=['POST'])
def create_orders():
    payload = request.get_json()
    if is_valid(payload):
        id = next_index()
        orders[id] = {'id': id, 'customer_name': payload['customer_name'], 'amount_paid': payload['amount_paid']}
        message = orders[id]
        rc = HTTP_201_CREATED
    else:
        message = { 'error' : 'Data is not valid' }
        rc = HTTP_400_BAD_REQUEST

    return reply(message, rc)

######################################################################
# UPDATE AN EXISTING Order
######################################################################
@app.route('/orders/<int:id>', methods=['PUT'])
def update_orders(id):
    if orders.has_key(id):
        payload = request.get_json()
        if is_valid(payload):
            orders[id] = {'id': id, 'customer_name': payload['customer_name'], 'amount_paid': payload['amount_paid']}
            message = orders[id]
            rc = HTTP_200_OK
        else:
            message = { 'error' : 'Order data was not valid' }
            rc = HTTP_400_BAD_REQUEST
    else:
        message = { 'error' : 'Order %s was not found' % id }
        rc = HTTP_404_NOT_FOUND

    return reply(message, rc)

######################################################################
# DELETE A Order
######################################################################
@app.route('/orders/<int:id>', methods=['DELETE'])
def delete_orders(id):
    del orders[id];
    return '', HTTP_204_NO_CONTENT

######################################################################      
# DUPLICATE A Order
######################################################################
@app.route('/orders/<int:id>/duplicate', methods=['GET'])
def duplicate_order(id):
    if orders.has_key(id):
        message = orders[id]

        new_id = next_index()
        orders[new_id] = {'id': new_id, 'customer_name': message['customer_name'], 'amount_paid': message['amount_paid']}
        message = orders[new_id]
        rc = HTTP_201_CREATED
    else:
        message = { 'error' : 'Order with id: %s was not found' % str(id) }
        rc = HTTP_404_NOT_FOUND

    return reply(message, rc)

######################################################################
#  U T I L I T Y   F U N C T I O N S
######################################################################
def next_index():
    global current_order_id
    with lock:
        current_order_id += 1
    return current_order_id

def reply(message, rc):
    response = Response(json.dumps(message))
    response.headers['Content-Type'] = 'application/json'
    response.status_code = rc
    return response

def is_valid(data):
    valid = False
{'fraction_non_alphanumeric': 0.30180533751962324, 'fraction_numerical': 0.016483516483516484, 'mean_word_length': 4.333856619570906, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4907263', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2634, 'n_words': 749}