Skip to content
Myr

sync.py

This file

Path

httpcore/sync.py

90 lines · 2.3 KiB · Python

Last changed by

Add SyncResponse.closeTom Christiebefore adoption0308062

History of this file

Take it

Add SyncConnectionPoolTom Christiebefore adoption4a36ec7

lines 1–32why, one step back
1import typing
2from 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())32

Add SyncResponse.streamTom Christiebefore adoptionc9b9ea0

lines 33–40why, one step back
33 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 break40

Add SyncResponse.closeTom Christiebefore adoption0308062

lines 41–43why, one step back
41 def close(self) -> None:
42 return asyncio_run(self._response.close())43

Add SyncConnectionPoolTom Christiebefore adoption4a36ec7

lines 44–90why, one step back
44
45class SyncClient:46 def __init__(self, client: Client):47 self._client = client4849 def request(50 self,51 method: str,52 url: typing.Union[str, URL],53 *,54 headers: typing.Sequence[typing.Tuple[bytes, bytes]] = (),55 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",56 ssl: typing.Optional[SSLConfig] = None,57 timeout: typing.Optional[TimeoutConfig] = None,58 stream: bool = False,59 ) -> SyncResponse:60 response = asyncio_run(61 self._client.request(62 method,63 url,64 headers=headers,65 body=body,66 ssl=ssl,67 timeout=timeout,68 stream=stream,69 )70 )71 return SyncResponse(response)7273 def close(self) -> None:74 asyncio_run(self._client.close())7576 def __enter__(self) -> "SyncClient":77 return self7879 def __exit__(80 self,81 exc_type: typing.Type[BaseException] = None,82 exc_value: BaseException = None,83 traceback: TracebackType = None,84 ) -> None:85 self.close()868788def SyncConnectionPool(*args: typing.Any, **kwargs: typing.Any) -> SyncClient:89 client = ConnectionPool(*args, **kwargs) # type: ignore90 return SyncClient(client)