Skip to content

Response Adapter

response_adapter

Protocol for translating application output into transport-level responses.

ResponseAdapter

Bases: Protocol

Translates application-layer output into a transport response.

Implementations handle transport-specific serialization (e.g.
JSON encoding, setting HTTP status headers, formatting CLI
output) and produce the raw response type consumed by the
transport framework.

Example
class OrderResponse:
    def __init__(self, order_id: str, status: str) -> None:
        self.order_id = order_id
        self.status = status


class JsonResponseAdapter:
    def adapt(self, output: object) -> object:
        return {"data": output}


adapter = JsonResponseAdapter()
response = adapter.adapt(OrderResponse(order_id="42", status="paid"))
# response is a JSON-serializable dict
Source code in src/forging_blocks/presentation/adapters/response_adapter.py
class ResponseAdapter[UseCaseOutput, RawResponse](Protocol):
    """Translates application-layer output into a transport response.

    Implementations handle transport-specific serialization (e.g.
    JSON encoding, setting HTTP status headers, formatting CLI
    output) and produce the raw response type consumed by the
    transport framework.

    Example:
        ```python
        class OrderResponse:
            def __init__(self, order_id: str, status: str) -> None:
                self.order_id = order_id
                self.status = status


        class JsonResponseAdapter:
            def adapt(self, output: object) -> object:
                return {"data": output}


        adapter = JsonResponseAdapter()
        response = adapter.adapt(OrderResponse(order_id="42", status="paid"))
        # response is a JSON-serializable dict
        ```
    """

    def adapt(self, output: UseCaseOutput) -> RawResponse:
        """Convert successful use-case output into a transport response.

        Args:
            output: The value returned by ``UseCasePort.execute`` on
                success.

        Returns:
            A transport-level response ready to send to the caller.

        """
        ...

    def adapt_error(self, view_model: ErrorViewModel) -> RawResponse:
        """Convert an ``ErrorViewModel`` into a transport error response.

        Args:
            view_model: The error view model produced by
                ``ErrorPresenter.to_view_model``, optionally enriched
                with status codes by ``ErrorStatusCodeMapper``.

        Returns:
            A transport-level error response (e.g. an HTTP response
            with the appropriate status code and JSON body).

        """
        ...

adapt(output: UseCaseOutput) -> RawResponse

Convert successful use-case output into a transport response.

Parameters:

Name Type Description Default
output UseCaseOutput

The value returned by UseCasePort.execute on
success.

required

Returns:

Type Description
RawResponse

A transport-level response ready to send to the caller.

Source code in src/forging_blocks/presentation/adapters/response_adapter.py
def adapt(self, output: UseCaseOutput) -> RawResponse:
    """Convert successful use-case output into a transport response.

    Args:
        output: The value returned by ``UseCasePort.execute`` on
            success.

    Returns:
        A transport-level response ready to send to the caller.

    """
    ...

adapt_error(view_model: ErrorViewModel) -> RawResponse

Convert an ErrorViewModel into a transport error response.

Parameters:

Name Type Description Default
view_model ErrorViewModel

The error view model produced by
ErrorPresenter.to_view_model, optionally enriched
with status codes by ErrorStatusCodeMapper.

required

Returns:

Type Description
RawResponse

A transport-level error response (e.g. an HTTP response

RawResponse

with the appropriate status code and JSON body).

Source code in src/forging_blocks/presentation/adapters/response_adapter.py
def adapt_error(self, view_model: ErrorViewModel) -> RawResponse:
    """Convert an ``ErrorViewModel`` into a transport error response.

    Args:
        view_model: The error view model produced by
            ``ErrorPresenter.to_view_model``, optionally enriched
            with status codes by ``ErrorStatusCodeMapper``.

    Returns:
        A transport-level error response (e.g. an HTTP response
        with the appropriate status code and JSON body).

    """
    ...