Skip to content
Myr

sync.py

This file

Path

httpcore/sync.py

90 lines · 2.4 KiB · Python

Last changed by

Module renamingTom Christiebefore adoptionb7d315a

History of this file

Take it

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 1–5why, one step back
1import typing
2from types import TracebackType34from .compat import asyncio_run5from .config import SSLConfig, TimeoutConfig

Module renamingTom Christiebefore adoptionb7d315a

line 6why, one step back
6from .connectionpool import ConnectionPool

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 7–90why, one step back
7from .datastructures import URL, Client, Response
8910class 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())3233 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 break4041 def close(self) -> None:42 return asyncio_run(self._response.close())434445class 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)