领星开放平台 API 客户端,基于 httpx 异步引擎实现。
Project description
目录
项目简介
本项目是领星官方 Python SDK 的现代化重构版本,将底层 HTTP 引擎从 aiohttp 替换为业界更主流的 httpx,完整保留了原始的签名算法、Token 自动刷新、错误重试等核心业务能力。
无论你是需要高并发的异步爬虫,lingxingapi-httpx 都能提供一致且稳定的开发体验。
功能特性
- 异步高性能:基于
httpx异步客户端,充分利用 Python 异步特性,轻松应对高并发场景 - 完整业务覆盖:涵盖基础数据、销售、FBA、产品、采购、仓库、广告、财务、工具、亚马逊源数据等十大模块
- 自动鉴权:
access_token与refresh_token在进程内自动缓存与刷新,多实例共享同一 Token - 智能重试:内置超时、限流、500 错误、网络断连等多场景自动重试机制,支持自定义重试策略
- 强类型校验:基于 Pydantic 的请求参数与响应数据校验,减少运行时错误
- 完善异常体系:十余种细粒度异常类型,便于精准捕获与处理各类 API 错误
安装
通过 PyPI 安装(推荐)
pip install lingxingapi-httpx
可选性能依赖
pip install orjson cytimes
从源码安装
git clone https://github.com/PerfectWorld233/lingxingapi-httpx.git
cd lingxingapi-httpx
pip install -e .
快速开始
httpx 异步版本
import asyncio
from lingxingapi_httpx import API
async def main():
async with API(
app_id="your_app_id",
app_secret="your_app_secret",
timeout=30,
ignore_timeout=True,
ignore_api_limit=True,
) as api:
# 获取 Token
token = await api.AccessToken()
print(token.access_token)
# 查询亚马逊市场列表
mps = await api.basic.Marketplaces()
for mp in mps.data:
print(mp.country, mp.country_code)
# 查询店铺列表
sellers = await api.basic.Sellers()
for seller in sellers.data:
print(seller.seller_name, seller.status)
asyncio.run(main())
配置凭证
方式一:.env 文件(推荐)
在项目根目录创建 .env 文件:
LINGXING_APP_ID=your_app_id
LINGXING_APP_SECRET=your_app_secret
方式二:环境变量
export LINGXING_APP_ID=your_app_id
export LINGXING_APP_SECRET=your_app_secret
方式三:代码中直接传入
api = API(app_id="your_app_id", app_secret="your_app_secret")
支持的 API 模块
| 模块 | 命名空间 | 说明 |
|---|---|---|
| 授权 | api.AccessToken() / api.RefreshToken() |
获取与刷新访问令牌 |
| 基础数据 | api.basic |
市场、店铺、国家、汇率、账户信息 |
| 销售数据 | api.sales |
Listing、订单、促销、刊登管理、自发货 |
| FBA 数据 | api.fba |
STA 任务、货件、包装组、到货接收 |
| 产品数据 | api.product |
本地产品、SPU、捆绑产品、分类、品牌、标签 |
| 采购数据 | api.purchase |
供应商、采购方、采购计划 |
| 仓库数据 | api.warehouse |
仓库、仓位、FBA/AWD 库存、库存流水 |
| 广告数据 | api.ads |
SP/SB/SD 广告基础数据与报表、DSP 报告 |
| 财务数据 | api.finance |
结算、交易、利润报表、库存分类账、广告发票 |
| 工具数据 | api.tools |
关键词监控、竞品监控 |
| 亚马逊源数据 | api.source |
源报表订单、库存、移除货件、报告导出 |
核心参数说明
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
app_id |
str |
必填 | 领星开放平台应用 ID |
app_secret |
str |
必填 | 领星开放平台应用密钥 |
timeout |
int / float |
60 |
请求超时(秒) |
ignore_timeout |
bool |
False |
超时时是否自动重试 |
ignore_timeout_wait |
int / float |
1 |
超时重试等待间隔(秒) |
ignore_timeout_retry |
int |
10 |
超时最大重试次数,-1 为无限 |
ignore_api_limit |
bool |
False |
限流时是否自动重试 |
ignore_api_limit_wait |
int / float |
1 |
限流重试等待间隔(秒) |
ignore_api_limit_retry |
int |
10 |
限流最大重试次数,-1 为无限 |
ignore_internal_server_error |
bool |
False |
500 错误时是否自动重试 |
ignore_internal_server_error_wait |
int / float |
1 |
500 错误重试等待间隔(秒) |
ignore_internal_server_error_retry |
int |
10 |
500 错误最大重试次数,-1 为无限 |
ignore_internet_connection |
bool |
False |
网络断开时是否自动重试 |
ignore_internet_connection_wait |
int / float |
1 |
网络断连重试等待间隔(秒) |
ignore_internet_connection_retry |
int |
10 |
网络断连最大重试次数,-1 为无限 |
max_tcp_connections |
int |
100 |
最大 TCP 连接数 |
异常体系
所有异常均继承自 BaseApiError,可按需捕获:
| 异常类 | 触发场景 |
|---|---|
ApiSettingsError |
初始化参数非法 |
InternetConnectionError |
无法连接互联网 |
ApiTimeoutError |
请求超时 |
InternalServerError |
领星服务器 500 错误 |
AccessTokenExpiredError |
access_token 过期 |
RefreshTokenExpiredError |
refresh_token 过期 |
SignatureExpiredError |
签名过期 |
TooManyRequestsError / ApiLimitError |
请求被限流 |
AppIdOrSecretError |
app_id 或 app_secret 不正确 |
InvalidParametersError |
请求参数非法 |
UnknownRequestError |
未知错误 |
与官方 SDK 的差异
| 特性 | 官方 SDK (aiohttp) |
本项目 (httpx) |
|---|---|---|
| 异步 / 同步 | 异步 | 异步 |
| HTTP 引擎 | aiohttp |
httpx.AsyncClient |
| 上下文管理器 | async with |
async with |
| 签名算法 | AES-ECB + MD5 + Base64 | 完全一致 |
| Token 自动刷新 | 支持 | 支持 |
| 限流 / 超时重试 | 支持 | 支持 |
| User-Agent | python-requests/2.31.0 |
无(避免 WAF 拦截) |
测试
运行全部测试
# httpx 异步版本
python test_basic_api_httpx.py
pytest 单测
已兼容 pytest,可在 PyCharm 中右键单独运行某个测试:
pytest test_basic_api_httpx.py::test_access_token -v
pytest test_basic_api_httpx.py::test_marketplaces -v
pytest test_basic_api_httpx.py::test_sellers -v
签名一致性验证
python test_signature_httpx.py
注意事项
- IP 白名单:领星开放平台要求将调用方的出口 IP 加入 API 白名单,否则可能返回 403。
- Token 复用:
access_token和refresh_token在进程内自动缓存,多个 API 实例共享同一 Token。 - 会话关闭:httpx / requests 的会话在
async with/with退出时自动关闭,建议始终使用上下文管理器。 - .env 安全:
.env文件包含敏感信息,请勿提交到 Git 仓库。建议将.env加入.gitignore。
贡献指南
欢迎提交 Issue 和 Pull Request!
- Fork 本仓库
- 创建你的特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 打开一个 Pull Request
请确保你的代码通过现有测试,并在必要时补充新的测试用例。
许可证
本项目为领星官方 SDK 的翻译/改写版本,仅供学习和内部使用。
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lingxingapi_httpx-0.1.2.tar.gz.
File metadata
- Download URL: lingxingapi_httpx-0.1.2.tar.gz
- Upload date:
- Size: 238.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.12.1.2 readme-renderer/44.0 requests/2.33.1 requests-toolbelt/1.0.0 urllib3/2.6.3 tqdm/4.67.3 importlib-metadata/9.0.0 keyring/25.7.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2eccab03719b14b14e30ce81b0d8cdedb88b243cb400cf912fef5dfb5a61ad0
|
|
| MD5 |
5928cd62f2033f570cc78baea0b85844
|
|
| BLAKE2b-256 |
dc7d8d0d65f2d7953d4b612c3bbcc76f503738a6387ec21d73e0258507aa5cc8
|
File details
Details for the file lingxingapi_httpx-0.1.2-py3-none-any.whl.
File metadata
- Download URL: lingxingapi_httpx-0.1.2-py3-none-any.whl
- Upload date:
- Size: 259.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.12.1.2 readme-renderer/44.0 requests/2.33.1 requests-toolbelt/1.0.0 urllib3/2.6.3 tqdm/4.67.3 importlib-metadata/9.0.0 keyring/25.7.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9eecf4b6997a17df9dc847c7c443216a1c15621311cd59294ce176585f0d0db
|
|
| MD5 |
a9cbbaf18ce12bfd026239cea8afcb63
|
|
| BLAKE2b-256 |
6d4bb0cc73690ba3f83f43577c8cb75d9b457985c0c236afe26b56011ec7beb9
|