Archived
refactor: 删除 aiohttp_compat 垫片,music_api 改走原生 core.http_client
core/aiohttp_compat 是「同步转异步」迁移期的 requests 同形异步垫片:它存在的 唯一理由是让既有 `requests.post/get + raise_for_status()` 风格的代码以最小 改动迁到异步。其两个调用方(music_api/api.py、music_api/client.py)**本就已是 async def**——垫片只剩「保留 requests 语义」这一层包装,属于迁移残留。 改动: - api.py:23 处 requests.post/get → await core.http_client.request()。 该文件每处都在请求后立刻 raise_for_status() 才取 .json(),从不把 4xx 当数据读, 故「_once 对非 2xx 直接抛 UpstreamHTTPError」与旧语义等价(原 raise_for_status 抛的 HTTPError 也一律落入 RequestException 分支); except requests.RequestException → except UpstreamError。 - client.py:两个自管重试的方法改用新公开的 core.http_client.request_once() (单次、不重试),保留原有「5xx 退避重试一次 / 4xx 立即失败」的分流—— 把 status_code>=500 的判断换成 UpstreamHTTPError.status,行为逐条对齐 (4xx=风控不重试,网络/5xx 重试一次)。响应体上限交由客户端 max_bytes 统一把关。 post_request_full 返回类型改为 HTTPResult(qr.py 用到的 .text/.cookies/.headers 三者在 HTTPResult 上齐备)。 - core/http_client:_once 更名并公开为 request_once(自管重试的调用方需要它)。 注:这是为「删垫片」而补的唯一一个小口子,不是新增抽象。 注意一处**有意的行为变化**:api.py 原经垫片请求零重试,现在 request() 自带 「网络异常/5xx 退避重试一次、4xx 不重试」(core.http_client 既定口径)。 429/460 属 4xx 不重试,不加剧风控。 - 删除 core/aiohttp_compat.py;web/app.py 去掉其 close();pyproject 去掉其 per-file-ignore;core/http_errors.py 注释更正为「仅供 core.sync_http」。 - 测试:test_aiohttp_compat.py 拆解为 test_async_bridge.py(只留桥的两条契约, 垫片自身的 4 条随垫片消亡);test_search_music / test_playlist_detail 的替身 边界从 api.requests.post 换成 api._http.request,异常类型换成 UpstreamNetworkError。 门禁:ruff 全过 / pyright 0-0 / pytest 383 项全绿(0 失败;383=387-6+2,账面吻合)。
This commit is contained in:
1 parent
b089dadac2
commit
65adfc9089
13 files changed
+139
-348
No files matched your search
+34
-27
@@ -7,7 +7,13 @@
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
from core import aiohttp_compat as requests # 异步 HTTP 垫片(requests 同形 API)
|
||||
from core.http_client import (
|
||||
HTTPResult,
|
||||
UpstreamError,
|
||||
UpstreamHTTPError,
|
||||
UpstreamTooLarge,
|
||||
)
|
||||
from core.http_client import client as _http
|
||||
|
||||
from .constants import (
|
||||
APIConstants,
|
||||
@@ -41,7 +47,12 @@ class HTTPClient:
|
||||
async def post_request(url: str, params: str, cookies: dict[str, str]) -> str:
|
||||
"""发送POST请求并返回文本响应。
|
||||
网络异常/5xx 做一次 1 秒退避重试(全站 API 调用此前零重试,
|
||||
上游抖动即整首失败);4xx(429/460 风控)不重试——立即重试会加剧风控。"""
|
||||
上游抖动即整首失败);4xx(429/460 风控)不重试——立即重试会加剧风控。
|
||||
|
||||
走 core.http_client.request_once(单次、不重试):重试策略由本函数自管,
|
||||
非 2xx 由 request_once 抛 UpstreamHTTPError,按 .status 分流——
|
||||
与原先「垫片只在 raise_for_status() 时才抛 HTTPError」的分流等价。
|
||||
"""
|
||||
headers = {
|
||||
'User-Agent': APIConstants.USER_AGENT,
|
||||
'Referer': APIConstants.REFERER,
|
||||
@@ -53,20 +64,21 @@ class HTTPClient:
|
||||
last_err = None
|
||||
for attempt in range(2):
|
||||
try:
|
||||
response = await requests.post(url, headers=headers, cookies=request_cookies,
|
||||
data={"params": params}, timeout=30)
|
||||
if response.status_code >= 500 and attempt == 0:
|
||||
response = await _http.request_once(
|
||||
'POST', url, headers=headers, cookies=request_cookies,
|
||||
data={"params": params}, timeout=30)
|
||||
return response.text
|
||||
except UpstreamHTTPError as e:
|
||||
if e.status >= 500 and attempt == 0:
|
||||
await asyncio.sleep(1.0)
|
||||
continue
|
||||
response.raise_for_status()
|
||||
if len(response.content) > _MAX_UPSTREAM_JSON_BYTES:
|
||||
raise APIException("上游响应体异常(超过大小上限)")
|
||||
return response.text
|
||||
except requests.HTTPError as e:
|
||||
# 4xx(429/460 风控)不重试——原实现 HTTPError 落入
|
||||
# 下方 RequestException 分支被重试一次,与 意图相反
|
||||
# 下方异常分支被重试一次,与意图相反
|
||||
raise APIException(f"HTTP请求失败: {e}") from e
|
||||
except requests.RequestException as e:
|
||||
except UpstreamTooLarge as e:
|
||||
# 响应体超限是确定性失败,重试无意义
|
||||
raise APIException("上游响应体异常(超过大小上限)") from e
|
||||
except UpstreamError as e:
|
||||
last_err = e
|
||||
if attempt == 0:
|
||||
await asyncio.sleep(1.0)
|
||||
@@ -74,7 +86,7 @@ class HTTPClient:
|
||||
raise APIException(f"HTTP请求失败: {last_err}")
|
||||
|
||||
@staticmethod
|
||||
async def post_request_full(url: str, params: str, cookies: dict[str, str]) -> requests.Response:
|
||||
async def post_request_full(url: str, params: str, cookies: dict[str, str]) -> HTTPResult:
|
||||
"""发送POST请求并返回完整响应对象(同 post_request 的有界重试)"""
|
||||
headers = {
|
||||
'User-Agent': APIConstants.USER_AGENT,
|
||||
@@ -87,26 +99,21 @@ class HTTPClient:
|
||||
last_err = None
|
||||
for attempt in range(2):
|
||||
try:
|
||||
response = await requests.post(url, headers=headers, cookies=request_cookies,
|
||||
data={"params": params}, timeout=30)
|
||||
if response.status_code >= 500 and attempt == 0:
|
||||
response = await _http.request_once(
|
||||
'POST', url, headers=headers, cookies=request_cookies,
|
||||
data={"params": params}, timeout=30)
|
||||
return response
|
||||
except UpstreamHTTPError as e:
|
||||
if e.status >= 500 and attempt == 0:
|
||||
await asyncio.sleep(1.0)
|
||||
continue
|
||||
response.raise_for_status()
|
||||
if len(response.content) > _MAX_UPSTREAM_JSON_BYTES:
|
||||
raise APIException("上游响应体异常(超过大小上限)")
|
||||
return response
|
||||
except requests.HTTPError as e:
|
||||
# 4xx 不重试(同上)
|
||||
raise APIException(f"HTTP请求失败: {e}") from e
|
||||
except requests.RequestException as e:
|
||||
except UpstreamTooLarge as e:
|
||||
raise APIException("上游响应体异常(超过大小上限)") from e
|
||||
except UpstreamError as e:
|
||||
last_err = e
|
||||
if attempt == 0:
|
||||
await asyncio.sleep(1.0)
|
||||
continue
|
||||
raise APIException(f"HTTP请求失败: {last_err}")
|
||||
|
||||
|
||||
# 上游 JSON 响应体上限——requests 已全量入内存,此处拦截的是
|
||||
# 异常/被劫持上游的超大 body 进入后续 .json() 解析与传播(音频/封面已有字节预算)
|
||||
_MAX_UPSTREAM_JSON_BYTES = 8 * 1024 * 1024
|
||||
Reference in new issue
Block a user