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

Pass event and context variables to after and on_exception decorators

Open jonaswitt opened this issue 6 years ago • 1 comments

It doesn't cost anything and would enable a wider range of use cases. For example:

  1. To determine whether the response body may be gzip-encoded in an after callback, the event object needs to be inspected (to get the request header "Accept-Encoding")
  2. To pass state from the before to the after callback, 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.

jonaswitt avatar Dec 19 '18 19:12 jonaswitt

sounds good to me, esp bc you thought of backwards compatibility 👍

dschep avatar Nov 05 '19 16:11 dschep