跳转至

utils.network

网络请求

Api dataclass

Api(url: str = '', method: str = 'GET', module: str = '', params: dict = dict(), data: dict = dict(), headers: dict = dict(), json_body: bool = False, verify: bool = False, platform: Literal['mobile', 'desktop', 'web'] = 'mobile', ignore_code: bool = False, extra_common: dict = dict(), credential: Credential = Credential(), comment: str = '')

API 请求类

ATTRIBUTE DESCRIPTION
url

请求地址

TYPE: str

method

请求方法

TYPE: str

module

请求模块

TYPE: str

params

请求参数

TYPE: dict

data

请求数据

TYPE: dict

headers

请求头

TYPE: dict

json_body

是否使用 json 作为载荷

TYPE: bool

verify

是否验证凭据

TYPE: bool

platform

API 来源

TYPE: Literal['mobile', 'desktop', 'web']

ignore_code

是否忽略返回值 code 检验直接返回

TYPE: bool

extra_common

额外参数

TYPE: dict

credential

账号凭据

TYPE: Credential

comment

API 注释

TYPE: str

update_params

update_params(**kwargs) -> Self

更新参数

Source code in qqmusic_api/utils/network.py
def update_params(self, **kwargs) -> Self:
    """更新参数"""
    self.params.update(kwargs)
    return self

update_data

update_data(**kwargs) -> Self

更新数据

Source code in qqmusic_api/utils/network.py
def update_data(self, **kwargs) -> Self:
    """更新数据"""
    self.data.update(kwargs)
    return self

update_extra_common

update_extra_common(**kwargs) -> Self

更新额外参数

Source code in qqmusic_api/utils/network.py
def update_extra_common(self, **kwargs) -> Self:
    """更新额外参数"""
    self.extra_common.update(kwargs)
    return self

update_headers

update_headers(**kwargs) -> Self

更新请求头

Source code in qqmusic_api/utils/network.py
def update_headers(self, **kwargs) -> Self:
    """更新请求头"""
    self.headers.update(kwargs)
    return self

update_cookies

update_cookies(cookies: Cookies) -> Self

更新 Cookies

Source code in qqmusic_api/utils/network.py
def update_cookies(self, cookies: httpx.Cookies) -> Self:
    """更新 Cookies"""
    self._cookies.update(cookies)
    return self

request async

request() -> Response

发起请求

Source code in qqmusic_api/utils/network.py
async def request(self) -> httpx.Response:
    """发起请求"""
    from .. import logger

    config = self._prepare_request()

    logger.debug(
        f"发起请求: {self.url} {self.module} {self.method} {self.module} params: {self.params} data: {self.data}"
    )

    resp = await self._session.request(**config)
    if not self.ignore_code:
        resp.raise_for_status()
    self._session.cookies.clear()
    return resp

fetch async

fetch() -> str | dict | None

发起请求并处理响应

Source code in qqmusic_api/utils/network.py
async def fetch(self) -> None | str | dict:
    """发起请求并处理响应"""
    return self._process_response(await self.request())

result async property

result: dict

获取请求结果