Skip to content

Bug: Event Handler REST API missing Response at top-level import #1389

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

Closed
heitorlessa opened this issue Jul 27, 2022 · 1 comment
Closed
Assignees
Labels
bug Something isn't working

Comments

@heitorlessa
Copy link
Contributor

Expected Behaviour

Importing Response to return fine-grained response should be possible as per documentation

from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response

Current Behaviour

When importing Response from the top-level event_handler it yields an ImportError

ImportError: cannot import name 'Response' from 'aws_lambda_powertools.event_handler' (/Users/lessa/DEV/aws-lambda-powertools-python/aws_lambda_powertools/event_handler/__init__.py)

Code snippet

import requests

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response, content_types
from aws_lambda_powertools.event_handler.exceptions import NotFoundError
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()


@app.not_found
@tracer.capture_method
def handle_not_found_errors(exc: NotFoundError) -> Response:
    logger.info(f"Not found route: {app.current_event.path}")
    return Response(status_code=418, content_type=content_types.TEXT_PLAIN, body="I'm a teapot!")


@app.get("/todos")
@tracer.capture_method
def get_todos():
    todos: requests.Response = requests.get("https://jsonplaceholder.typicode.com/todos")
    todos.raise_for_status()

    # for brevity, we'll limit to the first 10 only
    return {"todos": todos.json()[:10]}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

Possible Solution

No response

Steps to Reproduce

Follow the documentation on returning fine grained responses.

AWS Lambda Powertools for Python version

latest

AWS Lambda function runtime

3.9

Packaging format used

Lambda Layers

Debugging logs

No response

@heitorlessa heitorlessa added bug Something isn't working triage Pending triage from maintainers and removed triage Pending triage from maintainers labels Jul 27, 2022
@heitorlessa heitorlessa self-assigned this Jul 27, 2022
@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Jul 27, 2022
@github-actions
Copy link
Contributor

This is now released under 1.26.7 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant