Skip to main content

Xiyou OpenAPI Python SDK - 提供完整的API功能(支持同步和异步调用)

Project description

Xiyou SDK

Xiyou OpenAPI Python SDK,提供完整的API功能。

当前版本包含:

  • 认证模块 (Auth) - 完整的加签认证功能

规划中的模块:

  • 📋 API客户端模块
  • 📊 数据分析模块
  • 🔧 工具模块

特性

  • 纯Python标准库实现 - 无需外部依赖
  • HMAC-SHA256签名 - 安全可靠的签名算法
  • 简单易用 - 几行代码即可完成认证
  • 灵活集成 - 可与任何HTTP客户端库配合使用
  • 模块化设计 - 可扩展的架构设计

安装

pip install xiyou_sdk

快速开始

1. 基本使用

from xiyou_sdk import XiyouAuth

# 初始化认证对象
auth = XiyouAuth(
    client_id="your_client_id",
    secret_key="your_secret_key"
)

# 获取认证头部
headers = auth.get_auth_headers(
    method="POST",
    path="/v1/asins/traffic",
    body='{"entities":[{"country":"US","asin":"B09PCSR9SX"}]}'
)

print(headers)
# 输出:
# {
#     'X-Client-Id': 'your_client_id',
#     'X-Timestamp': '1753776583',
#     'X-Sign': '5572ae2572e2aef8fa23623a3f56fa245f2f74950b533fe63100ad8f688b254f',
#     'Content-Type': 'application/json'
# }

2. 与requests库结合使用

import requests
import json
from xiyou_sdk import XiyouAuth

auth = XiyouAuth("your_client_id", "your_secret_key")

# 构建请求数据
data = {"entities": [{"country": "US", "asin": "B09PCSR9SX"}]}
body = json.dumps(data, ensure_ascii=False, separators=(',', ':'))

# 获取认证头部
headers = auth.get_auth_headers(
    method="POST",
    path="/v1/asins/traffic",
    body=body
)

# 发起请求
response = requests.post(
    "https://api.xiyou.com/v1/asins/traffic",
    headers=headers,
    data=body
)

3. 与urllib结合使用

import urllib.request
import json
from xiyou_sdk import XiyouAuth

auth = XiyouAuth("your_client_id", "your_secret_key")

# 构建请求数据
data = {"entities": [{"country": "US", "asin": "B09PCSR9SX"}]}
body = json.dumps(data, ensure_ascii=False, separators=(',', ':'))

# 获取认证头部
headers = auth.get_auth_headers(
    method="POST",
    path="/v1/asins/traffic",
    body=body
)

# 发起请求
req = urllib.request.Request(
    "https://api.xiyou.com/v1/asins/traffic",
    data=body.encode(),
    headers=headers
)
response = urllib.request.urlopen(req)

API参考

XiyouAuth

构造函数

XiyouAuth(client_id: str, secret_key: str)

参数:

  • client_id: 客户端ID
  • secret_key: 密钥

方法

get_auth_headers()
get_auth_headers(
    method: str = "POST",
    path: str = "",
    body: str = "",
    timestamp: Optional[str] = None
) -> Dict[str, str]

生成包含认证信息的完整头部。

参数:

  • method: HTTP方法 (GET, POST, PUT, DELETE等)
  • path: API路径 (如: /v1/asins/traffic)
  • body: 请求体内容
  • timestamp: 自定义时间戳,默认使用当前时间

返回: 包含认证头部的字典

generate_signature()
generate_signature(
    timestamp: str,
    method: str = "POST",
    path: str = "",
    body: str = ""
) -> str

生成API请求签名。

参数:

  • timestamp: 时间戳字符串
  • method: HTTP方法
  • path: API路径
  • body: 请求体内容

返回: 签名字符串

签名算法

签名使用HMAC-SHA256算法,签名字符串格式为:

client_id + timestamp + method + path + body

支持的API

该SDK适用于所有Xiyou OpenAPI接口,包括:

  • /v1/asins/traffic - ASIN流量得分
  • /v1/asins/infoChange/trends/daily - ASIN基础信息变动趋势(天)
  • /v1/asins/trafficScore/trend/daily - ASIN流量得分趋势(天)
  • /v1/asins/trafficScore/trend/weekly - ASIN流量得分趋势(周)
  • /v1/asins/trafficScore/trend/monthly - ASIN流量得分趋势(月)
  • 以及其他所有API接口

模块架构

xiyou_sdk/
├── __init__.py          # 主包入口
├── auth.py              # 认证模块 ✅
├── client.py            # API客户端模块 (规划中)
├── models.py            # 数据模型模块 (规划中)
└── utils.py             # 工具模块 (规划中)

示例

查看 examples/ 目录中的示例代码:

  • basic_usage.py - 基本使用示例
  • signature_test.py - 签名验证测试

运行示例:

PYTHONPATH=. python examples/basic_usage.py
PYTHONPATH=. python examples/signature_test.py

许可证

MIT License

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

xiyou_sdk-1.1.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

xiyou_sdk-1.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file xiyou_sdk-1.1.0.tar.gz.

File metadata

  • Download URL: xiyou_sdk-1.1.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.5

File hashes

Hashes for xiyou_sdk-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ce0a3a5af9c9653ff4065df246f73548c35645da85006361bf42b16cabbfe42f
MD5 99aec4f1340bf3f89c7156ba352a36ea
BLAKE2b-256 6263f4c89ffe8d30135bcc2e9418b4a5d460b33d99ccf457bddc44fabeeed857

See more details on using hashes here.

File details

Details for the file xiyou_sdk-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: xiyou_sdk-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.5

File hashes

Hashes for xiyou_sdk-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b98c005fdc5be066a426992cdee968b040b0c6b3b8a455b7724f9efad111269
MD5 b3e6aa3d74cbda8293b77c87a3c27be4
BLAKE2b-256 a303e8858ea3fc5d82f8c7c62e27a1ce7342c31ac118bf3173887979183c4d39

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