"""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: owsresponse.response for more details. """ from flask import g, jsonify, request from owsresponse import response from owsresponse.adaptors.flask import flaskify from product_store_mapping import config from product_store_mapping.api import app from product_store_mapping.logic import product, track @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): """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("/mapping/product//store/", methods=["GET"]) def get_product_mapping_with_tracks(product_id, dms_id): """Handler for retrieving entire product mapping. Args: product_id (int): art_relations.releases.release_id dms_id (int): art_relations.customer_master_master.customer_master_master_id Returns: flask.Response """ return flaskify(product.get_product_with_tracks(int(product_id), int(dms_id))) @app.route("/mapping/product//store//product", methods=["GET"]) def get_product_mapping(product_id, store_id): """Handler for retrieving product mapping. Args: product_id (int): art_relations.releases.release_id store_id (int): art_relations.customer_master_master.customer_master_master_id Returns: flask.Response """ return flaskify(product.get_product(int(product_id), int(store_id))) @app.route("/mapping/track//store//track", methods=["GET"]) def get_track_mapping(track_unique_id, dms_id): """Handler for retrieving track mapping. Args: track_unique_id (int): art_relations.track.track.id dms_id (int): art_relations.customer_master_master.customer_master_master_id Returns: flask.Response containing the store_unique_id and custom_id """ return flaskify(track.get_track(int(track_unique_id), int(dms_id))) @app.route( "/mapping/product//store//product", methods=["DELETE"] ) def delete_product(product_id, store_id): """Handler for deleting product. Args: product_id (int): The product_id of the product we want to delete. store_id (int): The store_id of the product we want to delete. Returns: flask.Response: message containing empty list data upon successful delete, error Response message otherwise. """ return flaskify(product.delete_product(product_id, store_id)) @app.route( "/mapping/product//store//product", methods=["POST"] ) def save_product_mapping_data(product_id, store_id): """Handler to save product mapping data. Args: product_id (int): Product for which we want to add mapping. store_id (int): Store id for mapping with product. store_unique_id (int): Store unique identifier (optional in body). provider (string): Provider for this mapping (required in body). video_order_id (string): Mapping id that the store provided (required in body). Returns: flask.Response: Dict of new created mapping. """ data = request.get_json() return flaskify( product.save_product_data( product_id, store_id, data.get("provider"), data.get("video_order_id"), data.get("store_unique_id", 0), ) )