sync.py
This file
Path
httpcore/sync.py
91 lines · 2.6 KiB · Python
Last changed by
Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
History of this fileTake it
Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 1why, one step back1import asyncioMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 2–5why, one step back2import typing3from types import TracebackType45from .config import SSLConfig, TimeoutConfigMerge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
lines 6–8why, one step back6from .dispatch.connection_pool import ConnectionPool7from .interfaces import Adapter8from .models import URL, Headers, ResponseMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 9–11why, one step back91011class SyncResponse:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 12why, one step back12 def __init__(self, response: Response, loop: asyncio.AbstractEventLoop):Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 13why, one step back13 self._response = responseMerge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 14why, one step back14 self._loop = loopMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 15–20why, one step back1516 @property17 def status_code(self) -> int:18 return self._response.status_code1920 @propertyMerge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
lines 21–22why, one step back21 def reason_phrase(self) -> str:22 return self._response.reason_phraseMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 23–24why, one step back2324 @propertyMerge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
line 25why, one step back25 def headers(self) -> Headers:Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 26–28why, one step back26 return self._response.headers2728 @propertyMerge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
lines 29–30why, one step back29 def content(self) -> bytes:30 return self._response.contentMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 31–32why, one step back3132 def read(self) -> bytes:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 33why, one step back33 return self._loop.run_until_complete(self._response.read())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 34–38why, one step back3435 def stream(self) -> typing.Iterator[bytes]:36 inner = self._response.stream()37 while True:38 try:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 39why, one step back39 yield self._loop.run_until_complete(inner.__anext__())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 40–42why, one step back40 except StopAsyncIteration as exc:41 break42Add `raw` method to SyncResponseYeray Diaz Diazbefore adoption99dc1a9
lines 43–50why, one step back43 def raw(self) -> typing.Iterator[bytes]:44 inner = self._response.raw()45 while True:46 try:47 yield self._loop.run_until_complete(inner.__anext__())48 except StopAsyncIteration as exc:49 break50Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 51why, one step back51 def close(self) -> None:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 52why, one step back52 return self._loop.run_until_complete(self._response.close())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 53–55why, one step back535455class SyncClient:Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
lines 56–57why, one step back56 def __init__(self, adapter: Adapter):57 self._client = adapterMerge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 58why, one step back58 self._loop = asyncio.new_event_loop()Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 59–64why, one step back5960 def request(61 self,62 method: str,63 url: typing.Union[str, URL],64 *,Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
line 65why, one step back65 headers: typing.List[typing.Tuple[bytes, bytes]] = [],Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 66why, one step back66 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
line 67why, one step back67 **options: typing.AnyMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 68why, one step back68 ) -> SyncResponse:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 69why, one step back69 response = self._loop.run_until_complete(Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3
line 70why, one step back70 self._client.request(method, url, headers=headers, body=body, **options)Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 71why, one step back71 )Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 72why, one step back72 return SyncResponse(response, self._loop)Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 73–74why, one step back7374 def close(self) -> None:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 75why, one step back75 self._loop.run_until_complete(self._client.close())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 76–91why, one step back7677 def __enter__(self) -> "SyncClient":78 return self7980 def __exit__(81 self,82 exc_type: typing.Type[BaseException] = None,83 exc_value: BaseException = None,84 traceback: TracebackType = None,85 ) -> None:86 self.close()878889def SyncConnectionPool(*args: typing.Any, **kwargs: typing.Any) -> SyncClient:90 client = ConnectionPool(*args, **kwargs) # type: ignore91 return SyncClient(client)