lambda-decorators
lambda-decorators copied to clipboard
Pass event and context variables to after and on_exception decorators
It doesn't cost anything and would enable a wider range of use cases. For example:
- To determine whether the response body may be gzip-encoded in an
aftercallback, theeventobject needs to be inspected (to get the request header "Accept-Encoding") - To pass state from the
beforeto theaftercallback, the context variable could be used; e.g. to measure the time it took to process the request.
There's an initial proof-of-concept in https://github.com/jonaswitt/lambda-decorators/commit/1ed4697d1ec30f99b33c344d646fc33b269254f7 that works well for me.
For backwards compatibility, at least with the included decorators, it tries to figure out how many arguments the decorated function takes. I believe this could be extended to LambdaDecorator subclasses in order to avoid breaking existing user's code.
It uses getargspec() which works in Python 2 and 3 but is deprecated in Python 3. I haven't found a similar function that would work in both Python 2 and 3.
What do you think about this approach? I'd be happy to turn this into a PR (w/ updated documentation) if you agree with the general direction.
sounds good to me, esp bc you thought of backwards compatibility 👍