# Path: chaosmonkey/api/api_errors.py
# class APIError(Exception):
#     """
#     Represents an API Error
# 
#     :param message: message to be returned to the user
#     :param status_code: response status code (defaults to 400)
#     :param payload: custom payload to give extra info in the response
# 
#     Example:
#             >>> raise APIError("Error detected", 500, {"extra": "extra_info"})
# 
#     """
# 
#     def __init__(self, message, status_code=None, payload=None):
#         Exception.__init__(self)
#         self.message = message
#         self.status_code = status_code or 400
#         self.payload = payload
# 
#     def to_dict(self):
#         """Convert exception to dict"""
#         dict_ = dict(self.payload or ())
#         dict_['msg'] = self.message
#         return dict_
# 
#     def __str__(self):
#         return '[%d] %s' % (self.status_code, self.message)


from chaosmonkey.api.api_errors import APIError
from jsonschema import validate, ValidationError


def validate_payload(request, schema):
    """
    validates a request payload against a json schema

    :param request: request received with valid json body
    :param schema:  schema to validate the request payload

    :return: True
    :raises: :meth:`chaosmonkey.api.api_errors`
    """
    try:
        json = request.get_json()
        validate(json, schema)
    except ValidationError as e:
        raise APIError("invalid payload %s" % e.message)
    except Exception:

# Path: chaosmonkey/api/api_errors.py
# class APIError(Exception):
#     """
#     Represents an API Error
# 
#     :param message: message to be returned to the user
#     :param status_code: response status code (defaults to 400)
#     :param payload: custom payload to give extra info in the response
# 
#     Example:
#             >>> raise APIError("Error detected", 500, {"extra": "extra_info"})
# 
#     """
# 
#     def __init__(self, message, status_code=None, payload=None):
#         Exception.__init__(self)
#         self.message = message
#         self.status_code = status_code or 400
#         self.payload = payload
# 
#     def to_dict(self):
#         """Convert exception to dict"""
#         dict_ = dict(self.payload or ())
#         dict_['msg'] = self.message
#         return dict_
# 
#     def __str__(self):
#         return '[%d] %s' % (self.status_code, self.message)


# Path: chaosmonkey/api/request_validator.py
from chaosmonkey.api.api_errors import APIError
from jsonschema import validate, ValidationError



def validate_payload(request, schema):
    """
    validates a request payload against a json schema

    :param request: request received with valid json body
    :param schema:  schema to validate the request payload

    :return: True
    :raises: :meth:`chaosmonkey.api.api_errors`
    """
    try:
        json = request.get_json()
        validate(json, schema)
    except ValidationError as e:
        raise APIError("invalid payload %s" % e.message)
    except Exception:

        raise APIError("payload must be a valid json")