utils.network
网络请求
api_request
api_request(module: str, method: str, *, verify: bool = False, ignore_code: bool = False, process_bool: bool = True, catch_error_code: list[int] | None = None)
API请求装饰器.
用于将普通函数转换为 ApiRequest 实例的工厂函数.
| 参数 | 描述 |
|---|---|
module
|
模块名称 (e.g.
类型:
|
method
|
调用方法名称 (e.g.
类型:
|
verify
|
是否验证凭证有效性.如果为
类型:
|
ignore_code
|
是否忽略业务状态码检查.如果为
类型:
|
process_bool
|
是否转换布尔值.如果为
类型:
|
catch_error_code
|
视为成功的错误码列表.当响应 |
| 返回 | 描述 |
|---|---|
|
一个装饰器,将函数转换为返回 |
源代码位于: qqmusic_api/utils/network.py
BaseRequest
BaseRequest(common: dict[str, Any] | None = None, credential: Credential | None = None, verify: bool = False, ignore_code: bool = False)
Bases: ABC
请求基类
源代码位于: qqmusic_api/utils/network.py
build_cookies
staticmethod
build_cookies(credential: Credential) -> Cookies | None
根据凭证生成 cookies
源代码位于: qqmusic_api/utils/network.py
build_request_data
abstractmethod
build_request
统一构建请求参数
源代码位于: qqmusic_api/utils/network.py
request
async
执行请求
源代码位于: qqmusic_api/utils/network.py
ApiRequest
ApiRequest(module: str, method: str, api_func: Callable[_P, Coroutine[None, None, tuple[dict[Any, Any], Callable[[dict[str, Any]], _R]]]] | None = None, *, params: dict[str, Any] | None = None, common: dict[str, Any] | None = None, credential: Credential | None = None, verify: bool = False, ignore_code: bool = False, process_bool: bool = True, catch_error_code: list[int] | None = None)
Bases: BaseRequest, Generic[_P, _R]
API 请求处理器.
封装单个 API 请求的构建、发送和处理逻辑.
初始化 ApiRequest.
| 参数 | 描述 |
|---|---|
module
|
模块名.
类型:
|
method
|
方法名.
类型:
|
api_func
|
被装饰的原始 API 函数
类型:
|
params
|
请求参数字典. |
common
|
公共参数字典. |
credential
|
请求凭证.
类型:
|
verify
|
是否验证凭证.
类型:
|
ignore_code
|
是否忽略错误码.
类型:
|
process_bool
|
是否处理布尔值.
类型:
|
catch_error_code
|
捕获的错误码列表. |
源代码位于: qqmusic_api/utils/network.py
RequestGroup
RequestGroup(common: dict[str, Any] | None = None, credential: Credential | None = None, limit: int = 20)
Bases: BaseRequest
合并多个 API 请求,支持组级公共参数和重复模块方法处理
初始化 RequestGroup.
| 参数 | 描述 |
|---|---|
common
|
组级公共参数,将合并到每个子请求中. |
credential
|
组级凭证,将应用于所有子请求.
类型:
|
limit
|
单次请求的最大子请求数量.超过此数量会自动分批发送.
类型:
|
源代码位于: qqmusic_api/utils/network.py
add_request
add_request(request: ApiRequest[_P, _R], *args: args, **kwargs: kwargs) -> None
添加请求到组.
| 参数 | 描述 |
|---|---|
request
|
类型:
|
*args
|
传递给 API 函数的位置参数.
类型:
|
**kwargs
|
传递给 API 函数的关键字参数.
类型:
|
源代码位于: qqmusic_api/utils/network.py
build_request_data
execute
async
执行合并请求。
如果请求数量超过 limit,会自动分批执行并合并结果。
| 返回 | 描述 |
|---|---|
list[Any]
|
list[Any]: 请求结果列表 |