Skip to content
Myr

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 file

Take it

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 1why, one step back
1import asyncio

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 2–5why, one step back
2import typing
3from types import TracebackType45from .config import SSLConfig, TimeoutConfig

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

lines 6–8why, one step back
6from .dispatch.connection_pool import ConnectionPool
7from .interfaces import Adapter8from .models import URL, Headers, Response

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 9–11why, one step back
9
1011class SyncResponse:

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 12why, one step back
12 def __init__(self, response: Response, loop: asyncio.AbstractEventLoop):

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

line 13why, one step back
13 self._response = response

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 14why, one step back
14 self._loop = loop

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 15–20why, one step back
15
16 @property17 def status_code(self) -> int:18 return self._response.status_code1920 @property

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

lines 21–22why, one step back
21 def reason_phrase(self) -> str:
22 return self._response.reason_phrase

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 23–24why, one step back
23
24 @property

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

line 25why, one step back
25 def headers(self) -> Headers:

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 26–28why, one step back
26 return self._response.headers
2728 @property

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

lines 29–30why, one step back
29 def content(self) -> bytes:
30 return self._response.content

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 31–32why, one step back
31
32 def read(self) -> bytes:

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 33why, one step back
33 return self._loop.run_until_complete(self._response.read())

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 34–38why, one step back
34
35 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 back
39 yield self._loop.run_until_complete(inner.__anext__())

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 40–42why, one step back
40 except StopAsyncIteration as exc:
41 break42

Add `raw` method to SyncResponseYeray Diaz Diazbefore adoption99dc1a9

lines 43–50why, one step back
43 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 break50

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

line 51why, one step back
51 def close(self) -> None:

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 52why, one step back
52 return self._loop.run_until_complete(self._response.close())

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 53–55why, one step back
53
5455class SyncClient:

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

lines 56–57why, one step back
56 def __init__(self, adapter: Adapter):
57 self._client = adapter

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 58why, one step back
58 self._loop = asyncio.new_event_loop()

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 59–64why, one step back
59
60 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 back
65 headers: typing.List[typing.Tuple[bytes, bytes]] = [],

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

line 66why, one step back
66 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

line 67why, one step back
67 **options: typing.Any

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

line 68why, one step back
68 ) -> SyncResponse:

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 69why, one step back
69 response = self._loop.run_until_complete(

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

line 70why, one step back
70 self._client.request(method, url, headers=headers, body=body, **options)

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

line 71why, one step back
71 )

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 72why, one step back
72 return SyncResponse(response, self._loop)

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 73–74why, one step back
73
74 def close(self) -> None:

Merge pull request #17 from encode/close-keepalive-connectionsTom Christiebefore adoption91a2a1b

line 75why, one step back
75 self._loop.run_until_complete(self._client.close())

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 76–91why, one step back
76
77 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)