Skip to content
Myr

sync.py

This file

Path

httpcore/backends/sync.py

304 lines · 7.7 KiB · Python

Last changed by

Reorganise structureTom Christiebefore adoption0c6f618

History of this file

Take it

Reorganise structureTom Christiebefore adoption0c6f618

lines 1–304why, 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 ByteOrByteStream,18 Headers,19 HeaderTypes,20 Request,21 Response,22 URLTypes,23)242526class SyncResponse:27 def __init__(self, response: Response, loop: asyncio.AbstractEventLoop):28 self._response = response29 self._loop = loop3031 @property32 def status_code(self) -> int:33 return self._response.status_code3435 @property36 def reason_phrase(self) -> str:37 return self._response.reason_phrase3839 @property40 def protocol(self) -> typing.Optional[str]:41 return self._response.protocol4243 @property44 def headers(self) -> Headers:45 return self._response.headers4647 @property48 def content(self) -> bytes:49 return self._response.content5051 @property52 def text(self) -> str:53 return self._response.text5455 def read(self) -> bytes:56 return self._loop.run_until_complete(self._response.read())5758 def stream(self) -> typing.Iterator[bytes]:59 inner = self._response.stream()60 while True:61 try:62 yield self._loop.run_until_complete(inner.__anext__())63 except StopAsyncIteration as exc:64 break6566 def raw(self) -> typing.Iterator[bytes]:67 inner = self._response.raw()68 while True:69 try:70 yield self._loop.run_until_complete(inner.__anext__())71 except StopAsyncIteration as exc:72 break7374 def close(self) -> None:75 return self._loop.run_until_complete(self._response.close())7677 def __repr__(self) -> str:78 class_name = self.__class__.__name__79 return f"<{class_name}(status_code={self.status_code})>"808182class SyncClient:83 def __init__(84 self,85 ssl: SSLConfig = DEFAULT_SSL_CONFIG,86 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,87 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,88 max_redirects: int = DEFAULT_MAX_REDIRECTS,89 ) -> None:90 self._client = Client(91 ssl=ssl,92 timeout=timeout,93 pool_limits=pool_limits,94 max_redirects=max_redirects,95 )96 self._loop = asyncio.new_event_loop()9798 def request(99 self,100 method: str,101 url: URLTypes,102 *,103 content: ByteOrByteStream = b"",104 headers: HeaderTypes = None,105 stream: bool = False,106 allow_redirects: bool = True,107 ssl: SSLConfig = None,108 timeout: TimeoutConfig = None,109 ) -> SyncResponse:110 request = Request(method, url, headers=headers, content=content)111 self.prepare_request(request)112 response = self.send(113 request,114 stream=stream,115 allow_redirects=allow_redirects,116 ssl=ssl,117 timeout=timeout,118 )119 return response120121 def get(122 self,123 url: URLTypes,124 *,125 headers: HeaderTypes = None,126 stream: bool = False,127 allow_redirects: bool = True,128 ssl: SSLConfig = None,129 timeout: TimeoutConfig = None,130 ) -> SyncResponse:131 return self.request(132 "GET",133 url,134 headers=headers,135 stream=stream,136 allow_redirects=allow_redirects,137 ssl=ssl,138 timeout=timeout,139 )140141 def options(142 self,143 url: URLTypes,144 *,145 headers: HeaderTypes = None,146 stream: bool = False,147 allow_redirects: bool = True,148 ssl: SSLConfig = None,149 timeout: TimeoutConfig = None,150 ) -> SyncResponse:151 return self.request(152 "OPTIONS",153 url,154 headers=headers,155 stream=stream,156 allow_redirects=allow_redirects,157 ssl=ssl,158 timeout=timeout,159 )160161 def head(162 self,163 url: URLTypes,164 *,165 headers: HeaderTypes = None,166 stream: bool = False,167 allow_redirects: bool = False, #  Note: Differs to usual default.168 ssl: SSLConfig = None,169 timeout: TimeoutConfig = None,170 ) -> SyncResponse:171 return self.request(172 "HEAD",173 url,174 headers=headers,175 stream=stream,176 allow_redirects=allow_redirects,177 ssl=ssl,178 timeout=timeout,179 )180181 def post(182 self,183 url: URLTypes,184 *,185 content: ByteOrByteStream = b"",186 headers: HeaderTypes = None,187 stream: bool = False,188 allow_redirects: bool = True,189 ssl: SSLConfig = None,190 timeout: TimeoutConfig = None,191 ) -> SyncResponse:192 return self.request(193 "POST",194 url,195 content=content,196 headers=headers,197 stream=stream,198 allow_redirects=allow_redirects,199 ssl=ssl,200 timeout=timeout,201 )202203 def put(204 self,205 url: URLTypes,206 *,207 content: ByteOrByteStream = b"",208 headers: HeaderTypes = None,209 stream: bool = False,210 allow_redirects: bool = True,211 ssl: SSLConfig = None,212 timeout: TimeoutConfig = None,213 ) -> SyncResponse:214 return self.request(215 "PUT",216 url,217 content=content,218 headers=headers,219 stream=stream,220 allow_redirects=allow_redirects,221 ssl=ssl,222 timeout=timeout,223 )224225 def patch(226 self,227 url: URLTypes,228 *,229 content: ByteOrByteStream = b"",230 headers: HeaderTypes = None,231 stream: bool = False,232 allow_redirects: bool = True,233 ssl: SSLConfig = None,234 timeout: TimeoutConfig = None,235 ) -> SyncResponse:236 return self.request(237 "PATCH",238 url,239 content=content,240 headers=headers,241 stream=stream,242 allow_redirects=allow_redirects,243 ssl=ssl,244 timeout=timeout,245 )246247 def delete(248 self,249 url: URLTypes,250 *,251 content: ByteOrByteStream = b"",252 headers: HeaderTypes = None,253 stream: bool = False,254 allow_redirects: bool = True,255 ssl: SSLConfig = None,256 timeout: TimeoutConfig = None,257 ) -> SyncResponse:258 return self.request(259 "DELETE",260 url,261 content=content,262 headers=headers,263 stream=stream,264 allow_redirects=allow_redirects,265 ssl=ssl,266 timeout=timeout,267 )268269 def prepare_request(self, request: Request) -> None:270 self._client.prepare_request(request)271272 def send(273 self,274 request: Request,275 *,276 stream: bool = False,277 allow_redirects: bool = True,278 ssl: SSLConfig = None,279 timeout: TimeoutConfig = None,280 ) -> SyncResponse:281 response = self._loop.run_until_complete(282 self._client.send(283 request,284 stream=stream,285 allow_redirects=allow_redirects,286 ssl=ssl,287 timeout=timeout,288 )289 )290 return SyncResponse(response, self._loop)291292 def close(self) -> None:293 self._loop.run_until_complete(self._client.close())294295 def __enter__(self) -> "SyncClient":296 return self297298 def __exit__(299 self,300 exc_type: typing.Type[BaseException] = None,301 exc_value: BaseException = None,302 traceback: TracebackType = None,303 ) -> None:304 self.close()