Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(event-handler): new resolvers to fix current_event typing #978

Merged
merged 21 commits into from
Feb 3, 2022

Conversation

michaelbrewer
Copy link
Contributor

@michaelbrewer michaelbrewer commented Jan 26, 2022

Issue #, if available:

closes #761

Description of changes:

Add new specialized resolvers APIGatewayRestResolver, APIGatewayHttpResolver and ALBResolver

Example of an Application Load Balancer handler (ALBResolver) where current_event can only be an ALBEvent

from aws_lambda_powertools.event_handler import ALBResolver

app = ALBResolver()

@app.get("/lambda")
def foo():
    # Now you can access ALBEvent specific attributes
    assert app.current_event.request_context.elb_target_group_arn is not None
    return Response(200, content_types.TEXT_HTML, "foo")

Example of an REST API handler (APIGatewayRestResolver) where current_event can only be an APIGatewayProxyEvent

from aws_lambda_powertools.event_handler import APIGatewayRestResolver

app = APIGatewayRestResolver()

@app.get("/my/path")
def get_lambda() -> Response:
    # Now you can access APIGatewayProxyEvent specific attributes
    assert app.current_event.request_context.domain_name == "id.execute-api.us-east-1.amazonaws.com"
    return Response(200, content_types.APPLICATION_JSON, json.dumps({"foo": "value"}))

Example of an Http API handler (APIGatewayHttpResolver) where current_event can only be an APIGatewayProxyEventV2

from aws_lambda_powertools.event_handler import APIGatewayHttpResolver

app = APIGatewayHttpResolver()

@app.post("/my/path")
def my_path() -> Response:
    # Now you can access APIGatewayProxyEventV2 specific attributes
    assert app.current_event.cookies[0] == "cookie1"
    return Response(200, content_types.TEXT_PLAIN, "Foo")

Works with VSCode and PyCharm:

Screen Shot 2022-01-25 at 11 28 36 PM

Checklist

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…ties

Add new specialized resolvers APIGatewayProxyEventResolver, APIGatewayProxyEventV2Resolver and ALBEventResolver

closes aws-powertools#761
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jan 26, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jan 26, 2022

Codecov Report

Merging #978 (cda0d7c) into develop (d2d6e34) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #978   +/-   ##
========================================
  Coverage    99.96%   99.96%           
========================================
  Files          119      119           
  Lines         5335     5347   +12     
  Branches       607      610    +3     
========================================
+ Hits          5333     5345   +12     
  Partials         2        2           
Impacted Files Coverage Δ
aws_lambda_powertools/event_handler/api_gateway.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d2d6e34...cda0d7c. Read the comment docs.

@michaelbrewer michaelbrewer changed the title fix(event-handler): API Gateway current_event doesn't show all properties fix(event-handler): current_event doesn't show all properties Jan 26, 2022
@github-actions github-actions bot added the bug Something isn't working label Jan 26, 2022
@heitorlessa heitorlessa changed the title fix(event-handler): current_event doesn't show all properties fix(event-handler): introduce specialized event resolvers to fix current_event typing Jan 26, 2022
@heitorlessa heitorlessa added the p1 label Jan 31, 2022
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to make names shorter, explicit, and remove the Event redundant part. Also to remove duplicate docstrings as it's inherited from super class (despite the proxy_type showing in VSCode) to prevent what happened to Parameters utilities docstrings being accidentally left behind too

aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
Michael Brewer and others added 7 commits February 1, 2022 10:50
Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
@pull-request-size pull-request-size bot removed the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 2, 2022
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 2, 2022
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 2, 2022
…a-powertools-python into fix-event-handler

* 'fix-event-handler' of https://github.com/gyft/aws-lambda-powertools-python:
  docs(event_handler): Rename ApiGatewayResolver to APIGatewayRestResolver
  chore: correct docs
  chore: correct docs
  chore: use isinstance over type
  docs: add better BDD coments
  fix(logger): test generates logfile
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
@pull-request-size pull-request-size bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 3, 2022
@heitorlessa heitorlessa changed the title fix(event-handler): introduce specialized event resolvers to fix current_event typing feat(event-handler): APIGatewayRestResolver, APIGatewayHttpResolver, ALBResolver Feb 3, 2022
@github-actions github-actions bot added the feature New feature or functionality label Feb 3, 2022
@heitorlessa heitorlessa changed the title feat(event-handler): APIGatewayRestResolver, APIGatewayHttpResolver, ALBResolver feat(event-handler): new resolvers to fix current_event typing Feb 3, 2022
@heitorlessa heitorlessa merged commit 168ec48 into aws-powertools:develop Feb 3, 2022
@heitorlessa heitorlessa deleted the fix-event-handler branch February 3, 2022 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation feature New feature or functionality p1 size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Event Handler: API Gateway current_event doesn't show all properties
3 participants