""" 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: sony_metadata.response for more details. """ from flask import jsonify from flask import request from sony_metadata import config from sony_metadata import response from sony_metadata.api import app from sony_metadata.logic import label @app.route(config.HEALTH_CHECK) def health(): """Check the health of the application.""" return jsonify({'status': 'ok'}) @app.route('/label_fields/subaccount/', methods=['GET']) def label_fields_subaccount(subaccount_id): """Get sony label fields associated with . Args: subaccount_id (str): the id of the subaccount (must be convertible to int). Returns: Response: Flask response. """ try: subaccount_id = int(subaccount_id) except ValueError: return response.flaskify(response.create_error_response( code=response.ERROR_CODE_VALIDATION_ERROR, message='subaccount_id is not an int')) return response.flaskify(label.get_subaccount_labels(subaccount_id)) @app.route('/label_fields/vendor/', methods=['GET']) def label_fields_vendor(vendor_id): """Get sony label fields associated with . Args: vendor_id (str): the id of the vendor (must be convertible to int). Returns: Response: Flask response. """ try: vendor_id = int(vendor_id) except ValueError: return response.flaskify(response.create_error_response( code=response.ERROR_CODE_VALIDATION_ERROR, message='vendor_id is not an int')) return response.flaskify(label.get_vendor_labels(vendor_id)) @app.route('/label_fields/vendor/', methods=['PUT']) def cud_label_fields_vendor(vendor_id): """Create, update or delete metadata fields associated with . Args: vendor_id (str): the id of the vendor (must be convertible to int). Returns: Response: JSON. """ try: vendor_id = int(vendor_id) except ValueError: return response.flaskify(response.create_error_response( code=response.ERROR_CODE_VALIDATION_ERROR, message='vendor_id is not an int')) metadata = request.get_json() return response.flaskify(label.add_update_vendor_meta(vendor_id, metadata)) @app.route( '/label_fields/vendor//subaccount/', methods=['PUT']) def cud_label_fields_subaccount(vendor_id, subaccount_id): """Create or update metadata fields associated with . Args: vendor_id (int): the id of the vendor. subaccount_id (int): the id of the subaccount. Returns: Response: Flask response. """ return response.flaskify( label.add_update_vendor_meta( vendor_id, request.get_json(), subaccount_id))