Skip to main content

领星开放平台 API 客户端,提供 httpx 异步和 requests 同步两种 HTTP 引擎实现。

Project description

lingxingapi-httpx

PyPI Python Version License Code Style

领星开放平台 API 客户端,基于 httpxrequests 双引擎实现,完整支持异步与同步调用。


目录


项目简介

本项目是领星官方 Python SDK 的现代化重构版本,将底层 HTTP 引擎从 aiohttp 替换为业界更主流的 httpx(异步)和 requests(同步),完整保留了原始的签名算法、Token 自动刷新、错误重试等核心业务能力。

无论你是需要高并发的异步爬虫,还是简单直观的同步脚本,lingxingapi-httpx 都能提供一致且稳定的开发体验。


功能特性

  • 双引擎支持:同时提供 httpx 异步客户端与 requests 同步客户端,一套代码,两种选择
  • 完整业务覆盖:涵盖基础数据、销售、FBA、产品、采购、仓库、广告、财务、工具、亚马逊源数据等十大模块
  • 自动鉴权access_tokenrefresh_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())

requests 同步版本

from lingxingapi_sync import API

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 = api.AccessToken()
    print(token.access_token)

    # 查询亚马逊市场列表
    mps = api.basic.Marketplaces()
    for mp in mps.data:
        print(mp.country, mp.country_code)

    # 查询店铺列表
    sellers = api.basic.Sellers()
    for seller in sellers.data:
        print(seller.seller_name, seller.status)

配置凭证

方式一:.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_idapp_secret 不正确
InvalidParametersError 请求参数非法
UnknownRequestError 未知错误

与官方 SDK 的差异

特性 官方 SDK (aiohttp) 本项目 (httpx) 本项目 (requests)
异步 / 同步 异步 异步 同步
HTTP 引擎 aiohttp httpx.AsyncClient requests.Session
上下文管理器 async with async with with
签名算法 AES-ECB + MD5 + Base64 完全一致 完全一致
Token 自动刷新 支持 支持 支持
限流 / 超时重试 支持 支持 支持
User-Agent python-requests/2.31.0 无(避免 WAF 拦截) 无(避免 WAF 拦截)

测试

运行全部测试

# httpx 异步版本
python test_basic_api_httpx.py

# requests 同步版本
python test_basic_api.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

注意事项

  1. IP 白名单:领星开放平台要求将调用方的出口 IP 加入 API 白名单,否则可能返回 403。
  2. Token 复用access_tokenrefresh_token 在进程内自动缓存,多个 API 实例共享同一 Token。
  3. 会话关闭:httpx / requests 的会话在 async with / with 退出时自动关闭,建议始终使用上下文管理器。
  4. .env 安全.env 文件包含敏感信息,请勿提交到 Git 仓库。建议将 .env 加入 .gitignore

贡献指南

欢迎提交 Issue 和 Pull Request!

  1. Fork 本仓库
  2. 创建你的特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 打开一个 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

lingxingapi_httpx-0.1.1.tar.gz (239.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lingxingapi_httpx-0.1.1-py3-none-any.whl (259.6 kB view details)

Uploaded Python 3

File details

Details for the file lingxingapi_httpx-0.1.1.tar.gz.

File metadata

  • Download URL: lingxingapi_httpx-0.1.1.tar.gz
  • Upload date:
  • Size: 239.5 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

Hashes for lingxingapi_httpx-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2aca55569db56c55f75bdc69a3a53cb4e39d1ba01a1dbeb7498190f8a1fd87d2
MD5 244eb49c25916bcb6b97d3ea849cd0c8
BLAKE2b-256 4948138483213d413e06c0f5b066b1cae83a1730452f079bf4faf9b5ee300f25

See more details on using hashes here.

File details

Details for the file lingxingapi_httpx-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: lingxingapi_httpx-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 259.6 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

Hashes for lingxingapi_httpx-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5b7eb590f90b9a55267b88cc747c0b8f84d552ffb5b0b53e835d0fdec27bf44
MD5 0ed367255887e6f36551305f8b300706
BLAKE2b-256 eec2d7da2056b4f06297e32bcbb5dc1ff05d7a74e97d503986d4eca367003e7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page