def vary_charge(land_charge_id, version_id):
    current_app.logger.info("Endpoint called")
    payload = request.get_json()

    if not is_valid_charge_id(land_charge_id):
        raise ApplicationError("Invalid Land Charge ID", 'E422', 422)

    if not version_id.isdigit():
        raise ApplicationError("Invalid Version ID", 'E422', 422)

    errors = add_vary_handler.add_vary_validate(payload, 'v1_0')

    if errors:
        error_message = {"error": "Charge is invalid", "details": errors}
        current_app.logger.error("Errors found: {}".format(error_message))
        raise ApplicationError(error_message, 'E100', 400)

    current_app.logger.info("version_id is {}".format(version_id))
    status_code, result = AddResponses.add_vary_cancel_response(
        land_charge_id, version_id)

    return (json.dumps(result, sort_keys=True,
                       separators=(',', ':')), status_code, {
                           'Content-Type': 'application/json'
                       })
Exemple #2
0
def get_response_data(charge_id):
    current_app.logger.info(
        "retrieving json response for {}".format(charge_id))
    path = os.path.split(os.path.realpath(__file__))
    app_dir = os.path.split(path[0])[0]
    file_path = os.path.join(app_dir, "constants")
    if not is_valid_charge_id(charge_id):
        # CLL-3 is invalid charge ID response file
        file_name = "CLL-3"
    else:
        file_name = charge_id

    try:
        with open(os.path.join(file_path,
                               "{}.json".format(file_name))) as json_response:
            return get_status_code(file_name), json.load(json_response)
    except FileNotFoundError as ex:
        file_name = "LLC-1"
        try:
            current_app.logger.info(
                "retrieving json response for default charge {}".format(
                    file_name))
            with open(os.path.join(
                    file_path, "{}.json".format(file_name))) as json_response:
                return get_status_code(file_name), json.load(json_response)
        except Exception as ex:
            error_message = ex.errno
            raise ApplicationError(error_message, "E199", 500)
def cancel_charge(land_charge_id, version_id):
    current_app.logger.info("Endpoint called")

    if not is_valid_charge_id(land_charge_id):
        raise ApplicationError("Invalid Land Charge ID", 'E422', 422)

    if not version_id.isdigit():
        raise ApplicationError("Invalid Version ID", 'E422', 422)

    current_app.logger.info("Sending charge to maintain-api")
    status_code, result = AddResponses.add_vary_cancel_response(
        land_charge_id, version_id)

    if status_code == 400:
        current_app.logger.error("Errors found: {}".format(result[0]))
        raise ApplicationError(result[0], 'E400', status_code)

    current_app.logger.info("Charge sent to maintain-api - Building response")

    return (json.dumps(result, sort_keys=True,
                       separators=(',', ':')), status_code, {
                           'Content-Type': 'application/json'
                       })
Exemple #4
0
def encode_base_31(charge_id):
    """Encodes the given base 10 charge_id into a base 31 string.

    Throws ApplicationError if charge_id is less than ChargeId.FLOOR or greater than ChargeId.CEILING.
    """
    if charge_id < ChargeId.FLOOR or charge_id > ChargeId.CEILING:
        raise ApplicationError(
            'The given charge id ({}) is less than the allowed floor ({}), or greater than the '
            'allowed ceiling ({})'.format(charge_id, ChargeId.FLOOR,
                                          ChargeId.CEILING), 500)

    encoded = ''
    while charge_id > 0:
        charge_id, remainder = divmod(charge_id, 31)
        encoded = ChargeId.CHARACTERS[remainder] + encoded

    return encoded
def add_charge():
    current_app.logger.info("Endpoint called")
    payload = request.get_json()
    date = datetime.datetime.now().strftime("%Y-%m-%d")

    errors = add_vary_handler.add_vary_validate(payload, 'v1_0')

    if errors:
        error_message = {"error": "Charge is invalid", "details": errors}
        current_app.logger.error("Errors found: {}".format(error_message))
        raise ApplicationError(error_message, 'E100', 400)

    result = AddResponses.add_valid_response
    status_code = 200

    result["registration-date"] = date

    current_app.logger.info("Charge sent to maintain-api - Building response")

    return (json.dumps(result, sort_keys=True,
                       separators=(',', ':')), status_code, {
                           'Content-Type': 'application/json'
                       })