"""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 oto import response from oto.adaptors.flask import flaskify from product_configuration import config from product_configuration.api import app from product_configuration.logic import product_configuration @app.route(config.HEALTH_CHECK, methods=['GET']) def health(): """Check the health of the application.""" return jsonify({'status': 'ok'}) @app.route('/distribution-format-media-format', methods=['GET']) def get_distribution_format_media_formats(): """Retrieve all distribution_format_media formats. Returns: flask.response: The paginated results of the fetch. """ media_formats = \ product_configuration.get_distribution_format_media_formats() return flaskify(media_formats) @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_DSN 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('/distribution-format-media', methods=['GET']) def get_distribution_format_media(): """Return list of distribution format media types. Returns: flask.Response: a successul response with data or an error response. """ media_types = product_configuration.get_distribution_format_media_types() return flaskify(media_types) @app.route('/distribution-formats', methods=['GET']) def get_distribution_formats(): """Return list of distribution formats. Returns: flask.Response: a successful response with data or an error response. """ distribution_formats = product_configuration.get_distribution_formats() headers = {'Cache-Control': 'max-age=3600'} return flaskify(distribution_formats, headers) @app.route('/public/distribution-formats', methods=['GET']) def get_public_distribution_formats(): """Return list of distribution formats. Returns: flask.Response: a successful response with data or an error response. """ distribution_formats = product_configuration.get_distribution_formats() headers = {'Cache-Control': 'max-age=3600'} return flaskify(distribution_formats, headers) @app.route('/supply-chains', methods=['GET']) def get_supply_chains(): """Return list of supply chain configuration schemas. Returns: flask.Response: a successful response with data. """ return flaskify( product_configuration.get_supply_chain_configuration_schemas()) @app.route( '/supply-chain//distribution-formats', methods=['GET']) def get_supply_chain_configurations(supply_chain_id): """Return a list of supply chain configurations. Returns: flask.Response: a successful response with data or an error response. """ return flaskify( product_configuration.get_supply_chain_configurations(supply_chain_id)) @app.route( '/supply-chain//distribution-format/', # noqa methods=['GET']) def get_supply_chain_configuration(supply_chain_id, distribution_format_id): """Return a single supply chain configuration. Returns: flask.Response: a successful response with data or an error response. """ configuration = product_configuration.get_supply_chain_configuration( supply_chain_id, distribution_format_id) return flaskify(configuration) @app.route('/supply-chains/defaults', methods=['GET']) def get_supply_chains_defaults(): """Return list of supply chain defaults. Returns: flask.Response: a successful response with data. """ supply_chain_defaults = \ product_configuration.get_supply_chain_defaults() return flaskify(supply_chain_defaults) @app.route( '/distribution-format/', # noqa methods=['GET']) def get_distribution_format_by_distribution_format_id(distribution_format_id): """Return a distribution format record based on distribution format id. Returns: flask.Response: a successful response with data or an error response. """ distribution_format = product_configuration. \ get_distribution_format_by_distribution_format_id( distribution_format_id) return flaskify(distribution_format)