Skip to content

ProxyResponse

Proxy response object.

This object encapsulates the incoming request and the response from target server. It exposes a method to set the response object which can then be modified before being returned to the client.

Parameters:

Name Type Description Default
in_resp ClientResponse

The incoming response object.

required
Source code in aiorp/response.py
class ProxyResponse:
    """Proxy response object.

    This object encapsulates the incoming request and the response from target server.
    It exposes a method to set the response object which can then be modified before being
    returned to the client.

    Args:
        in_resp: The incoming response object.
    """

    def __init__(
        self,
        in_resp: client.ClientResponse,
    ):
        """Initialize the proxy response object.

        Args:
            in_resp: The incoming response object.
        """
        self.in_resp: client.ClientResponse = in_resp
        self._web: web.StreamResponse | None = None
        self._content: bytes | None = None

    @property
    def web_response_set(self) -> bool:
        """Checks if the web response is set already.

        Returns:
            A boolean, true if set, false otherwise
        """
        return self._web is not None

    @property
    def web(
        self,
    ) -> StreamResponse | Response:
        """Access the web response.

        Returns:
            A response, either StreamResponse or Response.

        Raises:
            ValueError: When response is not set yet.
        """
        if self._web is None:
            raise ValueError("Response has not been set")
        return self._web

    async def set_response(
        self, response_type: ResponseType = ResponseType.BASE
    ) -> StreamResponse | Response:
        """Set the response using the given response type.

        Args:
            response_type: The type of response to set.

        Returns:
            The set web response.

        Raises:
            ValueError: When attempted to set the response a second time.
        """
        if self._web is not None:
            raise ValueError("Response can only be set once")
        if response_type == ResponseType.STREAM:
            self._web = await self._get_stream_response()
        else:
            self._web = await self._get_base_response()
        return self._web

    async def _get_stream_response(self) -> StreamResponse:
        """Convert incoming response to stream response."""

        headers = CIMultiDict(self.in_resp.headers)

        # These headers should not be proxied
        headers.pop("content-length", None)
        headers.pop("content-encoding", None)
        headers.pop("content-type", None)

        stream_resp = StreamResponse(
            status=self.in_resp.status,
            reason=self.in_resp.reason,
            headers=self.in_resp.headers,
        )
        return stream_resp

    async def _get_base_response(self) -> Response:
        """Convert incoming response to base response."""
        content = await self.in_resp.read()

        headers = CIMultiDict(self.in_resp.headers)

        # These headers should not be proxied
        headers.pop("content-length", None)
        headers.pop("content-encoding", None)

        if content:
            headers["content-length"] = str(len(content))

        resp = Response(
            status=self.in_resp.status,
            reason=self.in_resp.reason,
            headers=headers,
            body=content,
        )
        return resp

web property

Access the web response.

Returns:

Type Description
StreamResponse | Response

A response, either StreamResponse or Response.

Raises:

Type Description
ValueError

When response is not set yet.

web_response_set property

Checks if the web response is set already.

Returns:

Type Description
bool

A boolean, true if set, false otherwise

__init__(in_resp)

Initialize the proxy response object.

Parameters:

Name Type Description Default
in_resp ClientResponse

The incoming response object.

required
Source code in aiorp/response.py
def __init__(
    self,
    in_resp: client.ClientResponse,
):
    """Initialize the proxy response object.

    Args:
        in_resp: The incoming response object.
    """
    self.in_resp: client.ClientResponse = in_resp
    self._web: web.StreamResponse | None = None
    self._content: bytes | None = None

set_response(response_type=ResponseType.BASE) async

Set the response using the given response type.

Parameters:

Name Type Description Default
response_type ResponseType

The type of response to set.

BASE

Returns:

Type Description
StreamResponse | Response

The set web response.

Raises:

Type Description
ValueError

When attempted to set the response a second time.

Source code in aiorp/response.py
async def set_response(
    self, response_type: ResponseType = ResponseType.BASE
) -> StreamResponse | Response:
    """Set the response using the given response type.

    Args:
        response_type: The type of response to set.

    Returns:
        The set web response.

    Raises:
        ValueError: When attempted to set the response a second time.
    """
    if self._web is not None:
        raise ValueError("Response can only be set once")
    if response_type == ResponseType.STREAM:
        self._web = await self._get_stream_response()
    else:
        self._web = await self._get_base_response()
    return self._web