"""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: images.response for more details. """ from flask import g from flask import request from images import config from images import response from images.api import app from images.constants import workstation as workstation_constants from images.logic import hello from images.logic import image from images.logic import image_location, image_status from images.validation import access from images.validation import json_schema from images.validation.schema import json_validators @app.route(config.HEALTH_CHECK, methods=['GET']) def health(): """Check the health of the application.""" return response.flaskify(hello.health_check()) @app.errorhandler(500) def exception_handler(error): """Default handler when uncaught exception is raised. 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 response.flaskify(response.create_fatal_response(message)) @app.route('/image', methods=['POST']) def post_image(): """Post an image.""" validation_response = json_schema.validate( data=request.get_json(), validator=json_validators.POST_IMAGE_VALIDATOR) if not validation_response: return response.flaskify(validation_response) php_session_cookie_value = request.cookies.get( workstation_constants.PHP_SESSION_COOKIE_KEY) if php_session_cookie_value is None: return response.flaskify( response.create_fatal_response('No session cookie found.')) filename = request.get_json().get('filename') post_image_response = image.post_image( filename, php_session_cookie_value) return response.flaskify(post_image_response) @app.route('/image//status', methods=['GET']) def get_image_status(import_asset_id): """Get an image's status by way of import_asset information. Gets the status of a given import asset along with relevant details. Args: import_asset_id (int): import_asset_id Returns: flask.Response: Status information for the image asset. """ return response.flaskify( image_status.get_import_asset_for_image(import_asset_id)) @app.route('/image//location', methods=['GET']) def get_image_location(upc): """Get an image's location by UPC. Args: upc (int): The image upc. Returns: flask.Response: Status information for the image asset. """ image_path = image_location.get_image_location(upc) if not image_path: return response.flaskify(image_path) validation = access.verify_grass_access( request, required=False, vendor=image_path.message.get('account_info').get('vendor_id'), subaccount=image_path.message.get('account_info').get('subaccount_id')) if not validation: return response.flaskify(validation) image_path.message = {'path': image_path.message.get('path')} return response.flaskify(image_path)