Skip to content

Docs: HTTP options not supported in decorator #1922

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
1 task done
TownCube opened this issue Feb 12, 2023 · 7 comments · Fixed by #1936
Closed
1 task done

Docs: HTTP options not supported in decorator #1922

TownCube opened this issue Feb 12, 2023 · 7 comments · Fixed by #1936
Assignees
Labels
documentation Improvements or additions to documentation event_handlers feature New feature or functionality

Comments

@TownCube
Copy link

What were you searching in the docs?

HTTP Methods states:

You can use named decorators to specify the HTTP method that should be handled in your functions. That is, app.<http_method>, where the HTTP method could be get, post, put, patch, delete, and options.

However when you try and use app.options you get:

AttributeError: 'APIGatewayRestResolver' object has no attribue 'options'

Is this related to an existing documentation section?

https://awslabs.github.io/aws-lambda-powertools-python/2.8.0/core/event_handler/api_gateway/#http-methods

How can we improve?

Amend the documentation to make it clear not all HTTP methods are supported.

Got a suggestion in mind?

No response

Acknowledgment

  • I understand the final update might be different from my proposed suggestion, or refused.
@TownCube TownCube added documentation Improvements or additions to documentation triage Pending triage from maintainers labels Feb 12, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Feb 12, 2023

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our AWS Lambda Powertools Discord: Invite link

@TownCube TownCube changed the title Docs: TITLE Docs: HTTP options not supported in decorator Feb 12, 2023
@heitorlessa
Copy link
Contributor

heitorlessa commented Feb 12, 2023

Thank you @TownCube for flagging it - we'll get it fixed tomorrow.

Out of curiosity, how do you plan on handling pre-flight requests? Maybe the CORSConfig[1] feature can help?

If CORSConfig isn't sufficient, you can use app.route("*", method="OPTIONS") in the meantime.

Thank you for helping us improve everyone's experience.

[1] CORSConfig handles both pre-flight and auto-injecting CORS headers for responses/errors. Code snippet below from the docs

import requests
from requests import Response

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, CORSConfig
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
logger = Logger()
cors_config = CORSConfig(allow_origin="https://example.com", max_age=300)
app = APIGatewayRestResolver(cors=cors_config)


@app.get("/todos")
@tracer.capture_method
def get_todos():
    todos: 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]}


@app.get("/todos/<todo_id>")
@tracer.capture_method
def get_todo_by_id(todo_id: str):  # value come as str
    todos: Response = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}")
    todos.raise_for_status()

    return {"todos": todos.json()}


@app.get("/healthcheck", cors=False)  # optionally removes CORS for a given route
@tracer.capture_method
def am_i_alive():
    return {"am_i_alive": "yes"}


# 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)

@heitorlessa heitorlessa added event_handlers and removed triage Pending triage from maintainers labels Feb 12, 2023
@leandrodamascena leandrodamascena self-assigned this Feb 13, 2023
@leandrodamascena
Copy link
Contributor

Hi @TownCube! I assigned this problem to myself, but I need to understand this in detail, mainly because we can help others to explain how to use app.options on their system.

As @heitorlessa mentioned, I'm also curious about what scenarios might be useful for these options and how can we handle pre-flight requests. Of course, we can add the app.options method to make this work according to the documentation, but can you help us explain these scenarios so we can write examples and explain them in detail? Or maybe you have bandwidth to submit a PR with this code and documentation modification? We can work together to have that. Would be amazing having your contribution here.

Thank you

@leandrodamascena leandrodamascena added the need-more-information Pending information to continue label Feb 13, 2023
@TownCube
Copy link
Author

My use case is for dynamic CORS for both internal and external pages (so two CORS URLs) that consume the same API, hopefully I'll no longer need this once #1006 is resolved.

@leandrodamascena leandrodamascena added feature New feature or functionality and removed need-more-information Pending information to continue labels Feb 16, 2023
@leandrodamascena
Copy link
Contributor

leandrodamascena commented Feb 16, 2023

Hi @TownCube!
We understand your needs and we've reflected on this. For today's release, we will fix the docs and remove the OPTIONS method. After that, we will prioritize multiple CORS Sources in the safest possible manner, and document the ways in which customers should do this (infra+runtime). As we need a definitive solution, we will strive to add this feature to avoid more workarounds that end up creating tech debt that nobody wants.

What do you think about collaborating on this new feature submitting a PR, or even reviewing the code once we have the PR ready?

Thank you so much.

@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Feb 17, 2023
@github-actions
Copy link
Contributor

This is now released under 2.9.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Feb 21, 2023
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
Projects
None yet
3 participants