Skip to main content

Shopline OpenAPI on python implement. (From https://open-api.docs.shoplineapp.com)

Project description

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, get_customers.Request())
        print(f"获取到 {len(response.items)} 个客户")

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

带参数的 API 调用

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:
        # 创建请求参数
        request = get_customers.Request(
            page=1,
            per_page=10,
            sort_by='desc',
            include_fields=['metafields']
        )
        
        # 调用 API
        response = await get_customers.call(session, request)
        
        for customer in response.items:
            print(f"客户: {customer.email}")

asyncio.run(get_recent_customers())

创建资源示例

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:
        # 创建加购品请求
        request = create_addon_product.Request(
            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, request)
        print(f"创建的加购品 ID: {addon_product.id}")

asyncio.run(create_addon())

API 覆盖

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

客户管理

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

产品管理

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

订单管理

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

营销工具

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

其他功能

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

错误处理

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

from shopline_sdk.exceptions import ShoplineAPIError

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

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

自定义请求头

async with client.new_session(headers={"Custom-Header": "value"}) as session:
    # 使用自定义请求头
    response = await get_customers.call(session)

开发

安装开发依赖

pip install -e ".[dev]"

运行测试

pytest

构建包

python -m build

许可证

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

贡献

欢迎提交 Issue 和 Pull Request!

链接

更新日志

v0.1.0

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

shopline_sdk_python-0.1.0-py3-none-any.whl (554.4 kB view details)

Uploaded Python 3

File details

Details for the file shopline_sdk_python-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for shopline_sdk_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4005505ac6f8e59220ab64215f1ab8ed72cbc0cd9815e14b4306a41af167047
MD5 d06d849ff84fe6a80a61607f97853d16
BLAKE2b-256 f65709985565f4f90b167d6ee8a18587957d6b8b5e0718e8050064c416af408c

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