初步实现携带cookie请求

This commit is contained in:
2024-09-02 23:13:29 +08:00
parent 1cd778c2e0
commit 7901b845ea
2 changed files with 45 additions and 1 deletions
+42
View File
@@ -1,9 +1,12 @@
import json
from typing import Literal
from abc import ABC, abstractmethod
import httpx
from httpx import AsyncClient
from ..types import Target
from ..config import config
from .http import http_client
@@ -35,6 +38,45 @@ class DefaultClientManager(ClientManager):
pass
class CookieClientManager(ClientManager):
_platform_name: str
async def _choose_cookie(self, target: Target) -> dict[str, str]:
if not target:
return {}
cookies = await config.get_cookie_by_target(target, self._platform_name)
if not cookies:
return {}
cookie = sorted(cookies, key=lambda x: x.last_usage, reverse=True)[0]
return json.loads(cookie.content)
async def get_client(self, target: Target | None) -> AsyncClient:
client = http_client()
cookie = await self._choose_cookie(target)
cookies = httpx.Cookies()
if cookie:
cookies.update(cookie)
client.cookies = cookies
return client
async def get_client_for_static(self) -> AsyncClient:
pass
async def get_query_name_client(self) -> AsyncClient:
pass
async def refresh_client(self):
pass
def create_cookie_client_manager(platform_name: str) -> type[CookieClientManager]:
return type(
"CookieClientManager",
(CookieClientManager,),
{"_platform_name": platform_name},
)
class Site:
schedule_type: Literal["date", "interval", "cron"]
schedule_setting: dict