"""ledger_contract_flowthrough endpoint blueprints.""" import simplejson as json from flask import Blueprint, request from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from werkzeug.exceptions import abort from ledger.constants.error import ( ERROR_CODE_AUTHORIZATION, ERROR_INVALID_SYNTAX, ERROR_MIN_RECORDS, ) from ledger.logic import ledger_contract_flowthrough as logic ledger_contract_flowthrough_api = Blueprint('ledger_contract_flowthrough_api', __name__) @ledger_contract_flowthrough_api.route( '/ledger-contract-flowthrough/bulk', methods=['POST'] ) def bulk_create_ledger_contract_flowthrough_entries(): """POST one or more ledger entries in an atomic transaction.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=403, ) ) parsed_body = json.loads(request.data.decode(), use_decimal=True) if not parsed_body: abort(status=400, description=ERROR_MIN_RECORDS) if not isinstance(parsed_body, list): abort(status=400, description=ERROR_INVALID_SYNTAX) result = logic.handle_bulk_ledger_entries(parsed_body) return flaskify(result)