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:
snowy-yang committed 2026-09-28 19:17:03 +08:00
1 parent b089dadac2
commit 65adfc9089
13 files changed
+139 -348

No files matched your search

-86
View File
@@ -1,86 +0,0 @@
# -*- coding: utf-8 -*-
"""兼容垫片与同步桥测试。"""
import asyncio
import pytest
from aiohttp import web
from core import aiohttp_compat as req
from core import async_bridge
@pytest.fixture()
async def server():
app = web.Application()
async def ok(request):
return web.json_response({'code': 200, 'data': 'hi'})
async def notfound(request):
return web.json_response({'code': 404}, status=404)
async def echo_post(request):
body = await request.text()
return web.json_response({'got': body, 'ua': request.headers.get('User-Agent')})
app.router.add_get('/ok', ok)
app.router.add_get('/nf', notfound)
app.router.add_post('/echo', echo_post)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '127.0.0.1', 0)
await site.start()
port = runner.addresses[0][1]
try:
yield f'http://127.0.0.1:{port}'
finally:
await runner.cleanup()
async def test_get_json(server):
r = await req.get(server + '/ok')
assert r.status_code == 200
assert r.json() == {'code': 200, 'data': 'hi'}
assert 'code' in r.text
r.raise_for_status() # 200 不抛
async def test_4xx_does_not_raise_until_raise_for_status(server):
r = await req.get(server + '/nf')
assert r.status_code == 404 # 请求本身不抛(同 requests)
assert r.json() == {'code': 404}
with pytest.raises(req.HTTPError):
r.raise_for_status()
async def test_post_headers_forwarded(server):
r = await req.post(server + '/echo', data={'params': 'x'}, headers={'User-Agent': 'ua-test'})
body = r.json()
assert body['ua'] == 'ua-test'
assert 'params' in body['got']
async def test_network_error_raises_request_exception():
with pytest.raises(req.RequestException):
await req.get('http://127.0.0.1:1/nope', timeout=1)
def test_bridge_runs_coro_from_sync():
async def coro():
await asyncio.sleep(0.01)
return 42
assert async_bridge.run_sync(coro()) == 42
async def test_bridge_rejects_inside_loop():
async def coro():
return 1
c = coro()
try:
with pytest.raises(RuntimeError):
async_bridge.run_sync(c)
finally:
c.close() # run_sync 拒绝执行,需自行关闭以免「未 await」告警
+31
View File
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""同步 ↔ 异步桥测试。
(原 test_aiohttp_compat.py:异步 HTTP 垫片 core.aiohttp_compat 已随其调用方
music_api 全面改走 core.http_client 而删除,仅保留桥本身的行为契约。)
"""
import asyncio
import pytest
from core import async_bridge
def test_bridge_runs_coro_from_sync():
async def coro():
await asyncio.sleep(0.01)
return 42
assert async_bridge.run_sync(coro()) == 42
async def test_bridge_rejects_inside_loop():
async def coro():
return 1
c = coro()
try:
with pytest.raises(RuntimeError):
async_bridge.run_sync(c)
finally:
c.close() # run_sync 拒绝执行,需自行关闭以免「未 await」告警
+10 -15
View File
@@ -9,14 +9,15 @@
- trackIds 数与实得数不一致时显式标 incomplete/missing
按惯例:先在未改动的原始实现上跑绿,重构时这批测试即等价性验证器。
替身边界:monkeypatch ``music_api.api.requests.post``,按 URL 分流——
PLAYLIST_DETAIL_API 与 SONG_DETAIL_V3 分别给响应替身。
替身边界:monkeypatch ``music_api.api._http.request``(core.http_client 的进程级
客户端),按 URL 分流——PLAYLIST_DETAIL_API 与 SONG_DETAIL_V3 分别给响应替身。
"""
import json
from typing import Any
import pytest
from core.http_client import UpstreamNetworkError
from music_api import APIException
from music_api import api as api_mod
from music_api.api import NeteaseAPI
@@ -26,19 +27,13 @@ _API = NeteaseAPI()
class _FakeResp:
"""``requests.post`` 响应替身:json 载荷或注入异常。"""
"""请求响应替身(``HTTPResult`` 的形状子集):json 载荷或注入异常。"""
def __init__(self, payload: Any = None, *,
status_error: Exception | None = None,
json_error: Exception | None = None):
self.payload = payload
self.status_error = status_error
self.json_error = json_error
def raise_for_status(self) -> None:
if self.status_error is not None:
raise self.status_error
def json(self) -> Any:
if self.json_error is not None:
raise self.json_error
@@ -47,14 +42,14 @@ class _FakeResp:
@pytest.fixture()
def api_capture(monkeypatch):
"""按 URL 分流的 requests.post 替身:歌单详情响应固定,批次响应按序消费。"""
"""按 URL 分流的请求替身:歌单详情响应固定,批次响应按序消费。"""
calls: list[dict[str, Any]] = []
state: dict[str, Any] = {
'playlist_resp': _FakeResp({'code': 200, 'playlist': {}}),
'batch_resps': [_FakeResp({'code': 200, 'songs': []})],
}
async def _fake_post(url: str, **kw: Any):
async def _fake_request(method: str, url: str, **kw: Any):
calls.append({'url': url, 'data': kw.get('data')})
if url == APIConstants.PLAYLIST_DETAIL_API:
return state['playlist_resp']
@@ -63,7 +58,7 @@ def api_capture(monkeypatch):
raise AssertionError('批次响应耗尽')
return state['batch_resps'].pop(0)
monkeypatch.setattr(api_mod.requests, 'post', _fake_post)
monkeypatch.setattr(api_mod._http, 'request', _fake_request)
state['calls'] = calls
return state
@@ -197,10 +192,10 @@ async def test_batches_of_100(api_capture):
async def test_network_error_maps_to_request_failed(api_capture, monkeypatch):
"""网络层异常(含批次阶段的):统一转「获取歌单详情请求失败」。"""
async def _boom(url: str, **kw: Any):
raise api_mod.requests.RequestException('connection reset')
async def _boom(method: str, url: str, **kw: Any):
raise UpstreamNetworkError('connection reset')
monkeypatch.setattr(api_mod.requests, 'post', _boom)
monkeypatch.setattr(api_mod._http, 'request', _boom)
with pytest.raises(APIException, match='获取歌单详情请求失败'):
await _API.get_playlist_detail(123, {})
+16 -20
View File
@@ -8,15 +8,17 @@
拆解时改坏任意一路都不会有测试报警。
按惯例:先在未改动的原始实现上跑绿,重构时这批测试即等价性验证器。
替身边界:monkeypatch ``music_api.api.requests.post``(aiohttp_compat 垫片
的同形入口),响应替身只实现 ``raise_for_status`` / ``json`` 两个同步方法——
``search_music`` 只用到这两个。
替身边界:monkeypatch ``music_api.api._http.request``(core.http_client 的进程级
客户端),响应替身实现 ``json`` 一个同步方法——``search_music`` 只用到它。
状态码错误不再由替身经 ``raise_for_status`` 模拟:请求入口自身即对非 2xx 抛
``UpstreamHTTPError``(见 core.http_client.request_once)。
"""
import json
from typing import Any
import pytest
from core.http_client import UpstreamNetworkError
from music_api import APIException
from music_api import api as api_mod
from music_api.api import NeteaseAPI
@@ -26,19 +28,13 @@ _API = NeteaseAPI()
class _FakeResp:
"""``requests.post`` 响应替身:json 载荷或注入异常。"""
"""请求响应替身(``HTTPResult`` 的形状子集):json 载荷或注入异常。"""
def __init__(self, payload: Any = None, *,
status_error: Exception | None = None,
json_error: Exception | None = None):
self.payload = payload
self.status_error = status_error
self.json_error = json_error
def raise_for_status(self) -> None:
if self.status_error is not None:
raise self.status_error
def json(self) -> Any:
if self.json_error is not None:
raise self.json_error
@@ -47,15 +43,16 @@ class _FakeResp:
@pytest.fixture()
def post_capture(monkeypatch):
"""替换 requests.post,捕获调用参数并按用例注入响应。"""
"""替换请求入口,捕获调用参数并按用例注入响应。"""
holder: dict[str, Any] = {'resp': _FakeResp({'code': 200, 'result': {}})}
async def _fake_post(url: str, **kw: Any):
async def _fake_request(method: str, url: str, **kw: Any):
holder['method'] = method
holder['url'] = url
holder['data'] = kw.get('data')
return holder['resp']
monkeypatch.setattr(api_mod.requests, 'post', _fake_post)
monkeypatch.setattr(api_mod._http, 'request', _fake_request)
return holder
@@ -178,16 +175,15 @@ async def test_non_dict_response_raises_format_error(post_capture):
async def test_request_error_maps_to_api_exception(post_capture, monkeypatch):
"""网络层异常(垫片 RequestException 族):转"搜索请求失败"。
"""网络层异常(core.http_client 的 UpstreamError 族):转"搜索请求失败"。
注意替身抛的必须是垫片自己的 ``RequestException``——真实链路里
aiohttp.ClientError 由 aiohttp_compat 内部翻译后才冒出来,直接抛底层
异常是不存在的形状(test_request_* 相关层次见 core.http_errors)。
注意替身抛的必须是 ``UpstreamNetworkError``——真实链路里 aiohttp.ClientError
由 core.http_client 内部翻译后才冒出来,直接抛底层异常是不存在的形状。
"""
async def _boom(url: str, **kw: Any):
raise api_mod.requests.RequestException('connection reset')
async def _boom(method: str, url: str, **kw: Any):
raise UpstreamNetworkError('connection reset')
monkeypatch.setattr(api_mod.requests, 'post', _boom)
monkeypatch.setattr(api_mod._http, 'request', _boom)
with pytest.raises(APIException, match='搜索请求失败'):
await _API.search_music('测试', {}, search_type=1)