Coder Social home page Coder Social logo

Support for Depends() about fastapi-events HOT 8 CLOSED

melvinkcx avatar melvinkcx commented on September 23, 2024 1
Support for Depends()

from fastapi-events.

Comments (8)

breathingcyborg avatar breathingcyborg commented on September 23, 2024 2

Hi @melvinkcx. We are facing the same issue. The event handlers often need to have access to other services, like in this example where we want to inject sequelize session using fastapi di system.

@local_handler.register(event_name='dummy-event')
async def dummy_event_handler(event : Event, db : Session = Depends(get_db)):
    # Do something with the db
    return

from fastapi-events.

melvinkcx avatar melvinkcx commented on September 23, 2024 2

Thank you for your patience. I just released a new version (link) that includes the support of dependencies in local handlers. Feel free to report any potential issues 🙏

from fastapi-events.

melvinkcx avatar melvinkcx commented on September 23, 2024 1

@unkindypie @breathingcyborg Thanks for raising the request. I'm looking into the feasibility of supporting DI in LocalHandler without introducing any breaking changes. If you already have an idea or a workaround you're currently using, feel free to share them =)

from fastapi-events.

breathingcyborg avatar breathingcyborg commented on September 23, 2024 1

@melvinkcx Thanks for looking into the issue.

As a workaround we created a custom decorator.

import inspect
from typing import Callable
from fastapi.dependencies.utils import get_dependant, solve_dependencies
from fastapi import Request
from contextlib import AsyncExitStack
from fastapi_events.typing import Event
from pydantic import ValidationError

def event_handler(path: str):
    """ decorator to enable fastapi dependency injection
    for fastapi_events event handlers

    Usage Example:
    @local_handler.register(event_name='event.test')
    @event_handler(path='handlers.event.test.handle_test_event')
    def handle_test_event(event: Optional[Event] = None, db: Session = Depends(get_db)):
        # use db here
    """
    def wrap(func: Callable):
        async def wrap_inner(event: Event):
            async with AsyncExitStack() as cm:
                request = Request({
                    "type": "http",
                    "headers": [],
                    "query_string": "",
                    "fastapi_astack": cm
                })
                dependant = get_dependant(
                    path=path,
                    call=func
                )
                values, errors, _1, _2, _3 = await solve_dependencies(
                    request=request,
                    dependant=dependant
                )
                values['event'] = event
                if errors:
                    raise ValidationError(errors, None)
                if inspect.iscoroutinefunction(dependant.call):
                    result = await dependant.call(**values)
                else:
                    result = dependant.call(**values)
                return result
        return wrap_inner
    return wrap

And we use it like this

@local_handler.register(event_name='event.test')
@event_handler(path='handlers.event.test.handle_test_event')
def handle_test_event(event: Optional[Event] = None, db: Session = Depends(get_db)):
    # use db here
    pass

This is very hacky

  • event must be the first argument and its value must be set to None
  • and because other things like dummy request and asyncexitstack.
  • also the path must be unique (not sure about this but I've read that fastapi caches dependencies)

from fastapi-events.

breathingcyborg avatar breathingcyborg commented on September 23, 2024 1

Thanks for your help. Although support for yield is not blocking for us, but it would be really helpful.

from fastapi-events.

melvinkcx avatar melvinkcx commented on September 23, 2024

Hi @unkindypie, I'm trying to understand the missing feature you're looking for here. Do you mean the events injected within the injected services are not working?
Do you mind providing a simple example of how you'd use it if it supports Depends()? Thank you =)

from fastapi-events.

melvinkcx avatar melvinkcx commented on September 23, 2024

Thanks for sharing your workaround. I'm working on a PR to support Depends() in local event handlers.

However, for the first version, there will be some limitations:

  • Sub-dependencies will be supported
  • Dependency caching is not supported
  • Dependency override is not supported
  • Dependencies using a generator (yield) are not supported (yet)

I figured the last point might be a blocker to some of you. Let me know if that's the case.

from fastapi-events.

unkindypie avatar unkindypie commented on September 23, 2024

Thank you @melvinkcx !

from fastapi-events.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.