Skip to content
Myr

sync.py

This file

Path

httpcore/backends/sync.py

315 lines · 8.0 KiB · Python

Last changed by

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

History of this file

Take it

Reorganise structureTom Christiebefore adoption0c6f618

lines 1–18why, one step back
1import asyncio
2import typing3from types import TracebackType45from ..client import Client6from ..config import (7 DEFAULT_MAX_REDIRECTS,8 DEFAULT_POOL_LIMITS,9 DEFAULT_SSL_CONFIG,10 DEFAULT_TIMEOUT_CONFIG,11 PoolLimits,12 SSLConfig,13 TimeoutConfig,14)15from ..models import (16 URL,17 Headers,18 HeaderTypes,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 19why, one step back
19 QueryParamTypes,

Reorganise structureTom Christiebefore adoption0c6f618

line 20why, one step back
20 Request,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 21why, one step back
21 RequestData,

Reorganise structureTom Christiebefore adoption0c6f618

lines 22–103why, one step back
22 Response,
23 URLTypes,24)252627class SyncResponse:28 def __init__(self, response: Response, loop: asyncio.AbstractEventLoop):29 self._response = response30 self._loop = loop3132 @property33 def status_code(self) -> int:34 return self._response.status_code3536 @property37 def reason_phrase(self) -> str:38 return self._response.reason_phrase3940 @property41 def protocol(self) -> typing.Optional[str]:42 return self._response.protocol4344 @property45 def headers(self) -> Headers:46 return self._response.headers4748 @property49 def content(self) -> bytes:50 return self._response.content5152 @property53 def text(self) -> str:54 return self._response.text5556 def read(self) -> bytes:57 return self._loop.run_until_complete(self._response.read())5859 def stream(self) -> typing.Iterator[bytes]:60 inner = self._response.stream()61 while True:62 try:63 yield self._loop.run_until_complete(inner.__anext__())64 except StopAsyncIteration as exc:65 break6667 def raw(self) -> typing.Iterator[bytes]:68 inner = self._response.raw()69 while True:70 try:71 yield self._loop.run_until_complete(inner.__anext__())72 except StopAsyncIteration as exc:73 break7475 def close(self) -> None:76 return self._loop.run_until_complete(self._response.close())7778 def __repr__(self) -> str:79 class_name = self.__class__.__name__80 return f"<{class_name}(status_code={self.status_code})>"818283class SyncClient:84 def __init__(85 self,86 ssl: SSLConfig = DEFAULT_SSL_CONFIG,87 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,88 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,89 max_redirects: int = DEFAULT_MAX_REDIRECTS,90 ) -> None:91 self._client = Client(92 ssl=ssl,93 timeout=timeout,94 pool_limits=pool_limits,95 max_redirects=max_redirects,96 )97 self._loop = asyncio.new_event_loop()9899 def request(100 self,101 method: str,102 url: URLTypes,103 *,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

lines 104–105why, one step back
104 data: RequestData = b"",
105 query_params: QueryParamTypes = None,

Reorganise structureTom Christiebefore adoption0c6f618

lines 106–111why, one step back
106 headers: HeaderTypes = None,
107 stream: bool = False,108 allow_redirects: bool = True,109 ssl: SSLConfig = None,110 timeout: TimeoutConfig = None,111 ) -> SyncResponse:

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

lines 112–114why, one step back
112 request = Request(
113 method, url, data=data, query_params=query_params, headers=headers114 )

Reorganise structureTom Christiebefore adoption0c6f618

lines 115–128why, one step back
115 self.prepare_request(request)
116 response = self.send(117 request,118 stream=stream,119 allow_redirects=allow_redirects,120 ssl=ssl,121 timeout=timeout,122 )123 return response124125 def get(126 self,127 url: URLTypes,128 *,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 129why, one step back
129 query_params: QueryParamTypes = None,

Reorganise structureTom Christiebefore adoption0c6f618

lines 130–149why, one step back
130 headers: HeaderTypes = None,
131 stream: bool = False,132 allow_redirects: bool = True,133 ssl: SSLConfig = None,134 timeout: TimeoutConfig = None,135 ) -> SyncResponse:136 return self.request(137 "GET",138 url,139 headers=headers,140 stream=stream,141 allow_redirects=allow_redirects,142 ssl=ssl,143 timeout=timeout,144 )145146 def options(147 self,148 url: URLTypes,149 *,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 150why, one step back
150 query_params: QueryParamTypes = None,

Reorganise structureTom Christiebefore adoption0c6f618

lines 151–170why, one step back
151 headers: HeaderTypes = None,
152 stream: bool = False,153 allow_redirects: bool = True,154 ssl: SSLConfig = None,155 timeout: TimeoutConfig = None,156 ) -> SyncResponse:157 return self.request(158 "OPTIONS",159 url,160 headers=headers,161 stream=stream,162 allow_redirects=allow_redirects,163 ssl=ssl,164 timeout=timeout,165 )166167 def head(168 self,169 url: URLTypes,170 *,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 171why, one step back
171 query_params: QueryParamTypes = None,

Reorganise structureTom Christiebefore adoption0c6f618

lines 172–191why, one step back
172 headers: HeaderTypes = None,
173 stream: bool = False,174 allow_redirects: bool = False, #  Note: Differs to usual default.175 ssl: SSLConfig = None,176 timeout: TimeoutConfig = None,177 ) -> SyncResponse:178 return self.request(179 "HEAD",180 url,181 headers=headers,182 stream=stream,183 allow_redirects=allow_redirects,184 ssl=ssl,185 timeout=timeout,186 )187188 def post(189 self,190 url: URLTypes,191 *,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

lines 192–193why, one step back
192 data: RequestData = b"",
193 query_params: QueryParamTypes = None,

Reorganise structureTom Christiebefore adoption0c6f618

lines 194–202why, one step back
194 headers: HeaderTypes = None,
195 stream: bool = False,196 allow_redirects: bool = True,197 ssl: SSLConfig = None,198 timeout: TimeoutConfig = None,199 ) -> SyncResponse:200 return self.request(201 "POST",202 url,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 203why, one step back
203 data=data,

Reorganise structureTom Christiebefore adoption0c6f618

lines 204–214why, one step back
204 headers=headers,
205 stream=stream,206 allow_redirects=allow_redirects,207 ssl=ssl,208 timeout=timeout,209 )210211 def put(212 self,213 url: URLTypes,214 *,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

lines 215–216why, one step back
215 data: RequestData = b"",
216 query_params: QueryParamTypes = None,

Reorganise structureTom Christiebefore adoption0c6f618

lines 217–225why, one step back
217 headers: HeaderTypes = None,
218 stream: bool = False,219 allow_redirects: bool = True,220 ssl: SSLConfig = None,221 timeout: TimeoutConfig = None,222 ) -> SyncResponse:223 return self.request(224 "PUT",225 url,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 226why, one step back
226 data=data,

Reorganise structureTom Christiebefore adoption0c6f618

lines 227–237why, one step back
227 headers=headers,
228 stream=stream,229 allow_redirects=allow_redirects,230 ssl=ssl,231 timeout=timeout,232 )233234 def patch(235 self,236 url: URLTypes,237 *,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

lines 238–239why, one step back
238 data: RequestData = b"",
239 query_params: QueryParamTypes = None,

Reorganise structureTom Christiebefore adoption0c6f618

lines 240–248why, one step back
240 headers: HeaderTypes = None,
241 stream: bool = False,242 allow_redirects: bool = True,243 ssl: SSLConfig = None,244 timeout: TimeoutConfig = None,245 ) -> SyncResponse:246 return self.request(247 "PATCH",248 url,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 249why, one step back
249 data=data,

Reorganise structureTom Christiebefore adoption0c6f618

lines 250–260why, one step back
250 headers=headers,
251 stream=stream,252 allow_redirects=allow_redirects,253 ssl=ssl,254 timeout=timeout,255 )256257 def delete(258 self,259 url: URLTypes,260 *,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

lines 261–262why, one step back
261 data: RequestData = b"",
262 query_params: QueryParamTypes = None,

Reorganise structureTom Christiebefore adoption0c6f618

lines 263–271why, one step back
263 headers: HeaderTypes = None,
264 stream: bool = False,265 allow_redirects: bool = True,266 ssl: SSLConfig = None,267 timeout: TimeoutConfig = None,268 ) -> SyncResponse:269 return self.request(270 "DELETE",271 url,

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 272why, one step back
272 data=data,

Reorganise structureTom Christiebefore adoption0c6f618

lines 273–315why, one step back
273 headers=headers,
274 stream=stream,275 allow_redirects=allow_redirects,276 ssl=ssl,277 timeout=timeout,278 )279280 def prepare_request(self, request: Request) -> None:281 self._client.prepare_request(request)282283 def send(284 self,285 request: Request,286 *,287 stream: bool = False,288 allow_redirects: bool = True,289 ssl: SSLConfig = None,290 timeout: TimeoutConfig = None,291 ) -> SyncResponse:292 response = self._loop.run_until_complete(293 self._client.send(294 request,295 stream=stream,296 allow_redirects=allow_redirects,297 ssl=ssl,298 timeout=timeout,299 )300 )301 return SyncResponse(response, self._loop)302303 def close(self) -> None:304 self._loop.run_until_complete(self._client.close())305306 def __enter__(self) -> "SyncClient":307 return self308309 def __exit__(310 self,311 exc_type: typing.Type[BaseException] = None,312 exc_value: BaseException = None,313 traceback: TracebackType = None,314 ) -> None:315 self.close()