lambda-decorators icon indicating copy to clipboard operation
lambda-decorators copied to clipboard

pass parameters to decorators

Open ed-sparkes opened this issue 6 years ago • 0 comments

Hi is there a way to define a decorator with paramaters like the CORS one.

Many thanks for help and thoughts, not sure if i am just not understanding the syntax to use or its not possible,

I have two use cases.

  1. On exception be able to define a friendly message per handler for error responses
@on_exception
def handle_errors(exception,friendlyMesssage="Something went wrong"):
    print(exception)
    return {'statusCode': 500, 'body':friendlyMesssage}

@handle_errors(friendlyMesssage="Failed to get something")
def getSomething(event, context):
   #do something
  1. On before - i am writing a middleware to implenent RBAC (role based access control) and want o provide the list of permissions required to call the function
@before
def rbac(event, context,permissions=[]):
    print(f'required permissions {" ".join(str(x) for x in permissions)}')
    # actually do the validation

@rbac(permissions=["get:dogs","put:badgers"])
def getSomething(event, context):
   #do something

ed-sparkes avatar Dec 03 '19 16:12 ed-sparkes