"""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 oto import response from oto.adaptors.flask import flaskify from promo_player import config from promo_player.api import app from promo_player.logic import promo_player @app.route(config.HEALTH_CHECK, methods=['GET']) def health(): """Check the health of the application.""" return jsonify({'status': 'ok'}) @app.route('/', methods=['GET']) def get_promo_player_for_frontend(code): """Get the data required to render a promo player. Args: code (str): the unique code of the promo player. Returns: flask.Response: containing the data. """ correlation_id = g.correlation_id return flaskify(promo_player.get_for_frontend(code, correlation_id)) @app.route('//track//manifest', methods=['GET']) def get_track_manifest_url(code, track_id): """Get the HLS manifest URL for a promo player's track. Args: code (str): the unique code of the promo player. track_id (int): the track ID. Returns: flask.Response: containing the HLS manifest URL. """ referrer = request.referrer or None return flaskify( promo_player.get_track_manifest_url(code, int(track_id), referrer)) @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))