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

Allow user provided handlers #177

Closed
cblegare opened this issue Apr 9, 2021 · 3 comments
Closed

Allow user provided handlers #177

cblegare opened this issue Apr 9, 2021 · 3 comments
Labels
feature New feature or request

Comments

@cblegare
Copy link

cblegare commented Apr 9, 2021

Hello there!

While investigating #176 I found it could be useful to add user provided handler factories to Mangum. It could look like

    def __init__(
        self, 
        app: ASGIApp, 
        lifespan: str = "auto", 
        additional_handler_factories: Optional[List[Callable[[event: dict, context: "LambdaContext", Dict[str, Any]], AbstractHandler]]] = None
        **handler_kwargs: Dict[str, Any]
    ) -> None:
        self.app = app
        self.lifespan = lifespan
        self.additional_handler_factories = additional_handle_factories or []
        self.additional_handler_factories.append(AbstractHandler.from_trigger)
        self.handler_kwargs = handler_kwargs

        if self.lifespan not in ("auto", "on", "off"):
            raise ConfigurationError(
                "Invalid argument supplied for `lifespan`. Choices are: auto|on|off"
            )


    def __call__(self, event: dict, context: "LambdaContext") -> dict:
        logger.debug("Event received.")


        with ExitStack() as stack:
            if self.lifespan != "off":
                lifespan_cycle: ContextManager = LifespanCycle(self.app, self.lifespan)
                stack.enter_context(lifespan_cycle)

            for handler_factory in self.additional_handler_factories:
                handler = handler_factory(event, context, **self.handler_kwargs)
                if handler:
                    break
            else:
                raise TypeError("Unable to determine handler from trigger event")

            http_cycle = HTTPCycle(handler.request)
            response = http_cycle(self.app, handler.body)


        return handler.transform_response(response)

In place of:

https://github.com/jordaneremieff/mangum/blob/3f312acb67aac30c07dfb305d3cd1881c59755c4/mangum/adapter.py#L47-L73

In order to accept user-provider handler factories. Would you be interested in this?

@jordaneremieff
Copy link
Collaborator

@cblegare I like this idea, thanks for proposing it.

I started working on a handler for WebSockets that might impact this design, so I think after I have a chance to get the initial WebSocket handler implemented that we can see how to work in configurable handlers - there's a bit of refactoring needed first.

It might be a week or two before I can look into this further.

@jordaneremieff
Copy link
Collaborator

I've introduced this provisionally in a recent PR using a method similar to what you've demonstrated. It's provisional/undocumented, but you can find an example in https://github.com/jordaneremieff/mangum/blob/main/tests/handlers/test_custom.py. I'm not sure how this will ultimately end up, so I'll leave this issue open for discussion/until I'm sure whether or not to support this going forward.

@jordaneremieff
Copy link
Collaborator

This feature is now added. Not considered provisional at this point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants