Source code for aas_client3_2.requests_transport

# SPDX-FileCopyrightText: 2026 aas-client3.2-python AUTHORS
#
# SPDX-License-Identifier: MIT

"""
Send the requests through a :py:class:`requests.Session`.

Install it with the extra ``aas-client3.2[requests]``. The session is passed in by
the user, so the session also carries the authentication, proxies, certificates
*etc.*:

.. code-block:: python

    import requests

    from aas_client3_2 import http
    from aas_client3_2 import requests_transport

    session = requests.Session()
    session.auth = ("user", "password")

    server = http.Server(
        transport=requests_transport.RequestsTransport(session),
        base_url="https://example.com/api/v3.2",
    )
"""

import contextlib
from typing import Iterator

try:
    import requests
except ImportError as exception:
    raise ImportError(
        "The module aas_client3_2.requests_transport needs the package requests. "
        "Please install it with: pip install aas-client3.2[requests]"
    ) from exception

from aas_client3_2 import http


[docs] class RequestsTransport: """Send the requests through a :py:class:`requests.Session`."""
[docs] def __init__( self, session: requests.Session, timeout: float = 30.0, chunk_size: int = 64 * 1024, ) -> None: """ Initialize with the given values. :param session: carries the authentication, proxies, certificates *etc.* :param timeout: in seconds for connecting and between the received bytes, not for the whole transfer :param chunk_size: size of the chunks in bytes when reading the response body """ self.session = session self.timeout = timeout self.chunk_size = chunk_size
[docs] @contextlib.contextmanager def send(self, request: http.Request) -> Iterator[http.StreamedResponse]: """Send the request, and give the response with the body in chunks.""" # NOTE (mristin): # A streamed body reports its length, so requests sends it with # ``Content-Length`` instead of chunked transfer encoding. with self.session.request( request.method, request.url, headers=request.headers, data=request.body, timeout=self.timeout, stream=True, allow_redirects=request.follow_redirects, ) as response: yield http.StreamedResponse( status_code=response.status_code, headers=response.headers, chunks=response.iter_content(chunk_size=self.chunk_size), )