snowland-shopeeapi
基于 aiohttp 的 Shopee OpenAPI (Partner API) v2 异步 SDK,同时支持正式环境与沙箱环境。
特性
- 纯
async/await异步实现,基于 aiohttp - 自动处理 Shopee 请求签名(HMAC-SHA256)
- 内置
Environment.PRODUCTION/Environment.SANDBOX两套域名 - 开箱即用的 API 分组:授权、店铺、商品、订单、物流
- 统一异常处理(
ShopeeAPIError)
安装
pip install snowland-shopeeapi
# 开发/测试依赖
pip install "snowland-shopeeapi[dev]"
本 SDK 的底层 HTTP 依赖 snowland-http(异步 aiohttp 后端)。
若从源码安装本仓库,请同时安装其本地依赖:
pip install -e ../snowland-http[aiohttp]
快速开始
import asyncio
from snowland_shopeeapi import ShopeeClient, Environment
PARTNER_ID = 123456
PARTNER_KEY = "your_partner_key"
SHOP_ID = 789012
REDIRECT_URL = "https://your-callback.com/cb"
async def main():
async with ShopeeClient(PARTNER_ID, PARTNER_KEY, Environment.SANDBOX) as client:
# 1) 引导商家授权,拼接授权 URL
auth_url = client.build_auth_url(REDIRECT_URL)
print("请商家访问:", auth_url)
# 2) 用授权回调拿到的 code 换取 token
code = "auth_code_from_callback"
token = await client.auth.get_token(code, SHOP_ID)
access_token = token["access_token"]
# 3) 调用店铺接口
info = await client.shop.get_shop_info(access_token, SHOP_ID)
print(info)
# 4) 刷新即将过期的 token
refreshed = await client.auth.refresh_token(
token["refresh_token"], access_token, SHOP_ID
)
print(refreshed)
asyncio.run(main())
环境切换
from snowland_shopeeapi import Environment
# 正式环境
client = ShopeeClient(PARTNER_ID, PARTNER_KEY, Environment.PRODUCTION)
# 沙箱环境(默认)
client = ShopeeClient(PARTNER_ID, PARTNER_KEY, Environment.SANDBOX)
支持的 API 分组
| 分组 | 示例方法 |
|---|---|
client.auth |
get_token(code, shop_id)、refresh_token(...) |
client.shop |
get_shop_info、get_profile、update_shop |
client.product |
get_item_list、get_item_base_info、add_item、update_item、delete_item |
client.order |
get_order_list、get_order_detail |
client.logistics |
get_shipping_parameter、create_shipping_document、get_tracking_number |
client.returns |
get_return_list、get_return_detail、confirm_return、dispute_return |
client.conversation |
get_conversation_list、get_messages、send_message、reply_message |
client.discount |
add_discount、update_discount、delete_discount、get_discount_list、get_discount、add_discount_item、update_discount_item、delete_discount_item |
所有店铺级接口都需要传入 access_token 与 shop_id。
连接池、限流与重试
底层 HTTP(连接池、限流)由 snowland-http 提供:默认使用 aiohttp 异步后端,
并通过 max_rate(每秒请求数)与 burst(突发上限)开启令牌桶限流,主动避免触发 Shopee 接口限流。
SDK 在此基础上对瞬时故障自动重试:网络错误、超时、HTTP 429/5xx,以及 Shopee 限流错误码
(RATE_LIMIT_EXCEEDED 等)。重试采用指数退避(retry_backoff * 2^(n-1)),并在收到 429 时优先采用响应头 Retry-After。
client = ShopeeClient(
PARTNER_ID, PARTNER_KEY, Environment.SANDBOX,
max_rate=10, burst=5,
enable_retry=True, max_retries=3, retry_backoff=0.5,
)
自定义请求
需要调用尚未封装的接口时,可直接使用底层方法:
data = await client.request(
"POST", "/api/v2/xxx/some_path",
json={"foo": "bar"}, access_token=access_token, shop_id=SHOP_ID,
)
测试
python -m unittest discover -s tests
许可证
BSD-3-Clause
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 snowland_shopeeapi-0.1.0.tar.gz.
File metadata
- Download URL: snowland_shopeeapi-0.1.0.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ba1f1f553b6a79ff8fb158f524661dbe7dff926b6fedb82defd7b062f2c0d00
|
|
| MD5 |
f8b588db44857b769d78e5769c19a1b6
|
|
| BLAKE2b-256 |
7680e6350764529ceb2745332d4e6415ff92ce357177477aaf97dc15baa2a3a7
|
File details
Details for the file snowland_shopeeapi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: snowland_shopeeapi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94a87927b8925cd1e2d0ec115965ff02f0497373763247ac641105879a556c02
|
|
| MD5 |
4d5fda54aa0b4ca4f88673e61369b860
|
|
| BLAKE2b-256 |
7566a05e2c507e71900a20b036835a2017579626faefbdf966458d5ee686eeb3
|