Skip to content
Myr

sync.py

This file

Path

httpcore/sync.py

79 lines · 2.0 KiB · Python

Last changed by

Add SyncConnectionPoolTom Christiebefore adoption4a36ec7

History of this file

Take it

1import typing2from types import TracebackType34from .compat import asyncio_run5from .config import SSLConfig, TimeoutConfig6from .datastructures import URL, Client, Response7from .pool import ConnectionPool8910class SyncResponse:11    def __init__(self, response: Response):12        self._response = response1314    @property15    def status_code(self) -> int:16        return self._response.status_code1718    @property19    def reason(self) -> str:20        return self._response.reason2122    @property23    def headers(self) -> typing.List[typing.Tuple[bytes, bytes]]:24        return self._response.headers2526    @property27    def body(self) -> bytes:28        return self._response.body2930    def read(self) -> bytes:31        return asyncio_run(self._response.read())323334class SyncClient:35    def __init__(self, client: Client):36        self._client = client3738    def request(39        self,40        method: str,41        url: typing.Union[str, URL],42        *,43        headers: typing.Sequence[typing.Tuple[bytes, bytes]] = (),44        body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",45        ssl: typing.Optional[SSLConfig] = None,46        timeout: typing.Optional[TimeoutConfig] = None,47        stream: bool = False,48    ) -> SyncResponse:49        response = asyncio_run(50            self._client.request(51                method,52                url,53                headers=headers,54                body=body,55                ssl=ssl,56                timeout=timeout,57                stream=stream,58            )59        )60        return SyncResponse(response)6162    def close(self) -> None:63        asyncio_run(self._client.close())6465    def __enter__(self) -> "SyncClient":66        return self6768    def __exit__(69        self,70        exc_type: typing.Type[BaseException] = None,71        exc_value: BaseException = None,72        traceback: TracebackType = None,73    ) -> None:74        self.close()757677def SyncConnectionPool(*args: typing.Any, **kwargs: typing.Any) -> SyncClient:78    client = ConnectionPool(*args, **kwargs)  # type: ignore79    return SyncClient(client)