Skip to content
Myr

sync.py

This file

Path

httpcore/sync.py

276 lines · 6.9 KiB · Python

Last changed by

Truckin' onTom Christiebefore adoption6b60d95

History of this file

Take it

1import asyncio2import 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 headers(self) -> Headers:41        return self._response.headers4243    @property44    def content(self) -> bytes:45        return self._response.content4647    @property48    def text(self) -> str:49        return self._response.text5051    def read(self) -> bytes:52        return self._loop.run_until_complete(self._response.read())5354    def stream(self) -> typing.Iterator[bytes]:55        inner = self._response.stream()56        while True:57            try:58                yield self._loop.run_until_complete(inner.__anext__())59            except StopAsyncIteration as exc:60                break6162    def raw(self) -> typing.Iterator[bytes]:63        inner = self._response.raw()64        while True:65            try:66                yield self._loop.run_until_complete(inner.__anext__())67            except StopAsyncIteration as exc:68                break6970    def close(self) -> None:71        return self._loop.run_until_complete(self._response.close())727374class SyncClient:75    def __init__(76        self,77        ssl: SSLConfig = DEFAULT_SSL_CONFIG,78        timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,79        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,80        max_redirects: int = DEFAULT_MAX_REDIRECTS,81    ) -> None:82        self._client = Client(83            ssl=ssl,84            timeout=timeout,85            pool_limits=pool_limits,86            max_redirects=max_redirects,87        )88        self._loop = asyncio.new_event_loop()8990    def request(91        self,92        method: str,93        url: URLTypes,94        *,95        content: ByteOrByteStream = b"",96        headers: HeaderTypes = None,97        stream: bool = False,98        allow_redirects: bool = True,99        ssl: SSLConfig = None,100        timeout: TimeoutConfig = None,101    ) -> SyncResponse:102        response = self._loop.run_until_complete(103            self._client.request(104                method,105                url,106                content=content,107                headers=headers,108                stream=stream,109                allow_redirects=allow_redirects,110                ssl=ssl,111                timeout=timeout,112            )113        )114        return SyncResponse(response, self._loop)115116    def get(117        self,118        url: URLTypes,119        *,120        headers: HeaderTypes = None,121        stream: bool = False,122        allow_redirects: bool = True,123        ssl: SSLConfig = None,124        timeout: TimeoutConfig = None,125    ) -> SyncResponse:126        return self.request(127            "GET",128            url,129            headers=headers,130            stream=stream,131            allow_redirects=allow_redirects,132            ssl=ssl,133            timeout=timeout,134        )135136    def options(137        self,138        url: URLTypes,139        *,140        headers: HeaderTypes = None,141        stream: bool = False,142        allow_redirects: bool = True,143        ssl: SSLConfig = None,144        timeout: TimeoutConfig = None,145    ) -> SyncResponse:146        return self.request(147            "OPTIONS",148            url,149            headers=headers,150            stream=stream,151            allow_redirects=allow_redirects,152            ssl=ssl,153            timeout=timeout,154        )155156    def head(157        self,158        url: URLTypes,159        *,160        headers: HeaderTypes = None,161        stream: bool = False,162        allow_redirects: bool = False,  #  Note: Differs to usual default.163        ssl: SSLConfig = None,164        timeout: TimeoutConfig = None,165    ) -> SyncResponse:166        return self.request(167            "HEAD",168            url,169            headers=headers,170            stream=stream,171            allow_redirects=allow_redirects,172            ssl=ssl,173            timeout=timeout,174        )175176    def post(177        self,178        url: URLTypes,179        *,180        content: ByteOrByteStream = b"",181        headers: HeaderTypes = None,182        stream: bool = False,183        allow_redirects: bool = True,184        ssl: SSLConfig = None,185        timeout: TimeoutConfig = None,186    ) -> SyncResponse:187        return self.request(188            "POST",189            url,190            content=content,191            headers=headers,192            stream=stream,193            allow_redirects=allow_redirects,194            ssl=ssl,195            timeout=timeout,196        )197198    def put(199        self,200        url: URLTypes,201        *,202        content: ByteOrByteStream = b"",203        headers: HeaderTypes = None,204        stream: bool = False,205        allow_redirects: bool = True,206        ssl: SSLConfig = None,207        timeout: TimeoutConfig = None,208    ) -> SyncResponse:209        return self.request(210            "PUT",211            url,212            content=content,213            headers=headers,214            stream=stream,215            allow_redirects=allow_redirects,216            ssl=ssl,217            timeout=timeout,218        )219220    def patch(221        self,222        url: URLTypes,223        *,224        content: ByteOrByteStream = b"",225        headers: HeaderTypes = None,226        stream: bool = False,227        allow_redirects: bool = True,228        ssl: SSLConfig = None,229        timeout: TimeoutConfig = None,230    ) -> SyncResponse:231        return self.request(232            "PATCH",233            url,234            content=content,235            headers=headers,236            stream=stream,237            allow_redirects=allow_redirects,238            ssl=ssl,239            timeout=timeout,240        )241242    def delete(243        self,244        url: URLTypes,245        *,246        content: ByteOrByteStream = b"",247        headers: HeaderTypes = None,248        stream: bool = False,249        allow_redirects: bool = True,250        ssl: SSLConfig = None,251        timeout: TimeoutConfig = None,252    ) -> SyncResponse:253        return self.request(254            "DELETE",255            url,256            content=content,257            headers=headers,258            stream=stream,259            allow_redirects=allow_redirects,260            ssl=ssl,261            timeout=timeout,262        )263264    def close(self) -> None:265        self._loop.run_until_complete(self._client.close())266267    def __enter__(self) -> "SyncClient":268        return self269270    def __exit__(271        self,272        exc_type: typing.Type[BaseException] = None,273        exc_value: BaseException = None,274        traceback: TracebackType = None,275    ) -> None:276        self.close()