2023-09-25 18:52:29 -04:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-10-01 00:38:11 -04:00
|
|
|
import json, sys, asyncio
|
2023-09-26 04:03:37 -04:00
|
|
|
from functools import partialmethod
|
|
|
|
|
2023-09-25 18:52:29 -04:00
|
|
|
from aiohttp import StreamReader
|
|
|
|
from aiohttp.base_protocol import BaseProtocol
|
|
|
|
|
2023-09-26 04:03:37 -04:00
|
|
|
from curl_cffi.requests import AsyncSession as BaseSession
|
2023-09-27 13:55:58 -04:00
|
|
|
from curl_cffi.requests import Response
|
2023-10-01 00:38:11 -04:00
|
|
|
|
2023-10-01 14:29:57 -04:00
|
|
|
import curl_cffi
|
|
|
|
|
|
|
|
is_newer_0_5_8 = hasattr(BaseSession, "_set_cookies") or hasattr(curl_cffi.requests.Cookies, "get_cookies_for_curl")
|
|
|
|
is_newer_0_5_9 = hasattr(curl_cffi.AsyncCurl, "remove_handle")
|
|
|
|
is_newer_0_5_10 = hasattr(BaseSession, "release_curl")
|
2023-09-25 18:52:29 -04:00
|
|
|
|
|
|
|
class StreamResponse:
|
2023-09-27 13:55:58 -04:00
|
|
|
def __init__(self, inner: Response, content: StreamReader, request):
|
2023-09-25 18:52:29 -04:00
|
|
|
self.inner = inner
|
|
|
|
self.content = content
|
|
|
|
self.request = request
|
|
|
|
self.status_code = inner.status_code
|
|
|
|
self.reason = inner.reason
|
|
|
|
self.ok = inner.ok
|
2023-09-26 04:03:37 -04:00
|
|
|
self.headers = inner.headers
|
|
|
|
self.cookies = inner.cookies
|
2023-09-25 18:52:29 -04:00
|
|
|
|
|
|
|
async def text(self) -> str:
|
|
|
|
content = await self.content.read()
|
|
|
|
return content.decode()
|
|
|
|
|
|
|
|
def raise_for_status(self):
|
|
|
|
if not self.ok:
|
|
|
|
raise RuntimeError(f"HTTP Error {self.status_code}: {self.reason}")
|
|
|
|
|
|
|
|
async def json(self, **kwargs):
|
|
|
|
return json.loads(await self.content.read(), **kwargs)
|
|
|
|
|
|
|
|
class StreamRequest:
|
|
|
|
def __init__(self, session: AsyncSession, method: str, url: str, **kwargs):
|
|
|
|
self.session = session
|
2023-10-01 00:38:11 -04:00
|
|
|
self.loop = session.loop if session.loop else asyncio.get_running_loop()
|
2023-09-25 18:52:29 -04:00
|
|
|
self.content = StreamReader(
|
|
|
|
BaseProtocol(session.loop),
|
|
|
|
sys.maxsize,
|
|
|
|
loop=session.loop
|
|
|
|
)
|
|
|
|
self.method = method
|
|
|
|
self.url = url
|
2023-10-01 04:36:09 -04:00
|
|
|
if "proxy" in kwargs:
|
|
|
|
proxy = kwargs.pop("proxy")
|
|
|
|
if proxy:
|
|
|
|
kwargs["proxies"] = {"http": proxy, "https": proxy}
|
2023-09-25 18:52:29 -04:00
|
|
|
self.options = kwargs
|
|
|
|
|
|
|
|
def on_content(self, data):
|
|
|
|
if not self.enter.done():
|
|
|
|
self.enter.set_result(None)
|
|
|
|
self.content.feed_data(data)
|
|
|
|
|
|
|
|
def on_done(self, task):
|
2023-10-01 00:38:11 -04:00
|
|
|
if not self.enter.done():
|
|
|
|
self.enter.set_result(None)
|
2023-09-25 18:52:29 -04:00
|
|
|
self.content.feed_eof()
|
|
|
|
|
|
|
|
async def __aenter__(self) -> StreamResponse:
|
|
|
|
self.curl = await self.session.pop_curl()
|
2023-09-26 04:03:37 -04:00
|
|
|
self.enter = self.loop.create_future()
|
2023-10-01 14:29:57 -04:00
|
|
|
if is_newer_0_5_10:
|
|
|
|
request, _, header_buffer, _, _ = self.session._set_curl_options(
|
|
|
|
self.curl,
|
|
|
|
self.method,
|
|
|
|
self.url,
|
|
|
|
content_callback=self.on_content,
|
|
|
|
**self.options
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
request, _, header_buffer = self.session._set_curl_options(
|
|
|
|
self.curl,
|
|
|
|
self.method,
|
|
|
|
self.url,
|
|
|
|
content_callback=self.on_content,
|
|
|
|
**self.options
|
|
|
|
)
|
2023-10-01 00:38:11 -04:00
|
|
|
if is_newer_0_5_9:
|
|
|
|
self.handle = self.session.acurl.add_handle(self.curl)
|
|
|
|
else:
|
|
|
|
await self.session.acurl.add_handle(self.curl, False)
|
|
|
|
self.handle = self.session.acurl._curl2future[self.curl]
|
2023-09-25 18:52:29 -04:00
|
|
|
self.handle.add_done_callback(self.on_done)
|
|
|
|
await self.enter
|
2023-10-01 04:36:09 -04:00
|
|
|
if is_newer_0_5_8:
|
2023-10-01 00:38:11 -04: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-09-25 18:52:29 -04:00
|
|
|
return StreamResponse(
|
2023-10-01 00:38:11 -04:00
|
|
|
response,
|
2023-09-25 18:52:29 -04:00
|
|
|
self.content,
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
|
|
|
async def __aexit__(self, exc_type, exc, tb):
|
2023-10-01 00:38:11 -04:00
|
|
|
if not self.handle.done():
|
|
|
|
self.session.acurl.set_result(self.curl)
|
|
|
|
self.curl.clean_after_perform()
|
|
|
|
self.curl.reset()
|
2023-10-01 04:36:09 -04:00
|
|
|
self.session.push_curl(self.curl)
|
2023-09-26 04:03:37 -04:00
|
|
|
|
|
|
|
class AsyncSession(BaseSession):
|
|
|
|
def request(
|
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
url: str,
|
|
|
|
**kwargs
|
|
|
|
) -> StreamRequest:
|
|
|
|
return StreamRequest(self, method, url, **kwargs)
|
|
|
|
|
|
|
|
head = partialmethod(request, "HEAD")
|
|
|
|
get = partialmethod(request, "GET")
|
|
|
|
post = partialmethod(request, "POST")
|
|
|
|
put = partialmethod(request, "PUT")
|
|
|
|
patch = partialmethod(request, "PATCH")
|
|
|
|
delete = partialmethod(request, "DELETE")
|