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(data-classes): decorator to instantiate data_classes and docs updates #442

Merged
merged 12 commits into from
Jun 5, 2021

Conversation

michaelbrewer
Copy link
Contributor

@michaelbrewer michaelbrewer commented May 26, 2021

Issue #, if available:
#434
aws-powertools/powertools-lambda#19

Description of changes:

Changes:

  • feat: Create a convenient decorator to instantiate data_classes instances
  • docs: Update docs to include @event_source example
  • docs: Add missing docs for ALBEvent and format examples to work with PyCharm
  • fix: Fix _generate_hash to handle the data being an instance of DictWrapper
TypeError: Object of type APIGatewayProxyEventV2 is not JSON serializable

Example usage

from typing import Dict, Any

from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEventV2, event_source
from aws_lambda_powertools.utilities.typing import LambdaContext


@event_source(data_class=APIGatewayProxyEventV2)
def lambda_handler(event: APIGatewayProxyEventV2, context: LambdaContext) -> Dict[str, Any]:
    assert event.get_header_value("x-foo") == "Foo"

And can be used with any of the existing data classes like:

from aws_lambda_powertools.utilities.data_classes import event_source
from aws_lambda_powertools.utilities.data_classes.connect_contact_flow_event import (
    ConnectContactFlowChannel,
    ConnectContactFlowEndpointType,
    ConnectContactFlowEvent,
    ConnectContactFlowInitiationMethod,
)


@event_source(data_class=ConnectContactFlowEvent)
def lambda_handler(event: ConnectContactFlowEvent, context):
    assert event.contact_data.attributes == {"Language": "en-US"}
    assert event.contact_data.channel == ConnectContactFlowChannel.VOICE
    assert event.contact_data.customer_endpoint.endpoint_type == ConnectContactFlowEndpointType.TELEPHONE_NUMBER
    assert event.contact_data.initiation_method == ConnectContactFlowInitiationMethod.API

And this still works with the existing idempotent handler

persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")

@event_source(data_class=APIGatewayProxyEventV2)
@idempotent(persistence_store=persistence_layer)
def lambda_handler(event: APIGatewayProxyEventV2, context):
    assert isinstance(event, APIGatewayProxyEventV2)
    ...

Checklist

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

@codecov-commenter
Copy link

codecov-commenter commented May 26, 2021

Codecov Report

Merging #442 (0fc80ce) into develop (9b88322) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #442   +/-   ##
========================================
  Coverage    99.90%   99.90%           
========================================
  Files          104      105    +1     
  Lines         4193     4202    +9     
  Branches       205      205           
========================================
+ Hits          4189     4198    +9     
  Misses           1        1           
  Partials         3        3           
Impacted Files Coverage Δ
...bda_powertools/utilities/data_classes/alb_event.py 100.00% <ø> (ø)
...mbda_powertools/utilities/data_classes/__init__.py 100.00% <100.00%> (ø)
..._powertools/utilities/data_classes/event_source.py 100.00% <100.00%> (ø)
...wertools/utilities/idempotency/persistence/base.py 99.36% <100.00%> (+<0.01%) ⬆️

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 9b88322...0fc80ce. Read the comment docs.

@heitorlessa
Copy link
Contributor

I like it - It solves these problems and branding, since data classes have grown to support methods and such.

It also solves #346 with a more specific doc - I'd like to include this in 1.18.0, as I'd need some time to create docs, think on how much to keep in each section and format.

@michaelbrewer
Copy link
Contributor Author

Should i update the docs then to include this?

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 28, 2021
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label May 29, 2021
@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 29, 2021
@michaelbrewer michaelbrewer changed the title feat(data-classes): decorator to instantiate data_classes feat(data-classes): decorator to instantiate data_classes and docs updates May 30, 2021
@heitorlessa
Copy link
Contributor

Confirming here that I have blocked time to review and get this incorporated tomorrow.

Just been abnormally busy in the last few days with a customer project

@heitorlessa heitorlessa added this to the 1.17.0 milestone Jun 4, 2021
@heitorlessa heitorlessa self-assigned this Jun 4, 2021
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.

One minor change to make it safer and more performant for the future, all rest LGTM :)

I don't have permission to push to your fork so will push the next release to early next week to ensure we incorporate these in time

Michael Brewer and others added 2 commits June 4, 2021 07:42
hasattr doesn't work in the way you might think it does - it's a misnomer in Python, really. It'll effectively do a blanket try/except, throw the results away, and specially in this case it'll access all getters from data -- the more getters the worse the performance.

Co-authored-by: Heitor Lessa <[email protected]>
@michaelbrewer
Copy link
Contributor Author

@heitorlessa does this look go to you now?

@heitorlessa
Copy link
Contributor

Yes, thanks a lot Mike!

@heitorlessa heitorlessa merged commit 675cc24 into aws-powertools:develop Jun 5, 2021
@michaelbrewer michaelbrewer deleted the feat-event-source-decorator branch June 5, 2021 17:09
heitorlessa added a commit that referenced this pull request Jun 8, 2021
…tools-python into develop

* 'develop' of https://github.com/awslabs/aws-lambda-powertools-python:
  chore(deps): bump boto3 from 1.17.88 to 1.17.89 (#466)
  feat(data-classes): add AttributeValueType to DynamoDBStreamEvent (#462)
  chore(deps): bump boto3 from 1.17.87 to 1.17.88 (#463)
  chore(deps-dev): bump mkdocs-material from 7.1.6 to 7.1.7 (#464)
  feat(data-classes): decorator to instantiate data_classes and docs updates (#442)
  chore(deps): bump boto3 from 1.17.86 to 1.17.87 (#459)
@heitorlessa heitorlessa added the feature New feature or functionality label Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature New feature or functionality size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants