def wrapper(event, _context):
     try:
         todo_id = event["pathParameters"]["id"]
         user_id = event["requestContext"]["identity"]["cognitoIdentityId"]
     except KeyError:
         return bad_request("User id and todo id are required")
     return func(user_id, todo_id, event)
Example #2
0
def validate_request(body: dict, required_fields: dict):
    """Validate that a request has all required data"""
    for field_key, field_name in required_fields.items():
        if not body.get(field_key):
            return bad_request(
                {"errors": {
                    field_key: f"{field_name} is required."
                }})
    return None
 def wrapper(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except pynamodb.exceptions.DoesNotExist as err:
         print(err)
         return not_found()
     except pydantic.ValidationError as err:
         return validation_error(err.json())
     except MissingValue as err:
         print(err)
         return validation_error(ujson.dumps(str(err)))
     except MissingID as err:
         print(err)
         return bad_request("ID is required in path")
     except Exception as err:
         print(err)
         return something_has_gone_wrong()
 def wrapper(event, _context):
     data = ujson.loads(event["body"])
     for field_key, field_name in required_fields.items():
         if not data.get(field_key):
             return bad_request(
                 {"errors": {
                     field_key: f"{field_name} is required."
                 }})
     kwargs = {}
     if with_identity:
         kwargs["identity_id"] = event["requestContext"]["identity"][
             "cognitoIdentityId"]
     if with_path_id:
         try:
             kwargs["path_id"] = event["pathParameters"]["id"]
         except KeyError:
             kwargs["path_id"] = None
     return func(data, **kwargs)
 def wrapper(event, _context):
     try:
         return func(id_=event["pathParameters"]["id"])
     except KeyError:
         return bad_request("ID is required in URL.")