omlish 0.0.0.dev467__py3-none-any.whl → 0.0.0.dev469__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- omlish/__about__.py +2 -2
 - omlish/asyncs/asyncio/sync.py +43 -0
 - omlish/asyncs/sync.py +25 -0
 - omlish/dataclasses/impl/api/classes/decorator.py +3 -0
 - omlish/dataclasses/impl/api/classes/make.py +3 -0
 - omlish/dataclasses/impl/concerns/repr.py +15 -2
 - omlish/dataclasses/specs.py +1 -0
 - omlish/http/all.py +16 -0
 - omlish/http/clients/asyncs.py +26 -14
 - omlish/http/clients/base.py +17 -1
 - omlish/http/clients/coro/__init__.py +0 -0
 - omlish/http/clients/coro/sync.py +170 -0
 - omlish/http/clients/default.py +208 -29
 - omlish/http/clients/executor.py +50 -0
 - omlish/http/clients/httpx.py +82 -4
 - omlish/http/clients/middleware.py +178 -0
 - omlish/http/clients/sync.py +25 -13
 - omlish/http/clients/urllib.py +4 -2
 - omlish/http/coro/client/connection.py +15 -6
 - omlish/http/coro/io.py +2 -0
 - omlish/http/urls.py +67 -0
 - omlish/io/buffers.py +3 -0
 - omlish/lang/__init__.py +3 -0
 - omlish/lang/functions.py +9 -4
 - omlish/lang/params.py +17 -0
 - omlish/sync.py +62 -21
 - {omlish-0.0.0.dev467.dist-info → omlish-0.0.0.dev469.dist-info}/METADATA +1 -1
 - {omlish-0.0.0.dev467.dist-info → omlish-0.0.0.dev469.dist-info}/RECORD +32 -25
 - {omlish-0.0.0.dev467.dist-info → omlish-0.0.0.dev469.dist-info}/WHEEL +0 -0
 - {omlish-0.0.0.dev467.dist-info → omlish-0.0.0.dev469.dist-info}/entry_points.txt +0 -0
 - {omlish-0.0.0.dev467.dist-info → omlish-0.0.0.dev469.dist-info}/licenses/LICENSE +0 -0
 - {omlish-0.0.0.dev467.dist-info → omlish-0.0.0.dev469.dist-info}/top_level.txt +0 -0
 
    
        omlish/http/clients/default.py
    CHANGED
    
    | 
         @@ -1,60 +1,239 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import abc
         
     | 
| 
      
 2 
     | 
    
         
            +
            import contextlib
         
     | 
| 
       1 
3 
     | 
    
         
             
            import typing as ta
         
     | 
| 
       2 
4 
     | 
    
         | 
| 
      
 5 
     | 
    
         
            +
            from ... import lang
         
     | 
| 
       3 
6 
     | 
    
         
             
            from ..headers import CanHttpHeaders
         
     | 
| 
      
 7 
     | 
    
         
            +
            from .asyncs import AsyncHttpClient
         
     | 
| 
      
 8 
     | 
    
         
            +
            from .base import HttpClientContext
         
     | 
| 
       4 
9 
     | 
    
         
             
            from .base import HttpRequest
         
     | 
| 
       5 
10 
     | 
    
         
             
            from .base import HttpResponse
         
     | 
| 
       6 
11 
     | 
    
         
             
            from .sync import HttpClient
         
     | 
| 
       7 
     | 
    
         
            -
             
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            with lang.auto_proxy_import(globals()):
         
     | 
| 
      
 15 
     | 
    
         
            +
                from . import httpx as _httpx
         
     | 
| 
      
 16 
     | 
    
         
            +
                from . import urllib as _urllib
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            C = ta.TypeVar('C')
         
     | 
| 
      
 20 
     | 
    
         
            +
            R = ta.TypeVar('R')
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            ##
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
            class _DefaultRequester(lang.Abstract, ta.Generic[C, R]):
         
     | 
| 
      
 27 
     | 
    
         
            +
                def __call__(
         
     | 
| 
      
 28 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 29 
     | 
    
         
            +
                        url: str,
         
     | 
| 
      
 30 
     | 
    
         
            +
                        method: str | None = None,
         
     | 
| 
      
 31 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 32 
     | 
    
         
            +
                        headers: CanHttpHeaders | None = None,
         
     | 
| 
      
 33 
     | 
    
         
            +
                        data: bytes | str | None = None,
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                        timeout_s: float | None = None,
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                        context: HttpClientContext | None = None,
         
     | 
| 
      
 38 
     | 
    
         
            +
                        check: bool = False,
         
     | 
| 
      
 39 
     | 
    
         
            +
                        client: C | None = None,  # noqa
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                        **kwargs: ta.Any,
         
     | 
| 
      
 42 
     | 
    
         
            +
                ) -> R:
         
     | 
| 
      
 43 
     | 
    
         
            +
                    request = HttpRequest(  # noqa
         
     | 
| 
      
 44 
     | 
    
         
            +
                        url,
         
     | 
| 
      
 45 
     | 
    
         
            +
                        method=method,
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                        headers=headers,
         
     | 
| 
      
 48 
     | 
    
         
            +
                        data=data,
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                        timeout_s=timeout_s,
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                        **kwargs,
         
     | 
| 
      
 53 
     | 
    
         
            +
                    )
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                    return self._do(
         
     | 
| 
      
 56 
     | 
    
         
            +
                        request,
         
     | 
| 
      
 57 
     | 
    
         
            +
                        context=context,
         
     | 
| 
      
 58 
     | 
    
         
            +
                        check=check,
         
     | 
| 
      
 59 
     | 
    
         
            +
                        client=client,
         
     | 
| 
      
 60 
     | 
    
         
            +
                    )
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                @abc.abstractmethod
         
     | 
| 
      
 63 
     | 
    
         
            +
                def _do(
         
     | 
| 
      
 64 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 65 
     | 
    
         
            +
                        request: HttpRequest,  # noqa
         
     | 
| 
      
 66 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 67 
     | 
    
         
            +
                        context: HttpClientContext | None = None,
         
     | 
| 
      
 68 
     | 
    
         
            +
                        check: bool = False,
         
     | 
| 
      
 69 
     | 
    
         
            +
                        client: C | None = None,  # noqa
         
     | 
| 
      
 70 
     | 
    
         
            +
                ) -> R:
         
     | 
| 
      
 71 
     | 
    
         
            +
                    raise NotImplementedError
         
     | 
| 
       8 
72 
     | 
    
         | 
| 
       9 
73 
     | 
    
         | 
| 
       10 
74 
     | 
    
         
             
            ##
         
     | 
| 
       11 
75 
     | 
    
         | 
| 
       12 
76 
     | 
    
         | 
| 
       13 
77 
     | 
    
         
             
            def _default_client() -> HttpClient:
         
     | 
| 
       14 
     | 
    
         
            -
                return UrllibHttpClient()
         
     | 
| 
      
 78 
     | 
    
         
            +
                return _urllib.UrllibHttpClient()
         
     | 
| 
       15 
79 
     | 
    
         | 
| 
       16 
80 
     | 
    
         | 
| 
       17 
81 
     | 
    
         
             
            def client() -> HttpClient:
         
     | 
| 
       18 
82 
     | 
    
         
             
                return _default_client()
         
     | 
| 
       19 
83 
     | 
    
         | 
| 
       20 
84 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
             
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
                     
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
      
 85 
     | 
    
         
            +
            @contextlib.contextmanager
         
     | 
| 
      
 86 
     | 
    
         
            +
            def manage_client(client: HttpClient | None) -> ta.Generator[HttpClient]:  # noqa
         
     | 
| 
      
 87 
     | 
    
         
            +
                if client is not None:
         
     | 
| 
      
 88 
     | 
    
         
            +
                    yield client
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
                else:
         
     | 
| 
      
 91 
     | 
    
         
            +
                    with _default_client() as client:  # noqa
         
     | 
| 
      
 92 
     | 
    
         
            +
                        yield client
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
      
 95 
     | 
    
         
            +
            #
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
             
     | 
| 
      
 98 
     | 
    
         
            +
            class _BaseSyncDefaultRequester(_DefaultRequester[HttpClient, R], lang.Abstract, ta.Generic[R]):
         
     | 
| 
      
 99 
     | 
    
         
            +
                def _do(
         
     | 
| 
      
 100 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 101 
     | 
    
         
            +
                        request: HttpRequest,  # noqa
         
     | 
| 
      
 102 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 103 
     | 
    
         
            +
                        context: HttpClientContext | None = None,
         
     | 
| 
      
 104 
     | 
    
         
            +
                        check: bool = False,
         
     | 
| 
      
 105 
     | 
    
         
            +
                        client: HttpClient | None = None,  # noqa
         
     | 
| 
      
 106 
     | 
    
         
            +
                ) -> R:
         
     | 
| 
      
 107 
     | 
    
         
            +
                    if context is None:
         
     | 
| 
      
 108 
     | 
    
         
            +
                        context = HttpClientContext()
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
                    if client is not None:
         
     | 
| 
      
 111 
     | 
    
         
            +
                        return self._do_(
         
     | 
| 
      
 112 
     | 
    
         
            +
                            client,
         
     | 
| 
      
 113 
     | 
    
         
            +
                            context,
         
     | 
| 
      
 114 
     | 
    
         
            +
                            request,
         
     | 
| 
      
 115 
     | 
    
         
            +
                            check=check,
         
     | 
| 
      
 116 
     | 
    
         
            +
                        )
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
      
 118 
     | 
    
         
            +
                    else:
         
     | 
| 
      
 119 
     | 
    
         
            +
                        with _default_client() as client:  # noqa
         
     | 
| 
      
 120 
     | 
    
         
            +
                            return self._do_(
         
     | 
| 
      
 121 
     | 
    
         
            +
                                client,
         
     | 
| 
      
 122 
     | 
    
         
            +
                                context,
         
     | 
| 
      
 123 
     | 
    
         
            +
                                request,
         
     | 
| 
      
 124 
     | 
    
         
            +
                                check=check,
         
     | 
| 
      
 125 
     | 
    
         
            +
                            )
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                @abc.abstractmethod
         
     | 
| 
      
 128 
     | 
    
         
            +
                def _do_(
         
     | 
| 
      
 129 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 130 
     | 
    
         
            +
                        client: HttpClient,  # noqa
         
     | 
| 
      
 131 
     | 
    
         
            +
                        context: HttpClientContext,
         
     | 
| 
      
 132 
     | 
    
         
            +
                        request: HttpRequest,  # noqa
         
     | 
| 
      
 133 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 134 
     | 
    
         
            +
                        check: bool = False,  # noqa
         
     | 
| 
      
 135 
     | 
    
         
            +
                ) -> R:
         
     | 
| 
      
 136 
     | 
    
         
            +
                    raise NotImplementedError
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
             
     | 
| 
      
 139 
     | 
    
         
            +
            class _SyncDefaultRequester(_BaseSyncDefaultRequester[HttpResponse]):
         
     | 
| 
      
 140 
     | 
    
         
            +
                def _do_(
         
     | 
| 
      
 141 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 142 
     | 
    
         
            +
                        client: HttpClient,  # noqa
         
     | 
| 
      
 143 
     | 
    
         
            +
                        context: HttpClientContext,
         
     | 
| 
      
 144 
     | 
    
         
            +
                        request: HttpRequest,  # noqa
         
     | 
| 
      
 145 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 146 
     | 
    
         
            +
                        check: bool = False,  # noqa
         
     | 
| 
      
 147 
     | 
    
         
            +
                ) -> HttpResponse:
         
     | 
| 
      
 148 
     | 
    
         
            +
                    return client.request(
         
     | 
| 
      
 149 
     | 
    
         
            +
                        request,
         
     | 
| 
      
 150 
     | 
    
         
            +
                        context=context,
         
     | 
| 
      
 151 
     | 
    
         
            +
                        check=check,
         
     | 
| 
      
 152 
     | 
    
         
            +
                    )
         
     | 
| 
      
 153 
     | 
    
         
            +
             
     | 
| 
      
 154 
     | 
    
         
            +
             
     | 
| 
      
 155 
     | 
    
         
            +
            request = _SyncDefaultRequester()
         
     | 
| 
       27 
156 
     | 
    
         | 
| 
       28 
     | 
    
         
            -
                    timeout_s: float | None = None,
         
     | 
| 
       29 
157 
     | 
    
         | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
      
 158 
     | 
    
         
            +
            ##
         
     | 
| 
       31 
159 
     | 
    
         | 
| 
       32 
     | 
    
         
            -
                    client: HttpClient | None = None,  # noqa
         
     | 
| 
       33 
160 
     | 
    
         | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
       35 
     | 
    
         
            -
            ) 
     | 
| 
       36 
     | 
    
         
            -
                req = HttpRequest(
         
     | 
| 
       37 
     | 
    
         
            -
                    url,
         
     | 
| 
       38 
     | 
    
         
            -
                    method=method,
         
     | 
| 
      
 161 
     | 
    
         
            +
            def _default_async_client() -> AsyncHttpClient:
         
     | 
| 
      
 162 
     | 
    
         
            +
                return _httpx.HttpxAsyncHttpClient()
         
     | 
| 
       39 
163 
     | 
    
         | 
| 
       40 
     | 
    
         
            -
                    headers=headers,
         
     | 
| 
       41 
     | 
    
         
            -
                    data=data,
         
     | 
| 
       42 
164 
     | 
    
         | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
      
 165 
     | 
    
         
            +
            def async_client() -> AsyncHttpClient:
         
     | 
| 
      
 166 
     | 
    
         
            +
                return _default_async_client()
         
     | 
| 
       44 
167 
     | 
    
         | 
| 
       45 
     | 
    
         
            -
                    **kwargs,
         
     | 
| 
       46 
     | 
    
         
            -
                )
         
     | 
| 
       47 
168 
     | 
    
         | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
      
 169 
     | 
    
         
            +
            @contextlib.asynccontextmanager
         
     | 
| 
      
 170 
     | 
    
         
            +
            async def manage_async_client(client: AsyncHttpClient | None) -> ta.AsyncGenerator[AsyncHttpClient]:  # noqa
         
     | 
| 
      
 171 
     | 
    
         
            +
                if client is not None:
         
     | 
| 
      
 172 
     | 
    
         
            +
                    yield client
         
     | 
| 
       51 
173 
     | 
    
         | 
| 
      
 174 
     | 
    
         
            +
                else:
         
     | 
| 
      
 175 
     | 
    
         
            +
                    async with _default_async_client() as client:  # noqa
         
     | 
| 
      
 176 
     | 
    
         
            +
                        yield client
         
     | 
| 
      
 177 
     | 
    
         
            +
             
     | 
| 
      
 178 
     | 
    
         
            +
             
     | 
| 
      
 179 
     | 
    
         
            +
            #
         
     | 
| 
      
 180 
     | 
    
         
            +
             
     | 
| 
      
 181 
     | 
    
         
            +
             
     | 
| 
      
 182 
     | 
    
         
            +
            class _BaseAsyncDefaultRequester(_DefaultRequester[AsyncHttpClient, ta.Awaitable[R]], lang.Abstract, ta.Generic[R]):
         
     | 
| 
      
 183 
     | 
    
         
            +
                async def _do(
         
     | 
| 
      
 184 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 185 
     | 
    
         
            +
                        request: HttpRequest,  # noqa
         
     | 
| 
      
 186 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 187 
     | 
    
         
            +
                        context: HttpClientContext | None = None,
         
     | 
| 
      
 188 
     | 
    
         
            +
                        check: bool = False,
         
     | 
| 
      
 189 
     | 
    
         
            +
                        client: AsyncHttpClient | None = None,  # noqa
         
     | 
| 
      
 190 
     | 
    
         
            +
                ) -> R:
         
     | 
| 
      
 191 
     | 
    
         
            +
                    if context is None:
         
     | 
| 
      
 192 
     | 
    
         
            +
                        context = HttpClientContext()
         
     | 
| 
      
 193 
     | 
    
         
            +
             
     | 
| 
      
 194 
     | 
    
         
            +
                    if client is not None:
         
     | 
| 
      
 195 
     | 
    
         
            +
                        return await self._do_(
         
     | 
| 
      
 196 
     | 
    
         
            +
                            client,
         
     | 
| 
      
 197 
     | 
    
         
            +
                            context,
         
     | 
| 
      
 198 
     | 
    
         
            +
                            request,
         
     | 
| 
      
 199 
     | 
    
         
            +
                            check=check,
         
     | 
| 
      
 200 
     | 
    
         
            +
                        )
         
     | 
| 
      
 201 
     | 
    
         
            +
             
     | 
| 
      
 202 
     | 
    
         
            +
                    else:
         
     | 
| 
      
 203 
     | 
    
         
            +
                        async with _default_async_client() as client:  # noqa
         
     | 
| 
      
 204 
     | 
    
         
            +
                            return await self._do_(
         
     | 
| 
      
 205 
     | 
    
         
            +
                                client,
         
     | 
| 
      
 206 
     | 
    
         
            +
                                context,
         
     | 
| 
      
 207 
     | 
    
         
            +
                                request,
         
     | 
| 
      
 208 
     | 
    
         
            +
                                check=check,
         
     | 
| 
      
 209 
     | 
    
         
            +
                            )
         
     | 
| 
      
 210 
     | 
    
         
            +
             
     | 
| 
      
 211 
     | 
    
         
            +
                @abc.abstractmethod
         
     | 
| 
      
 212 
     | 
    
         
            +
                def _do_(
         
     | 
| 
      
 213 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 214 
     | 
    
         
            +
                        client: AsyncHttpClient,  # noqa
         
     | 
| 
      
 215 
     | 
    
         
            +
                        context: HttpClientContext,
         
     | 
| 
      
 216 
     | 
    
         
            +
                        request: HttpRequest,  # noqa
         
     | 
| 
      
 217 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 218 
     | 
    
         
            +
                        check: bool = False,  # noqa
         
     | 
| 
      
 219 
     | 
    
         
            +
                ) -> ta.Awaitable[R]:
         
     | 
| 
      
 220 
     | 
    
         
            +
                    raise NotImplementedError
         
     | 
| 
      
 221 
     | 
    
         
            +
             
     | 
| 
      
 222 
     | 
    
         
            +
             
     | 
| 
      
 223 
     | 
    
         
            +
            class _AsyncDefaultRequester(_BaseAsyncDefaultRequester[HttpResponse]):
         
     | 
| 
      
 224 
     | 
    
         
            +
                async def _do_(
         
     | 
| 
      
 225 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 226 
     | 
    
         
            +
                        client: AsyncHttpClient,  # noqa
         
     | 
| 
      
 227 
     | 
    
         
            +
                        context: HttpClientContext,
         
     | 
| 
      
 228 
     | 
    
         
            +
                        request: HttpRequest,  # noqa
         
     | 
| 
      
 229 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 230 
     | 
    
         
            +
                        check: bool = False,
         
     | 
| 
      
 231 
     | 
    
         
            +
                ) -> HttpResponse:  # noqa
         
     | 
| 
      
 232 
     | 
    
         
            +
                    return await client.request(
         
     | 
| 
      
 233 
     | 
    
         
            +
                        request,
         
     | 
| 
      
 234 
     | 
    
         
            +
                        context=context,
         
     | 
| 
       52 
235 
     | 
    
         
             
                        check=check,
         
     | 
| 
       53 
236 
     | 
    
         
             
                    )
         
     | 
| 
       54 
237 
     | 
    
         | 
| 
       55 
     | 
    
         
            -
                if client is not None:
         
     | 
| 
       56 
     | 
    
         
            -
                    return do(client)
         
     | 
| 
       57 
238 
     | 
    
         | 
| 
       58 
     | 
    
         
            -
             
     | 
| 
       59 
     | 
    
         
            -
                    with _default_client() as cli:
         
     | 
| 
       60 
     | 
    
         
            -
                        return do(cli)
         
     | 
| 
      
 239 
     | 
    
         
            +
            async_request = _AsyncDefaultRequester()
         
     | 
| 
         @@ -0,0 +1,50 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # ruff: noqa: UP043 UP045
         
     | 
| 
      
 2 
     | 
    
         
            +
            # @omlish-lite
         
     | 
| 
      
 3 
     | 
    
         
            +
            import dataclasses as dc
         
     | 
| 
      
 4 
     | 
    
         
            +
            import typing as ta
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            from .asyncs import AsyncHttpClient
         
     | 
| 
      
 7 
     | 
    
         
            +
            from .asyncs import AsyncStreamHttpResponse
         
     | 
| 
      
 8 
     | 
    
         
            +
            from .base import HttpClientContext
         
     | 
| 
      
 9 
     | 
    
         
            +
            from .base import HttpRequest
         
     | 
| 
      
 10 
     | 
    
         
            +
            from .sync import HttpClient
         
     | 
| 
      
 11 
     | 
    
         
            +
            from .sync import StreamHttpResponse
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            ##
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            class ExecutorAsyncHttpClient(AsyncHttpClient):
         
     | 
| 
      
 18 
     | 
    
         
            +
                def __init__(
         
     | 
| 
      
 19 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 20 
     | 
    
         
            +
                        run_in_executor: ta.Callable[..., ta.Awaitable],
         
     | 
| 
      
 21 
     | 
    
         
            +
                        client: HttpClient,
         
     | 
| 
      
 22 
     | 
    
         
            +
                ) -> None:
         
     | 
| 
      
 23 
     | 
    
         
            +
                    super().__init__()
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                    self._run_in_executor = run_in_executor
         
     | 
| 
      
 26 
     | 
    
         
            +
                    self._client = client
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                @dc.dataclass(frozen=True)
         
     | 
| 
      
 29 
     | 
    
         
            +
                class _StreamAdapter:
         
     | 
| 
      
 30 
     | 
    
         
            +
                    owner: 'ExecutorAsyncHttpClient'
         
     | 
| 
      
 31 
     | 
    
         
            +
                    resp: StreamHttpResponse
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                    async def read1(self, /, n: int = -1) -> bytes:
         
     | 
| 
      
 34 
     | 
    
         
            +
                        return await self.owner._run_in_executor(self.resp.stream.read1, n)  # noqa
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                    async def close(self) -> None:
         
     | 
| 
      
 37 
     | 
    
         
            +
                        return await self.owner._run_in_executor(self.resp.close)  # noqa
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                async def _stream_request(self, ctx: HttpClientContext, req: HttpRequest) -> AsyncStreamHttpResponse:
         
     | 
| 
      
 40 
     | 
    
         
            +
                    resp: StreamHttpResponse = await self._run_in_executor(lambda: self._client.stream_request(req, context=ctx))
         
     | 
| 
      
 41 
     | 
    
         
            +
                    return AsyncStreamHttpResponse(
         
     | 
| 
      
 42 
     | 
    
         
            +
                        status=resp.status,
         
     | 
| 
      
 43 
     | 
    
         
            +
                        headers=resp.headers,
         
     | 
| 
      
 44 
     | 
    
         
            +
                        request=req,
         
     | 
| 
      
 45 
     | 
    
         
            +
                        underlying=resp,
         
     | 
| 
      
 46 
     | 
    
         
            +
                        **(dict(  # type: ignore
         
     | 
| 
      
 47 
     | 
    
         
            +
                            stream=(adapter := self._StreamAdapter(self, resp)),
         
     | 
| 
      
 48 
     | 
    
         
            +
                            _closer=adapter.close,
         
     | 
| 
      
 49 
     | 
    
         
            +
                        ) if resp.has_data else {}),
         
     | 
| 
      
 50 
     | 
    
         
            +
                    )
         
     | 
    
        omlish/http/clients/httpx.py
    CHANGED
    
    | 
         @@ -2,12 +2,17 @@ 
     | 
|
| 
       2 
2 
     | 
    
         
             
            TODO:
         
     | 
| 
       3 
3 
     | 
    
         
             
             - standardize following redirects
         
     | 
| 
       4 
4 
     | 
    
         
             
            """
         
     | 
| 
      
 5 
     | 
    
         
            +
            import contextlib
         
     | 
| 
       5 
6 
     | 
    
         
             
            import functools
         
     | 
| 
       6 
7 
     | 
    
         
             
            import typing as ta
         
     | 
| 
       7 
8 
     | 
    
         | 
| 
       8 
9 
     | 
    
         
             
            from ... import dataclasses as dc
         
     | 
| 
       9 
10 
     | 
    
         
             
            from ... import lang
         
     | 
| 
      
 11 
     | 
    
         
            +
            from ...io.buffers import ReadableListBuffer
         
     | 
| 
       10 
12 
     | 
    
         
             
            from ..headers import HttpHeaders
         
     | 
| 
      
 13 
     | 
    
         
            +
            from .asyncs import AsyncHttpClient
         
     | 
| 
      
 14 
     | 
    
         
            +
            from .asyncs import AsyncStreamHttpResponse
         
     | 
| 
      
 15 
     | 
    
         
            +
            from .base import HttpClientContext
         
     | 
| 
       11 
16 
     | 
    
         
             
            from .base import HttpClientError
         
     | 
| 
       12 
17 
     | 
    
         
             
            from .base import HttpRequest
         
     | 
| 
       13 
18 
     | 
    
         
             
            from .sync import HttpClient
         
     | 
| 
         @@ -27,17 +32,29 @@ class HttpxHttpClient(HttpClient): 
     | 
|
| 
       27 
32 
     | 
    
         
             
                @dc.dataclass(frozen=True)
         
     | 
| 
       28 
33 
     | 
    
         
             
                class _StreamAdapter:
         
     | 
| 
       29 
34 
     | 
    
         
             
                    it: ta.Iterator[bytes]
         
     | 
| 
      
 35 
     | 
    
         
            +
                    buf: ReadableListBuffer = dc.field(default_factory=ReadableListBuffer)
         
     | 
| 
       30 
36 
     | 
    
         | 
| 
       31 
     | 
    
         
            -
                    def  
     | 
| 
      
 37 
     | 
    
         
            +
                    def read1(self, /, n: int = -1) -> bytes:
         
     | 
| 
       32 
38 
     | 
    
         
             
                        if n < 0:
         
     | 
| 
       33 
     | 
    
         
            -
                             
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
      
 39 
     | 
    
         
            +
                            if (b := self.buf.read(n)) is not None:
         
     | 
| 
      
 40 
     | 
    
         
            +
                                return b
         
     | 
| 
       35 
41 
     | 
    
         
             
                            try:
         
     | 
| 
       36 
42 
     | 
    
         
             
                                return next(self.it)
         
     | 
| 
       37 
43 
     | 
    
         
             
                            except StopIteration:
         
     | 
| 
       38 
44 
     | 
    
         
             
                                return b''
         
     | 
| 
       39 
45 
     | 
    
         | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
      
 46 
     | 
    
         
            +
                        else:
         
     | 
| 
      
 47 
     | 
    
         
            +
                            while len(self.buf) < n:
         
     | 
| 
      
 48 
     | 
    
         
            +
                                try:
         
     | 
| 
      
 49 
     | 
    
         
            +
                                    b = next(self.it)
         
     | 
| 
      
 50 
     | 
    
         
            +
                                except StopIteration:
         
     | 
| 
      
 51 
     | 
    
         
            +
                                    b = b''
         
     | 
| 
      
 52 
     | 
    
         
            +
                                if not b:
         
     | 
| 
      
 53 
     | 
    
         
            +
                                    return self.buf.read() or b''
         
     | 
| 
      
 54 
     | 
    
         
            +
                                self.buf.feed(b)
         
     | 
| 
      
 55 
     | 
    
         
            +
                            return self.buf.read(n) or b''
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                def _stream_request(self, ctx: HttpClientContext, req: HttpRequest) -> StreamHttpResponse:
         
     | 
| 
       41 
58 
     | 
    
         
             
                    try:
         
     | 
| 
       42 
59 
     | 
    
         
             
                        resp_cm = httpx.stream(
         
     | 
| 
       43 
60 
     | 
    
         
             
                            method=req.method_or_default,
         
     | 
| 
         @@ -70,3 +87,64 @@ class HttpxHttpClient(HttpClient): 
     | 
|
| 
       70 
87 
     | 
    
         
             
                    except Exception:
         
     | 
| 
       71 
88 
     | 
    
         
             
                        resp_close()
         
     | 
| 
       72 
89 
     | 
    
         
             
                        raise
         
     | 
| 
      
 90 
     | 
    
         
            +
             
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
            ##
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
      
 95 
     | 
    
         
            +
            class HttpxAsyncHttpClient(AsyncHttpClient):
         
     | 
| 
      
 96 
     | 
    
         
            +
                @dc.dataclass(frozen=True)
         
     | 
| 
      
 97 
     | 
    
         
            +
                class _StreamAdapter:
         
     | 
| 
      
 98 
     | 
    
         
            +
                    it: ta.AsyncIterator[bytes]
         
     | 
| 
      
 99 
     | 
    
         
            +
                    buf: ReadableListBuffer = dc.field(default_factory=ReadableListBuffer)
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                    async def read1(self, /, n: int = -1) -> bytes:
         
     | 
| 
      
 102 
     | 
    
         
            +
                        if n < 0:
         
     | 
| 
      
 103 
     | 
    
         
            +
                            if (b := self.buf.read(n)) is not None:
         
     | 
| 
      
 104 
     | 
    
         
            +
                                return b
         
     | 
| 
      
 105 
     | 
    
         
            +
                            try:
         
     | 
| 
      
 106 
     | 
    
         
            +
                                return await anext(self.it)
         
     | 
| 
      
 107 
     | 
    
         
            +
                            except StopAsyncIteration:
         
     | 
| 
      
 108 
     | 
    
         
            +
                                return b''
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
                        else:
         
     | 
| 
      
 111 
     | 
    
         
            +
                            while len(self.buf) < n:
         
     | 
| 
      
 112 
     | 
    
         
            +
                                try:
         
     | 
| 
      
 113 
     | 
    
         
            +
                                    b = await anext(self.it)
         
     | 
| 
      
 114 
     | 
    
         
            +
                                except StopAsyncIteration:
         
     | 
| 
      
 115 
     | 
    
         
            +
                                    b = b''
         
     | 
| 
      
 116 
     | 
    
         
            +
                                if not b:
         
     | 
| 
      
 117 
     | 
    
         
            +
                                    return self.buf.read() or b''
         
     | 
| 
      
 118 
     | 
    
         
            +
                                self.buf.feed(b)
         
     | 
| 
      
 119 
     | 
    
         
            +
                            return self.buf.read(n) or b''
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
                async def _stream_request(self, ctx: HttpClientContext, req: HttpRequest) -> AsyncStreamHttpResponse:
         
     | 
| 
      
 122 
     | 
    
         
            +
                    es = contextlib.AsyncExitStack()
         
     | 
| 
      
 123 
     | 
    
         
            +
             
     | 
| 
      
 124 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 125 
     | 
    
         
            +
                        client = await es.enter_async_context(httpx.AsyncClient())
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                        resp = await es.enter_async_context(client.stream(
         
     | 
| 
      
 128 
     | 
    
         
            +
                            method=req.method_or_default,
         
     | 
| 
      
 129 
     | 
    
         
            +
                            url=req.url,
         
     | 
| 
      
 130 
     | 
    
         
            +
                            headers=req.headers_ or None,  # type: ignore
         
     | 
| 
      
 131 
     | 
    
         
            +
                            content=req.data,
         
     | 
| 
      
 132 
     | 
    
         
            +
                            timeout=req.timeout_s,
         
     | 
| 
      
 133 
     | 
    
         
            +
                        ))
         
     | 
| 
      
 134 
     | 
    
         
            +
             
     | 
| 
      
 135 
     | 
    
         
            +
                        return AsyncStreamHttpResponse(
         
     | 
| 
      
 136 
     | 
    
         
            +
                            status=resp.status_code,
         
     | 
| 
      
 137 
     | 
    
         
            +
                            headers=HttpHeaders(resp.headers.raw),
         
     | 
| 
      
 138 
     | 
    
         
            +
                            request=req,
         
     | 
| 
      
 139 
     | 
    
         
            +
                            underlying=resp,
         
     | 
| 
      
 140 
     | 
    
         
            +
                            stream=self._StreamAdapter(resp.aiter_bytes()),
         
     | 
| 
      
 141 
     | 
    
         
            +
                            _closer=es.aclose,
         
     | 
| 
      
 142 
     | 
    
         
            +
                        )
         
     | 
| 
      
 143 
     | 
    
         
            +
             
     | 
| 
      
 144 
     | 
    
         
            +
                    except httpx.HTTPError as e:
         
     | 
| 
      
 145 
     | 
    
         
            +
                        await es.aclose()
         
     | 
| 
      
 146 
     | 
    
         
            +
                        raise HttpClientError from e
         
     | 
| 
      
 147 
     | 
    
         
            +
             
     | 
| 
      
 148 
     | 
    
         
            +
                    except Exception:
         
     | 
| 
      
 149 
     | 
    
         
            +
                        await es.aclose()
         
     | 
| 
      
 150 
     | 
    
         
            +
                        raise
         
     | 
| 
         @@ -0,0 +1,178 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # ruff: noqa: UP007 UP043 UP045
         
     | 
| 
      
 2 
     | 
    
         
            +
            # @omlish-lite
         
     | 
| 
      
 3 
     | 
    
         
            +
            """
         
     | 
| 
      
 4 
     | 
    
         
            +
            TODO:
         
     | 
| 
      
 5 
     | 
    
         
            +
             - redirect
         
     | 
| 
      
 6 
     | 
    
         
            +
              - referrer header?
         
     | 
| 
      
 7 
     | 
    
         
            +
              - non-forwarded headers, host check, etc lol
         
     | 
| 
      
 8 
     | 
    
         
            +
             - 'check' kw becomes StatusCheckingMiddleware?
         
     | 
| 
      
 9 
     | 
    
         
            +
            """
         
     | 
| 
      
 10 
     | 
    
         
            +
            import dataclasses as dc
         
     | 
| 
      
 11 
     | 
    
         
            +
            import typing as ta
         
     | 
| 
      
 12 
     | 
    
         
            +
            import urllib.parse
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            from ...lite.abstract import Abstract
         
     | 
| 
      
 15 
     | 
    
         
            +
            from ...lite.check import check
         
     | 
| 
      
 16 
     | 
    
         
            +
            from ..urls import parsed_url_replace
         
     | 
| 
      
 17 
     | 
    
         
            +
            from .asyncs import AsyncHttpClient
         
     | 
| 
      
 18 
     | 
    
         
            +
            from .asyncs import AsyncStreamHttpResponse
         
     | 
| 
      
 19 
     | 
    
         
            +
            from .base import BaseHttpClient
         
     | 
| 
      
 20 
     | 
    
         
            +
            from .base import BaseHttpResponse
         
     | 
| 
      
 21 
     | 
    
         
            +
            from .base import HttpClientContext
         
     | 
| 
      
 22 
     | 
    
         
            +
            from .base import HttpClientError
         
     | 
| 
      
 23 
     | 
    
         
            +
            from .base import HttpRequest
         
     | 
| 
      
 24 
     | 
    
         
            +
            from .sync import HttpClient
         
     | 
| 
      
 25 
     | 
    
         
            +
            from .sync import StreamHttpResponse
         
     | 
| 
      
 26 
     | 
    
         
            +
            from .sync import close_response
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            BaseHttpClientT = ta.TypeVar('BaseHttpClientT', bound=BaseHttpClient)
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
            ##
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
            class HttpClientMiddleware(Abstract):
         
     | 
| 
      
 36 
     | 
    
         
            +
                def process_request(
         
     | 
| 
      
 37 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 38 
     | 
    
         
            +
                        ctx: HttpClientContext,
         
     | 
| 
      
 39 
     | 
    
         
            +
                        req: HttpRequest,
         
     | 
| 
      
 40 
     | 
    
         
            +
                ) -> HttpRequest:
         
     | 
| 
      
 41 
     | 
    
         
            +
                    return req
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                def process_response(
         
     | 
| 
      
 44 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 45 
     | 
    
         
            +
                        ctx: HttpClientContext,
         
     | 
| 
      
 46 
     | 
    
         
            +
                        req: HttpRequest,
         
     | 
| 
      
 47 
     | 
    
         
            +
                        resp: BaseHttpResponse,
         
     | 
| 
      
 48 
     | 
    
         
            +
                ) -> ta.Union[BaseHttpResponse, HttpRequest]:
         
     | 
| 
      
 49 
     | 
    
         
            +
                    return resp
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
            class AbstractMiddlewareHttpClient(Abstract, ta.Generic[BaseHttpClientT]):
         
     | 
| 
      
 53 
     | 
    
         
            +
                def __init__(
         
     | 
| 
      
 54 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 55 
     | 
    
         
            +
                        client: BaseHttpClientT,
         
     | 
| 
      
 56 
     | 
    
         
            +
                        middlewares: ta.Iterable[HttpClientMiddleware],
         
     | 
| 
      
 57 
     | 
    
         
            +
                ) -> None:
         
     | 
| 
      
 58 
     | 
    
         
            +
                    super().__init__()
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
                    self._client = client
         
     | 
| 
      
 61 
     | 
    
         
            +
                    self._middlewares = list(middlewares)
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                def _process_request(
         
     | 
| 
      
 64 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 65 
     | 
    
         
            +
                        ctx: HttpClientContext,
         
     | 
| 
      
 66 
     | 
    
         
            +
                        req: HttpRequest,
         
     | 
| 
      
 67 
     | 
    
         
            +
                ) -> HttpRequest:
         
     | 
| 
      
 68 
     | 
    
         
            +
                    for mw in self._middlewares:
         
     | 
| 
      
 69 
     | 
    
         
            +
                        req = mw.process_request(ctx, req)
         
     | 
| 
      
 70 
     | 
    
         
            +
                    return req
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
                def _process_response(
         
     | 
| 
      
 73 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 74 
     | 
    
         
            +
                        ctx: HttpClientContext,
         
     | 
| 
      
 75 
     | 
    
         
            +
                        req: HttpRequest,
         
     | 
| 
      
 76 
     | 
    
         
            +
                        resp: BaseHttpResponse,
         
     | 
| 
      
 77 
     | 
    
         
            +
                ) -> ta.Union[BaseHttpResponse, HttpRequest]:
         
     | 
| 
      
 78 
     | 
    
         
            +
                    for mw in self._middlewares:
         
     | 
| 
      
 79 
     | 
    
         
            +
                        nxt = mw.process_response(ctx, req, resp)
         
     | 
| 
      
 80 
     | 
    
         
            +
                        if isinstance(nxt, HttpRequest):
         
     | 
| 
      
 81 
     | 
    
         
            +
                            return nxt
         
     | 
| 
      
 82 
     | 
    
         
            +
                        else:
         
     | 
| 
      
 83 
     | 
    
         
            +
                            resp = nxt
         
     | 
| 
      
 84 
     | 
    
         
            +
                    return resp
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
            class MiddlewareHttpClient(AbstractMiddlewareHttpClient[HttpClient], HttpClient):
         
     | 
| 
      
 88 
     | 
    
         
            +
                def _stream_request(self, ctx: HttpClientContext, req: HttpRequest) -> StreamHttpResponse:
         
     | 
| 
      
 89 
     | 
    
         
            +
                    while True:
         
     | 
| 
      
 90 
     | 
    
         
            +
                        req = self._process_request(ctx, req)
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
                        resp = self._client.stream_request(req, context=ctx)
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
                        try:
         
     | 
| 
      
 95 
     | 
    
         
            +
                            out = self._process_response(ctx, req, resp)
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
                            if isinstance(out, HttpRequest):
         
     | 
| 
      
 98 
     | 
    
         
            +
                                close_response(resp)
         
     | 
| 
      
 99 
     | 
    
         
            +
                                req = out
         
     | 
| 
      
 100 
     | 
    
         
            +
                                continue
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                            elif isinstance(out, StreamHttpResponse):
         
     | 
| 
      
 103 
     | 
    
         
            +
                                return out
         
     | 
| 
      
 104 
     | 
    
         
            +
             
     | 
| 
      
 105 
     | 
    
         
            +
                            else:
         
     | 
| 
      
 106 
     | 
    
         
            +
                                raise TypeError(out)  # noqa
         
     | 
| 
      
 107 
     | 
    
         
            +
             
     | 
| 
      
 108 
     | 
    
         
            +
                        except Exception:
         
     | 
| 
      
 109 
     | 
    
         
            +
                            close_response(resp)
         
     | 
| 
      
 110 
     | 
    
         
            +
                            raise
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
                    raise RuntimeError
         
     | 
| 
      
 113 
     | 
    
         
            +
             
     | 
| 
      
 114 
     | 
    
         
            +
             
     | 
| 
      
 115 
     | 
    
         
            +
            class MiddlewareAsyncHttpClient(AbstractMiddlewareHttpClient[AsyncHttpClient], AsyncHttpClient):
         
     | 
| 
      
 116 
     | 
    
         
            +
                def _stream_request(self, ctx: HttpClientContext, req: HttpRequest) -> ta.Awaitable[AsyncStreamHttpResponse]:
         
     | 
| 
      
 117 
     | 
    
         
            +
                    return self._client.stream_request(self._process_request(ctx, req))
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
             
     | 
| 
      
 120 
     | 
    
         
            +
            ##
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
             
     | 
| 
      
 123 
     | 
    
         
            +
            class TooManyRedirectsHttpClientError(HttpClientError):
         
     | 
| 
      
 124 
     | 
    
         
            +
                pass
         
     | 
| 
      
 125 
     | 
    
         
            +
             
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
            class RedirectHandlingHttpClientMiddleware(HttpClientMiddleware):
         
     | 
| 
      
 128 
     | 
    
         
            +
                DEFAULT_MAX_REDIRECTS: ta.ClassVar[int] = 5
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
      
 130 
     | 
    
         
            +
                def __init__(
         
     | 
| 
      
 131 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 132 
     | 
    
         
            +
                        *,
         
     | 
| 
      
 133 
     | 
    
         
            +
                        max_redirects: ta.Optional[int] = None,
         
     | 
| 
      
 134 
     | 
    
         
            +
                ) -> None:
         
     | 
| 
      
 135 
     | 
    
         
            +
                    super().__init__()
         
     | 
| 
      
 136 
     | 
    
         
            +
             
     | 
| 
      
 137 
     | 
    
         
            +
                    if max_redirects is None:
         
     | 
| 
      
 138 
     | 
    
         
            +
                        max_redirects = self.DEFAULT_MAX_REDIRECTS
         
     | 
| 
      
 139 
     | 
    
         
            +
                    self._max_redirects = max_redirects
         
     | 
| 
      
 140 
     | 
    
         
            +
             
     | 
| 
      
 141 
     | 
    
         
            +
                @dc.dataclass()
         
     | 
| 
      
 142 
     | 
    
         
            +
                class _State:
         
     | 
| 
      
 143 
     | 
    
         
            +
                    num_redirects: int = 0
         
     | 
| 
      
 144 
     | 
    
         
            +
             
     | 
| 
      
 145 
     | 
    
         
            +
                def _get_state(self, ctx: HttpClientContext) -> _State:
         
     | 
| 
      
 146 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 147 
     | 
    
         
            +
                        return ctx._dct[self._State]  # noqa
         
     | 
| 
      
 148 
     | 
    
         
            +
                    except KeyError:
         
     | 
| 
      
 149 
     | 
    
         
            +
                        ret = ctx._dct[self._State] = self._State()  # noqa
         
     | 
| 
      
 150 
     | 
    
         
            +
                        return ret
         
     | 
| 
      
 151 
     | 
    
         
            +
             
     | 
| 
      
 152 
     | 
    
         
            +
                def process_response(
         
     | 
| 
      
 153 
     | 
    
         
            +
                        self,
         
     | 
| 
      
 154 
     | 
    
         
            +
                        ctx: HttpClientContext,
         
     | 
| 
      
 155 
     | 
    
         
            +
                        req: HttpRequest,
         
     | 
| 
      
 156 
     | 
    
         
            +
                        resp: BaseHttpResponse,
         
     | 
| 
      
 157 
     | 
    
         
            +
                ) -> ta.Union[BaseHttpResponse, HttpRequest]:  # noqa
         
     | 
| 
      
 158 
     | 
    
         
            +
                    if resp.status == 302:
         
     | 
| 
      
 159 
     | 
    
         
            +
                        st = self._get_state(ctx)
         
     | 
| 
      
 160 
     | 
    
         
            +
                        if st.num_redirects >= self._max_redirects:
         
     | 
| 
      
 161 
     | 
    
         
            +
                            raise TooManyRedirectsHttpClientError
         
     | 
| 
      
 162 
     | 
    
         
            +
                        st.num_redirects += 1
         
     | 
| 
      
 163 
     | 
    
         
            +
             
     | 
| 
      
 164 
     | 
    
         
            +
                        rd_url = check.not_none(resp.headers).single_str_dct['location']
         
     | 
| 
      
 165 
     | 
    
         
            +
             
     | 
| 
      
 166 
     | 
    
         
            +
                        rd_purl = urllib.parse.urlparse(rd_url)
         
     | 
| 
      
 167 
     | 
    
         
            +
                        if not rd_purl.netloc:
         
     | 
| 
      
 168 
     | 
    
         
            +
                            rq_purl = urllib.parse.urlparse(req.url)
         
     | 
| 
      
 169 
     | 
    
         
            +
                            rd_purl = parsed_url_replace(
         
     | 
| 
      
 170 
     | 
    
         
            +
                                rd_purl,
         
     | 
| 
      
 171 
     | 
    
         
            +
                                scheme=rq_purl.scheme,
         
     | 
| 
      
 172 
     | 
    
         
            +
                                netloc=rq_purl.netloc,
         
     | 
| 
      
 173 
     | 
    
         
            +
                            )
         
     | 
| 
      
 174 
     | 
    
         
            +
                            rd_url = urllib.parse.urlunparse(rd_purl)
         
     | 
| 
      
 175 
     | 
    
         
            +
             
     | 
| 
      
 176 
     | 
    
         
            +
                        return dc.replace(req, url=rd_url)
         
     | 
| 
      
 177 
     | 
    
         
            +
             
     | 
| 
      
 178 
     | 
    
         
            +
                    return resp
         
     |