Skip to content

Service protocol

In Combadge a definition of a service protocol (also known as interface) is de-coupled from a service implementation. That allows a developer to define a service's interface and later bind it to a backend which in turn is directly responsible for handling requests and responses.

To define a service protocol one makes use of the PEP 544 aka «structural subtyping». Combadge inspects the protocol during «binding».

combadge.core.interfaces.SupportsService

Convenience base for service protocols.

Tip

Combadge can inspect any Protocol or ABC. But it might be a little easier to inherit from SupportsService since it provides the bind(to_backend) method as a shorthand for bind(from_protocol, to_backend).

Source code in combadge/core/interfaces.py
class SupportsService(Protocol):
    """
    Convenience base for service protocols.

    Tip:
        Combadge can inspect any `Protocol` or `ABC`.
        But it might be a little easier to inherit from `#!python SupportsService`
        since it provides the `bind(to_backend)` method as a shorthand for `#!python bind(from_protocol, to_backend)`.
    """

    @classmethod
    def bind(cls, to_backend: ProvidesBinder, /) -> Self:
        """Bind the current protocol to the specified backend."""
        return bind(cls, to_backend)

    def __enter__(self) -> Self:
        return self

    async def __aenter__(self) -> Self:
        return self

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
    ) -> Any:
        return None

    async def __aexit__(
        self,
        exc_type: type[BaseException] | None,
        exc_value: BaseException | None,
        traceback: TracebackType | None,
    ) -> Any:
        return None

bind classmethod

bind(to_backend: ProvidesBinder) -> Self

Bind the current protocol to the specified backend.

Source code in combadge/core/interfaces.py
@classmethod
def bind(cls, to_backend: ProvidesBinder, /) -> Self:
    """Bind the current protocol to the specified backend."""
    return bind(cls, to_backend)