sync.py
This file
Path
httpcore/sync.py
95 lines · 2.6 KiB · Python
Last changed by
Merge pull request #21 from yeraydiazdiaz/wait-for-connection-closeTom Christiebefore adoption118d94b
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 pull request #23 from encode/adaptersTom Christiebefore adoption696c054
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 @propertyFinessing interfaceTom Christiebefore adoptiond7619a9
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 pull request #23 from encode/adaptersTom Christiebefore adoption696c054
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 @propertyResponse.contentTom Christiebefore adoption4c55113
lines 29–30why, one step back29 def content(self) -> bytes:30 return self._response.contentMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 31why, one step back31Add idna support. More tests.Tom Christiebefore adoptionb948798
lines 32–35why, one step back32 @property33 def text(self) -> str:34 return self._response.text35Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 36why, one step back36 def read(self) -> bytes:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 37why, one step back37 return self._loop.run_until_complete(self._response.read())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 38–42why, one step back3839 def stream(self) -> typing.Iterator[bytes]:40 inner = self._response.stream()41 while True:42 try:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 43why, one step back43 yield self._loop.run_until_complete(inner.__anext__())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 44–46why, one step back44 except StopAsyncIteration as exc:45 break46Merge pull request #21 from yeraydiazdiaz/wait-for-connection-closeTom Christiebefore adoption118d94b
lines 47–54why, one step back47 def raw(self) -> typing.Iterator[bytes]:48 inner = self._response.raw()49 while True:50 try:51 yield self._loop.run_until_complete(inner.__anext__())52 except StopAsyncIteration as exc:53 break54Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 55why, one step back55 def close(self) -> None:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 56why, one step back56 return self._loop.run_until_complete(self._response.close())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 57–59why, one step back575859class SyncClient:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 60–61why, one step back60 def __init__(self, adapter: Adapter):61 self._client = adapterMerge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 62why, one step back62 self._loop = asyncio.new_event_loop()Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 63–68why, one step back6364 def request(65 self,66 method: str,67 url: typing.Union[str, URL],68 *,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 69why, one step back69 headers: typing.List[typing.Tuple[bytes, bytes]] = [],Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 70why, one step back70 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 71why, one step back71 **options: typing.AnyMerge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 72why, one step back72 ) -> SyncResponse:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 73why, one step back73 response = self._loop.run_until_complete(Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 74why, one step back74 self._client.request(method, url, headers=headers, body=body, **options)Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 75why, one step back75 )Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 76why, one step back76 return SyncResponse(response, self._loop)Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 77–78why, one step back7778 def close(self) -> None:Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b
line 79why, one step back79 self._loop.run_until_complete(self._client.close())Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 80–95why, one step back8081 def __enter__(self) -> "SyncClient":82 return self8384 def __exit__(85 self,86 exc_type: typing.Type[BaseException] = None,87 exc_value: BaseException = None,88 traceback: TracebackType = None,89 ) -> None:90 self.close()919293def SyncConnectionPool(*args: typing.Any, **kwargs: typing.Any) -> SyncClient:94 client = ConnectionPool(*args, **kwargs) # type: ignore95 return SyncClient(client)