Skip to content

feat(event_handler): support to enable or disable compression in custom responses #2544

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

Merged
merged 6 commits into from
Jun 23, 2023
Merged

feat(event_handler): support to enable or disable compression in custom responses #2544

merged 6 commits into from
Jun 23, 2023

Conversation

leandrodamascena
Copy link
Contributor

@leandrodamascena leandrodamascena commented Jun 23, 2023

Issue number: #2513

Summary

Changes

Use case: Customers use the exception_handler decorator to handle 4xx/5xx errors. They might want to encode non-2xx responses.

This pull request improve the Response class with a new compress parameter to override compression setting defined in the route.

User experience

BEFORE

import json

from aws_lambda_powertools.event_handler import (
    APIGatewayRestResolver,
    Response,
    content_types,
)
from aws_lambda_powertools.utilities.typing import LambdaContext

app = APIGatewayRestResolver()

@app.exception_handler(NameError)
def handle_invalid_params(ex: NameError):
    response = Response(
            status_code=500,
            content_type=content_types.APPLICATION_JSON,
            body=json.dumps({"error": "error"})
        )
    return response 

@app.get("/hello", compress=True)
def hello():
    raise NameError("Error")


def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

AFTER

import json

from aws_lambda_powertools.event_handler import (
    APIGatewayRestResolver,
    Response,
    content_types,
)
from aws_lambda_powertools.utilities.typing import LambdaContext

app = APIGatewayRestResolver()

@app.exception_handler(NameError)
def handle_invalid_params(ex: NameError):
    response = Response(
            status_code=500,
            content_type=content_types.APPLICATION_JSON,
            body=json.dumps({"error": "error"}),
            compress=False
        )
    return response 

@app.get("/hello", compress=True)
def hello():
    raise NameError("Error")


def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team June 23, 2023 09:55
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation event_handlers tests labels Jun 23, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 23, 2023
@github-actions github-actions bot added the feature New feature or functionality label Jun 23, 2023
@heitorlessa
Copy link
Contributor

looking

@heitorlessa
Copy link
Contributor

quick comment: Update the example not to send stacktrace to prevent data leak; log the stack trace instead, and return another message instead back to the client.

@codecov-commenter
Copy link

codecov-commenter commented Jun 23, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (bf3f970) 97.19% compared to head (466f967) 97.20%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #2544   +/-   ##
========================================
  Coverage    97.19%   97.20%           
========================================
  Files          158      158           
  Lines         7348     7358   +10     
  Branches       531      534    +3     
========================================
+ Hits          7142     7152   +10     
  Misses         159      159           
  Partials        47       47           
Impacted Files Coverage Δ
...lambda_powertools/utilities/data_classes/common.py 100.00% <ø> (ø)
aws_lambda_powertools/event_handler/api_gateway.py 100.00% <100.00%> (ø)
...ws_lambda_powertools/utilities/batch/decorators.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

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.

This is great!! Only one major change to reduce cognitive overload and make it easier to extend new functionalities later.

@heitorlessa heitorlessa changed the title feat(event_handler): enhance Response class with compress parameter feat(event_handler): support to enable or disable compression in custom responses Jun 23, 2023
@leandrodamascena leandrodamascena merged commit 74cd99d into aws-powertools:develop Jun 23, 2023
rafaelgsr pushed a commit to rafaelgsr/aws-lambda-powertools-python that referenced this pull request Jun 30, 2023
…om responses (aws-powertools#2544)

* feature: adding Response compress parameter

* feature: addressing Heitor's feedback

* feature: addressing Heitor's feedback

* refactor(event_handler): make _has_compression_enabled standalone

---------

Co-authored-by: Heitor Lessa <lessa@amazon.co.uk>
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 event_handlers feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: APIGatewayRestResolver exception_handler compress data
3 participants