refactor: 预留并行修复所需的共享接口
This commit is contained in:
@@ -16,7 +16,7 @@ import httpx
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import padding
|
||||
|
||||
from app.features.download.ids import normalize_id
|
||||
from app.shared.ids import normalize_id
|
||||
from app.platform.constants import (
|
||||
APP_KEY,
|
||||
CAS_CAPTCHA,
|
||||
@@ -45,6 +45,10 @@ class NqiError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class NqiAuthError(NqiError):
|
||||
pass
|
||||
|
||||
|
||||
def rsa_encrypt(public_key_b64: str, text: str) -> str:
|
||||
der = base64.b64decode(public_key_b64)
|
||||
key = serialization.load_der_public_key(der)
|
||||
@@ -554,16 +558,32 @@ class NqiClient:
|
||||
records.extend(extra)
|
||||
return records
|
||||
|
||||
async def _request(self, method: str, url: str, *, check_auth: bool = True, **kwargs) -> httpx.Response:
|
||||
send = getattr(self.client, method.lower())
|
||||
try:
|
||||
response = await send(url, **kwargs)
|
||||
except httpx.TimeoutException as exc:
|
||||
raise NqiError("平台响应超时,请稍后重试") from exc
|
||||
except httpx.RequestError as exc:
|
||||
raise NqiError("连接失败,请稍后重试") from exc
|
||||
except RuntimeError as exc:
|
||||
if "client has been closed" in str(exc):
|
||||
raise NqiAuthError("需要重新登录") from exc
|
||||
raise
|
||||
if check_auth and (response.status_code in {401, 403} or _landed_on_login(response)):
|
||||
raise NqiAuthError("登录已失效,请重新登录")
|
||||
return response
|
||||
|
||||
async def _signed_get(self, url: str, params: dict) -> dict:
|
||||
headers = build_sign(params)
|
||||
headers["Referer"] = f"{ORIGIN}/pro-problem-warehouse-web/"
|
||||
response = await self.client.get(url, params=params, headers=headers)
|
||||
response = await self._request("GET", url, params=params, headers=headers)
|
||||
return _api_payload(response)
|
||||
|
||||
async def _signed_post(self, url: str, body: dict) -> dict:
|
||||
headers = build_sign({})
|
||||
headers["Referer"] = f"{ORIGIN}/pro-problem-warehouse-web/"
|
||||
response = await self.client.post(url, json=body, headers=headers)
|
||||
response = await self._request("POST", url, json=body, headers=headers)
|
||||
return _api_payload(response)
|
||||
|
||||
|
||||
@@ -647,9 +667,19 @@ def _media_type(response: httpx.Response) -> str:
|
||||
return raw or "image/jpeg"
|
||||
|
||||
|
||||
def _landed_on_login(response: httpx.Response) -> bool:
|
||||
url = getattr(response, "url", None)
|
||||
if url is None:
|
||||
return False
|
||||
path = getattr(url, "path", "") or ""
|
||||
if path:
|
||||
return path.rstrip("/").endswith("/cas/login")
|
||||
return "/cas/login" in str(url)
|
||||
|
||||
|
||||
def _json_or_error(response: httpx.Response, fallback: str) -> dict:
|
||||
if response.status_code in {401, 403}:
|
||||
raise NqiError("登录已失效,请重新登录")
|
||||
raise NqiAuthError("登录已失效,请重新登录")
|
||||
try:
|
||||
data = response.json()
|
||||
except json.JSONDecodeError as exc:
|
||||
|
||||
Reference in New Issue
Block a user