fastapi-distributed-websocket icon indicating copy to clipboard operation
fastapi-distributed-websocket copied to clipboard

Normalize broker interfaces `__init__` signatures

Open DontPanicO opened this issue 2 years ago • 0 comments

Feature or enhancement

Ensure that __init__ method signature of interfaces inheriting from BrokerInterface contains a positional arg for the broker url.

Ptich

Interfaces inheriting from BrokerInterface now have different __init__ signatures. We should at least make sure that they accept a positional parameter for the broker url. That will also improve the _init_broker method used by WebSocketManager. We could then accept *args, **kwargs for user convenience:

# ./_broker.py

class BrokerInterface(ABC):
    @abstractmethod
    def __init__(broker_url: str, *args, **kwargs):
        ...
# Also `InMemoryBroker` and `RedisBroker`
# should be updated to match that

# ./manager.py

# this require also `create_broker` signature to
# be updated to match `*args`.
def _init_broker(url: str, broker_class: Any | None = None, *args, **kwargs) -> BrokerT:
    if broker_class:
        assert is_valid_broker(
            broker_class
        ), 'Invalid broker class. Use distributed_websocket.utils.is_valid_broker to check if your broker_class is valid.'
    broker_factory = broker_class or create_broker
    return broker_factory(url, *args, **kwargs)

DontPanicO avatar Feb 11 '23 11:02 DontPanicO