2023-09-25 22:52:29 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
import warnings
|
|
|
|
import json
|
|
|
|
import asyncio
|
2023-10-02 00:04:22 +00:00
|
|
|
from functools import partialmethod
|
|
|
|
from asyncio import Future, Queue
|
2023-10-07 17:00:45 +00:00
|
|
|
from typing import AsyncGenerator, Union, Optional
|
2023-09-25 22:52:29 +00:00
|
|
|
|
2023-10-02 00:04:22 +00:00
|
|
|
from curl_cffi.requests import AsyncSession, Response
|
2023-10-01 18:29:57 +00:00
|
|
|
import curl_cffi
|
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
is_newer_0_5_8: bool = hasattr(AsyncSession, "_set_cookies") or hasattr(curl_cffi.requests.Cookies, "get_cookies_for_curl")
|
|
|
|
is_newer_0_5_9: bool = hasattr(curl_cffi.AsyncCurl, "remove_handle")
|
|
|
|
is_newer_0_5_10: bool = hasattr(AsyncSession, "release_curl")
|
|
|
|
|
2023-09-25 22:52:29 +00:00
|
|
|
|
|
|
|
class StreamResponse:
|
2023-10-07 08:17:43 +00:00
|
|
|
def __init__(self, inner: Response, queue: Queue[bytes]) -> None:
|
|
|
|
self.inner: Response = inner
|
|
|
|
self.queue: Queue[bytes] = queue
|
2023-10-02 00:04:22 +00:00
|
|
|
self.request = inner.request
|
2023-10-07 08:17:43 +00:00
|
|
|
self.status_code: int = inner.status_code
|
|
|
|
self.reason: str = inner.reason
|
|
|
|
self.ok: bool = inner.ok
|
2023-09-26 08:03:37 +00:00
|
|
|
self.headers = inner.headers
|
|
|
|
self.cookies = inner.cookies
|
2023-09-25 22:52:29 +00:00
|
|
|
|
|
|
|
async def text(self) -> str:
|
2023-10-07 08:17:43 +00:00
|
|
|
content: bytes = await self.read()
|
2023-09-25 22:52:29 +00:00
|
|
|
return content.decode()
|
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
def raise_for_status(self) -> None:
|
2023-09-25 22:52:29 +00:00
|
|
|
if not self.ok:
|
|
|
|
raise RuntimeError(f"HTTP Error {self.status_code}: {self.reason}")
|
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
async def json(self, **kwargs) -> dict:
|
2023-10-02 00:04:22 +00:00
|
|
|
return json.loads(await self.read(), **kwargs)
|
2023-10-07 08:17:43 +00:00
|
|
|
|
2023-10-07 17:00:45 +00:00
|
|
|
async def iter_lines(
|
|
|
|
self, chunk_size: Optional[int] = None, decode_unicode: bool = False, delimiter: Optional[str] = None
|
|
|
|
) -> AsyncGenerator[bytes, None]:
|
|
|
|
"""
|
|
|
|
Copied from: https://requests.readthedocs.io/en/latest/_modules/requests/models/
|
|
|
|
which is under the License: Apache 2.0
|
|
|
|
"""
|
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
pending: bytes = None
|
2023-10-02 00:04:22 +00:00
|
|
|
|
|
|
|
async for chunk in self.iter_content(
|
|
|
|
chunk_size=chunk_size, decode_unicode=decode_unicode
|
|
|
|
):
|
|
|
|
if pending is not None:
|
|
|
|
chunk = pending + chunk
|
|
|
|
if delimiter:
|
|
|
|
lines = chunk.split(delimiter)
|
|
|
|
else:
|
|
|
|
lines = chunk.splitlines()
|
|
|
|
if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]:
|
|
|
|
pending = lines.pop()
|
|
|
|
else:
|
|
|
|
pending = None
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
yield line
|
|
|
|
|
|
|
|
if pending is not None:
|
|
|
|
yield pending
|
|
|
|
|
2023-10-07 17:00:45 +00:00
|
|
|
async def iter_content(
|
|
|
|
self, chunk_size: Optional[int] = None, decode_unicode: bool = False
|
|
|
|
) -> AsyncGenerator[bytes, None]:
|
2023-10-02 00:04:22 +00:00
|
|
|
if chunk_size:
|
|
|
|
warnings.warn("chunk_size is ignored, there is no way to tell curl that.")
|
|
|
|
if decode_unicode:
|
|
|
|
raise NotImplementedError()
|
|
|
|
while True:
|
|
|
|
chunk = await self.queue.get()
|
|
|
|
if chunk is None:
|
|
|
|
return
|
|
|
|
yield chunk
|
|
|
|
|
|
|
|
async def read(self) -> bytes:
|
|
|
|
return b"".join([chunk async for chunk in self.iter_content()])
|
2023-09-25 22:52:29 +00:00
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
|
2023-09-25 22:52:29 +00:00
|
|
|
class StreamRequest:
|
2023-10-07 17:00:45 +00:00
|
|
|
def __init__(self, session: AsyncSession, method: str, url: str, **kwargs: Union[bool, int, str]) -> None:
|
2023-10-07 08:17:43 +00:00
|
|
|
self.session: AsyncSession = session
|
|
|
|
self.loop: asyncio.AbstractEventLoop = session.loop if session.loop else asyncio.get_running_loop()
|
|
|
|
self.queue: Queue[bytes] = Queue()
|
|
|
|
self.method: str = method
|
|
|
|
self.url: str = url
|
|
|
|
self.options: dict = kwargs
|
2023-10-07 17:00:45 +00:00
|
|
|
self.handle: Optional[curl_cffi.AsyncCurl] = None
|
2023-10-07 08:17:43 +00:00
|
|
|
|
|
|
|
def _on_content(self, data: bytes) -> None:
|
2023-09-25 22:52:29 +00:00
|
|
|
if not self.enter.done():
|
|
|
|
self.enter.set_result(None)
|
2023-10-02 00:04:22 +00:00
|
|
|
self.queue.put_nowait(data)
|
2023-09-25 22:52:29 +00:00
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
def _on_done(self, task: Future) -> None:
|
2023-10-01 04:38:11 +00:00
|
|
|
if not self.enter.done():
|
|
|
|
self.enter.set_result(None)
|
2023-10-02 00:04:22 +00:00
|
|
|
self.queue.put_nowait(None)
|
2023-09-25 22:52:29 +00:00
|
|
|
|
2023-10-02 15:01:15 +00:00
|
|
|
self.loop.call_soon(self.release_curl)
|
2023-10-02 00:04:22 +00:00
|
|
|
|
|
|
|
async def fetch(self) -> StreamResponse:
|
|
|
|
if self.handle:
|
|
|
|
raise RuntimeError("Request already started")
|
2023-10-07 08:17:43 +00:00
|
|
|
self.curl: curl_cffi.AsyncCurl = await self.session.pop_curl()
|
|
|
|
self.enter: asyncio.Future = self.loop.create_future()
|
2023-10-01 18:29:57 +00:00
|
|
|
if is_newer_0_5_10:
|
|
|
|
request, _, header_buffer, _, _ = self.session._set_curl_options(
|
|
|
|
self.curl,
|
|
|
|
self.method,
|
|
|
|
self.url,
|
2023-10-02 00:04:22 +00:00
|
|
|
content_callback=self._on_content,
|
2023-10-01 18:29:57 +00:00
|
|
|
**self.options
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
request, _, header_buffer = self.session._set_curl_options(
|
|
|
|
self.curl,
|
|
|
|
self.method,
|
|
|
|
self.url,
|
2023-10-02 00:04:22 +00:00
|
|
|
content_callback=self._on_content,
|
2023-10-01 18:29:57 +00:00
|
|
|
**self.options
|
|
|
|
)
|
2023-10-01 04:38:11 +00:00
|
|
|
if is_newer_0_5_9:
|
2023-10-07 08:17:43 +00:00
|
|
|
self.handle = self.session.acurl.add_handle(self.curl)
|
2023-10-01 04:38:11 +00:00
|
|
|
else:
|
|
|
|
await self.session.acurl.add_handle(self.curl, False)
|
|
|
|
self.handle = self.session.acurl._curl2future[self.curl]
|
2023-10-02 00:04:22 +00:00
|
|
|
self.handle.add_done_callback(self._on_done)
|
|
|
|
# Wait for headers
|
2023-09-25 22:52:29 +00:00
|
|
|
await self.enter
|
2023-10-02 00:04:22 +00:00
|
|
|
# Raise exceptions
|
|
|
|
if self.handle.done():
|
|
|
|
self.handle.result()
|
2023-10-01 08:36:09 +00:00
|
|
|
if is_newer_0_5_8:
|
2023-10-01 04:38:11 +00:00
|
|
|
response = self.session._parse_response(self.curl, _, header_buffer)
|
|
|
|
response.request = request
|
|
|
|
else:
|
|
|
|
response = self.session._parse_response(self.curl, request, _, header_buffer)
|
2023-10-07 17:00:45 +00:00
|
|
|
return StreamResponse(response, self.queue)
|
2023-10-07 08:17:43 +00:00
|
|
|
|
2023-10-02 00:04:22 +00:00
|
|
|
async def __aenter__(self) -> StreamResponse:
|
|
|
|
return await self.fetch()
|
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
async def __aexit__(self, *args) -> None:
|
2023-10-02 15:01:15 +00:00
|
|
|
self.release_curl()
|
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
def release_curl(self) -> None:
|
2023-10-02 15:01:15 +00:00
|
|
|
if is_newer_0_5_10:
|
|
|
|
self.session.release_curl(self.curl)
|
|
|
|
return
|
|
|
|
if not self.curl:
|
|
|
|
return
|
|
|
|
self.curl.clean_after_perform()
|
|
|
|
if is_newer_0_5_9:
|
|
|
|
self.session.acurl.remove_handle(self.curl)
|
|
|
|
elif not self.handle.done() and not self.handle.cancelled():
|
|
|
|
self.session.acurl.set_result(self.curl)
|
|
|
|
self.curl.reset()
|
|
|
|
self.session.push_curl(self.curl)
|
|
|
|
self.curl = None
|
2023-10-02 00:04:22 +00:00
|
|
|
|
2023-10-07 08:17:43 +00:00
|
|
|
|
2023-10-02 00:04:22 +00:00
|
|
|
class StreamSession(AsyncSession):
|
2023-09-26 08:03:37 +00:00
|
|
|
def request(
|
2023-10-07 17:00:45 +00:00
|
|
|
self, method: str, url: str, **kwargs
|
2023-09-26 08:03:37 +00:00
|
|
|
) -> StreamRequest:
|
|
|
|
return StreamRequest(self, method, url, **kwargs)
|
2023-10-07 08:17:43 +00:00
|
|
|
|
2023-09-26 08:03:37 +00:00
|
|
|
head = partialmethod(request, "HEAD")
|
|
|
|
get = partialmethod(request, "GET")
|
|
|
|
post = partialmethod(request, "POST")
|
|
|
|
put = partialmethod(request, "PUT")
|
|
|
|
patch = partialmethod(request, "PATCH")
|
|
|
|
delete = partialmethod(request, "DELETE")
|