sync.py
This file
Path
httpcore/sync.py
87 lines · 2.3 KiB · Python
Last changed by
Add SyncResponse.streamTom Christiebefore adoptionc9b9ea0
History of this fileTake it
Add SyncConnectionPoolTom Christiebefore adoption4a36ec7
lines 1–32why, one step back1import 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())32Add SyncResponse.streamTom Christiebefore adoptionc9b9ea0
lines 33–40why, one step back33 def stream(self) -> typing.Iterator[bytes]:34 inner = self._response.stream()35 while True:36 try:37 yield asyncio_run(inner.__anext__())38 except StopAsyncIteration as exc:39 break40Add SyncConnectionPoolTom Christiebefore adoption4a36ec7
lines 41–87why, one step back4142class SyncClient:43 def __init__(self, client: Client):44 self._client = client4546 def request(47 self,48 method: str,49 url: typing.Union[str, URL],50 *,51 headers: typing.Sequence[typing.Tuple[bytes, bytes]] = (),52 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",53 ssl: typing.Optional[SSLConfig] = None,54 timeout: typing.Optional[TimeoutConfig] = None,55 stream: bool = False,56 ) -> SyncResponse:57 response = asyncio_run(58 self._client.request(59 method,60 url,61 headers=headers,62 body=body,63 ssl=ssl,64 timeout=timeout,65 stream=stream,66 )67 )68 return SyncResponse(response)6970 def close(self) -> None:71 asyncio_run(self._client.close())7273 def __enter__(self) -> "SyncClient":74 return self7576 def __exit__(77 self,78 exc_type: typing.Type[BaseException] = None,79 exc_value: BaseException = None,80 traceback: TracebackType = None,81 ) -> None:82 self.close()838485def SyncConnectionPool(*args: typing.Any, **kwargs: typing.Any) -> SyncClient:86 client = ConnectionPool(*args, **kwargs) # type: ignore87 return SyncClient(client)