"""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. """ from flask import g from flask import jsonify from flask import request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from asset_file_details import config from asset_file_details.api import app from asset_file_details.constants import asset_format as \ asset_format_const from asset_file_details.constants import error from asset_file_details.constants import physical_locations from asset_file_details.logic import file_details as \ file_detail_logic from asset_file_details.logic import flac_file_details \ as flac_file_details_logic from asset_file_details.utils import handler_util @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( '/flac-file-details/upc//cd//track_id/', methods=['GET'] ) def get_flac_file_details(upc, cd, track_id): """Get details for the flac file for a given a upc, cd, and track id combo. Args: upc (str): UPC of the asset. cd (str): CD (volume number) of the asset. track_id (str): Track ID (track number) of the asset. Returns: flask.Response: A response with JSON containing the flac file details """ result_dict = flac_file_details_logic.get_flac_file_details( upc, cd, track_id) return flaskify(result_dict) @app.route( '/file-details/format//upc/', methods=['GET'] ) def get_file_details_by_format(file_format, upc): """Get details for the file for a given a upc. Args: file_format (str): File format. upc (str): UPC of the asset. Returns: flask.Response: A response with JSON containing the file details """ headers_response = flask_request.verify_grass_headers(request) if not headers_response: return flaskify(headers_response) ownership_response = flask_request.verify_grass_ownership( request, handler_util.check_product_ownership, upc=upc, correlation_id=g.correlation_id) if not ownership_response: return flaskify(ownership_response) if file_format not in asset_format_const.ALLOWED_AUDIO_FORMATS: return flaskify(response.create_error_response( code=error.ERROR_CODE_INVALID_FORMAT, message=error.ERROR_MESSAGE_INVALID_FORMAT )) physical_location_id = request.args.get('physical_location_id', '1') if physical_location_id is not None and physical_location_id not in physical_locations.PHYSICAL_LOCATION_IDS: return flaskify(response.create_error_response( code=error.ERROR_CODE_INVALID_PHYSICAL_LOCATION_ID, message=error.ERROR_MESSAGE_INVALID_PHYSICAL_LOCATION_ID )) result_dict = file_detail_logic.get_file_details_by_format( file_format, upc, physical_location_id) headers = {'Cache-Control': 'max-age=1800'} return flaskify(result_dict, headers)