Skip to main content

Shopline SDK for Python

Python Version License PyPI Version

一个用于与 Shopline OpenAPI 交互的 Python SDK。基于 Shopline OpenAPI 文档 实现。

特性

  • 🚀 异步支持: 基于 aiohttp 的异步 HTTP 客户端
  • 📝 类型安全: 使用 pydantic 进行数据验证和类型提示
  • 🛡️ 错误处理: 完善的异常处理机制
  • 📚 完整覆盖: 支持 Shopline OpenAPI 的所有端点
  • 🔧 易于使用: 简洁的 API 设计,易于集成

安装

pip install shopline-sdk-python

或者使用 uv:

uv add shopline-sdk-python

快速开始

基本用法

import asyncio
import os
from shopline_sdk.client import ShoplineAPIClient
from shopline_sdk.apis.customers import get_customers

async def main():
    # 创建客户端
    client = ShoplineAPIClient(os.getenv('SHOPLINE_ACCESS_TOKEN'))
    
    # 使用客户端会话
    async with client.new_session() as session:
        # 调用 API
        response = await get_customers.call(session)
        print(f"获取到 {len(response.items)} 个客户")

# 运行异步函数
asyncio.run(main())

带参数的 API 调用

import os
import asyncio
from shopline_sdk.client import ShoplineAPIClient
from shopline_sdk.apis.customers import get_customers

async def get_recent_customers():
    client = ShoplineAPIClient(os.getenv('SHOPLINE_ACCESS_TOKEN'))
    
    async with client.new_session() as session:
        # 创建查询参数
        params = get_customers.Params(
            page=1,
            per_page=10,
            sort_by='desc',
            include_fields=['metafields']
        )
        
        # 调用 API
        response = await get_customers.call(session, params=params)
        
        for customer in response.items:
            print(f"客户: {customer.email}")

asyncio.run(get_recent_customers())

创建资源示例

import os
import asyncio
from shopline_sdk.client import ShoplineAPIClient
from shopline_sdk.apis.addon_products import create_addon_product
from shopline_sdk.models.money import Money
from shopline_sdk.models.translatable import Translatable

async def create_addon():
    client = ShoplineAPIClient(os.getenv('SHOPLINE_ACCESS_TOKEN'))
    
    async with client.new_session() as session:
        # 创建加购品请求体
        body = create_addon_product.Body(
            title_translations=Translatable(
                zh_tw="加購商品",
                en="Addon Product"
            ),
            sku="ADDON-001",
            unlimited_quantity=True,
            cost=Money(amount="10.00", currency="USD")
        )
        
        # 调用 API
        addon_product = await create_addon_product.call(session, body=body)
        print(f"创建的加购品 ID: {addon_product.id}")

asyncio.run(create_addon())

更新资源示例

import os
import asyncio
from shopline_sdk.client import ShoplineAPIClient
from shopline_sdk.apis.customers import update_customer

async def update_customer_info():
    client = ShoplineAPIClient(os.getenv('SHOPLINE_ACCESS_TOKEN'))
    
    async with client.new_session() as session:
        customer_id = "5a55b3c973746f507e120000"
        
        # 创建查询参数(可选字段)
        params = update_customer.Params(
            fields=["id", "email", "name"]  # 只返回指定字段
        )
        
        # 创建请求体(要更新的数据)
        body = update_customer.Body(
            name="张三",
            email="zhangsan@example.com",
            is_accept_marketing=True
        )
        
        # 调用 API
        updated_customer = await update_customer.call(
            session,
            id=customer_id,
            params=params,
            body=body
        )
        print(f"更新的客户: {updated_customer.name} ({updated_customer.email})")

asyncio.run(update_customer_info())

API 覆盖

SDK 支持 Shopline OpenAPI 的所有主要功能模块:

客户管理

  • 获取客户列表
  • 创建、更新、删除客户
  • 客户标签管理
  • 会员积分管理
  • 客户元数据管理

产品管理

  • 产品 CRUD 操作
  • 产品变体管理
  • 库存管理
  • 加购品管理
  • 产品元数据管理

订单管理

  • 订单查询和管理
  • 订单配送管理
  • 订单元数据管理

营销工具

  • 促销活动管理
  • 联盟营销
  • 优惠券管理
  • 会员等级管理

其他功能

  • 分类管理
  • 媒体文件管理
  • 主题设置
  • 批量操作

错误处理

SDK 提供了完善的错误处理机制:

from shopline_sdk.client import ShoplineAPIClient
from shopline_sdk.exceptions import ShoplineAPIError
from shopline_sdk.apis.customers import get_customers

async def handle_errors():
    client = ShoplineAPIClient("invalid_token")
    
    try:
        async with client.new_session() as session:
            response = await get_customers.call(session)
    except ShoplineAPIError as e:
        print(f"API 错误: {e.status_code} - {e.message}")
        print(f"错误代码: {e.code}")
        if e.error:
            print(f"详细错误: {e.error}")

配置

环境变量

建议使用环境变量来管理敏感信息:

export SHOPLINE_ACCESS_TOKEN="your_access_token_here"

自定义基础 URL

from shopline_sdk.client import ShoplineAPIClient
client = ShoplineAPIClient(
    access_token="your_token",
    base_url="https://custom-api.shopline.io/v1"
)

许可证

本项目采用 GPL-3.0 许可证。详见 LICENSE 文件。

贡献

欢迎提交 Issue 和 Pull Request!

链接

更新日志

v0.1.0

  • 初始版本发布
  • 支持所有 Shopline OpenAPI 端点
  • 异步客户端实现

Metadata

Release files for shopline-sdk-python 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shopline-sdk-python 0.3.3
File Size Uploaded Provenance Actions
shopline_sdk_python-0.3.3.tar.gz 207.1 kB Without provenance Details for shopline_sdk_python-0.3.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shopline-sdk-python 0.3.3
File Interpreter ABI Platform Actions
shopline_sdk_python-0.3.3-py3-none-any.whl Python 3 none any Details for shopline_sdk_python-0.3.3-py3-none-any.whl

Total release size: 867.6 kB

Release files / shopline_sdk_python-0.3.3.tar.gz

Download URL shopline_sdk_python-0.3.3.tar.gz
Size 207.1 kB
Tags Source
SHA-256 checksum
How to use checksums
00dbb2ac1fe44848d8c95110ed3b8a82ef14929bc8c2ec579d8a561c8888c734
BLAKE2b-256 checksum
How to use checksums
1ab510390ae882ae1aa2063914e5c93c193cb0467598b53a08a04564b26f227f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / shopline_sdk_python-0.3.3-py3-none-any.whl

Download URL shopline_sdk_python-0.3.3-py3-none-any.whl
Size 660.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1fbc469ccdd288dce67d2d42fd73663e4b42dc4d819e25b3ca25a978c3b5e38d
BLAKE2b-256 checksum
How to use checksums
62e7e1cfbb5decb57e18fedf96d1fbe848bb2d976d96946559fceaba15f1732b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for shopline-sdk-python 0.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page