DontPanicO / fastapi-distributed-websocket

A library to implement websocket for distibuted system based on FastAPI.
MIT License
52 stars 10 forks source link

Normalize broker interfaces `__init__` signatures #7

Open DontPanicO opened 1 year ago

DontPanicO commented 1 year ago

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)