# Client python microservice for processing json search results

import logging
import time

from flask import Flask, abort, jsonify, request

logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    level=logging.INFO,
)
logger = logging.getLogger(__name__)


def process(client, data):
    logger.info('Processing client ' + client)

    # put here your client processing code
    return data


app = Flask(__name__)
app.config['DEBUG'] = False


@app.route("/process", methods=['POST'])
def post_process():
    payload = request.get_json()
    if not payload or 'client' not in payload or 'data' not in payload:
        abort(400, description="Request must include 'client' and 'data' fields")
    start = time.perf_counter()
    output = process(payload['client'], payload['data'])
    elapsed_ms = int((time.perf_counter() - start) * 1000)
    logger.info('Processing took %d ms' % elapsed_ms)
    return jsonify(output)


if __name__ == "__main__":
    app.run(host='0.0.0.0')
