"""Application Handlers. Requests are redirected to handlers, which are responsible for getting information from the URL and passing it down to the logic layer. The way each layer talks to each other is through Response objects which defines the type status of the data and the data itself. Please note: the Orchard uses the term handlers over views as convention for clarity See: oto.response for more details. """ import boto3 from flask import g from flask import json from flask import jsonify from owsresponse import response from owsresponse.adaptors.flask import flaskify from ddex_lambda_proxy import config from ddex_lambda_proxy.api import app from ddex_lambda_proxy.models import ows_product @app.route(config.HEALTH_CHECK, methods=['GET']) def health(): """Check the health of the application.""" return jsonify({'status': 'ok'}) @app.errorhandler(500) def exception_handler(error): """Handle error when uncaught exception is raised. Default exception handler. Note: Exception will also be sent to Sentry if config.SENTRY is set. Returns: flask.Response: A 500 response with JSON 'code' & 'message' payload. """ message = ( 'The server encountered an internal error ' 'and was unable to complete your request.') g.log.exception(error) return flaskify(response.create_fatal_response(message)) @app.route('/ddex-rdx/upc/', methods=['GET']) def get_rdx_ddex(upc): """Get the RDx specific DDEX metadata given the UPC for a product.""" product_response = ows_product.get_product_for_upc(upc) product_id = int(product_response.message.get('product_id')) client = boto3.client('lambda', region_name=config.AWS_REGION) response = client.invoke( FunctionName=config.DDEX_RDR_LAMBDA_NAME, Payload=json.dumps({'product_id': str(product_id)}) ) return response.get('Payload').read()