Skip to content

Feature request: APIGatewayRestResolver decorator support for class methods (use case: constructor injection) #1696

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 of 2 tasks
harwoodjp opened this issue Nov 8, 2022 · 7 comments
Labels
event_handlers feature-request feature request help wanted Could use a second pair of eyes/hands

Comments

@harwoodjp
Copy link

harwoodjp commented Nov 8, 2022

Use case

I'd like to decorate class methods with APIGatewayRestResolver's router.

Particularly, I want to use constructor injection in my app. Support dependency injection in lambda parameters is good prerequisite reading.

Here is illustrative code. We have a GET that queries Dynamo with animal_id and returns a JSON response:

class App:
    resolver = APIGatewayRestResolver()

    def __init__(
            self,
            dynamo_client: DynamoClient = DynamoClient(ANIMAL_TABLE)
    ):
        self.dynamo_client = dynamo_client

    @resolver.get("/animal/<animal_id>")
    def get_animal(self, animal_id):
        result = self.dynamo_client.get_item_by_key("animal_id", animal_id)
        payload = json.dumps(result.get("data", []))
        return Response(
            status_code=HTTPStatus.OK,
            content_type=content_types.APPLICATION_JSON,
            body=payload,
        )

def lambda_handler(event, context):
    return App().resolver.resolve(event, context)

Currently I get the following error:

missing 1 required positional argument: 'self'

which indicates self argument isn't being passed. So I believe APIGatewayRestResolver router can't decorate class methods.

Solution/User Experience

Solution is probably:

  • Configure the decorator to pass self

Alternative solutions

Another solution is to simply use method injection as described by @heitorlessa in Support dependency injection in lambda parameters. This negates the benefits of constructor injection, which is useful when multiple methods share a client, for example.

Acknowledgment

@harwoodjp harwoodjp added feature-request feature request triage Pending triage from maintainers labels Nov 8, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Nov 8, 2022

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

@harwoodjp harwoodjp changed the title Feature request: APIGatewayRestResolver decorator support for constructor injection Feature request: APIGatewayRestResolver decorator support for class methods (use case: constructor injection) Nov 8, 2022
@heitorlessa
Copy link
Contributor

Hey @harwoodjp if you have the bandwidth, I'd welcome a PR for this.

The issue is that route method was implement as a closure not an actual decorator that could handle both functions and classes (it's different for classes, specially when preserving identity).

It's something I've been wanting to refactor but didn't get the bandwidth yet. Class support decorators is becoming a new ask so we should support this, expand the Middleware Factory utility, and more recently allow the new Middleware support with Classes in mind.

Appreciate any help here and can guide any step if you have the bandwidth too.

Thanks a lot!

@heitorlessa heitorlessa added event_handlers help wanted Could use a second pair of eyes/hands and removed triage Pending triage from maintainers labels Nov 8, 2022
@heitorlessa heitorlessa added the triage Pending triage from maintainers label Jan 31, 2023
@heitorlessa
Copy link
Contributor

Adding 'triage' label so we revisit it in upcoming iterations

@walmsles
Copy link
Contributor

walmsles commented Aug 3, 2023

I have completed an initial POC pass at API Middleware for ApiRestResolver - the middleware supports any python Callable structure including a class implementing the __call__ method. Maybe this would address the feature request here? I feel middleware addresses a lot of issues currently in the log that are old so commenting here to try and collate them all.

PR: #2917

@manmartgarc
Copy link

should this be closed?

@anafalcao
Copy link
Contributor

The use is covered, please open a new issue if needed!

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.

@anafalcao anafalcao moved this from Coming soon to Closed in Powertools for AWS Lambda (Python) Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature-request feature request help wanted Could use a second pair of eyes/hands
Projects
Development

No branches or pull requests

5 participants