Skip to main content

py-wisharetec-toolkit

Wisharetec SaaS 系统的 Python 客户端库,提供同步和异步接口调用能力。

功能特性

  • 封装 SaaS 系统的认证机制,包括登录、令牌刷新和令牌缓存
  • 提供同步和异步两种 HTTP 请求模式
  • 集成 JSON Schema 校验和 JSONPath 查询工具
  • 支持 diskcache 和 Redis 两种缓存后端

安装

使用 uv(推荐)

uv add py-wisharetec-toolkit

使用 pip

pip install py-wisharetec-toolkit

依赖包

httpx>=0.27.0
pydantic>=2.0
jsonpath-ng>=1.5.3
jsonschema>=4.21.0
diskcache>=5.6.3
redis>=4.6.0
py-httpx-toolkit>=1.0.0

项目结构

src/
└── py_wisharetec_toolkit/
    ├── __init__.py          # 包入口模块
    └── saas/
        ├── __init__.py      # SaaS 模块入口,包含 Saas 客户端类
        ├── models.py        # 数据模型(查询条件等)
        ├── responses.py     # 响应模型(分页器等)
        └── utils.py         # 工具函数(JSON Schema 校验、响应模型构建等)

API 说明

Saas 类

初始化

from py_wisharetec_toolkit.saas import Saas

saas = Saas(
    base_url="https://saas.wisharetec.com/",
    account="your_account",
    password="your_password",
    cache_config={
        "instance": None,  # diskcache.Cache 或 redis.Redis 实例
        "key": "py_wisharetec_saas_account",
        "expire": 7100,
    },
    client_kwargs={
        "timeout": 60,
        "verify": False,
    }
)

同步方法

方法 说明
login() 用户登录,获取认证 Token
refresh_token() 智能刷新 Token(支持缓存)
query_space_manage_tree() 查询空间管理树结构
request_with_token() 发送带授权头的请求

异步方法

方法 说明
async_login() 异步用户登录
async_refresh_token() 异步智能刷新 Token
async_query_space_manage_tree() 异步查询空间管理树
async_request_with_token() 异步发送带授权头的请求

utils 工具函数

json_is_valid

校验 JSON 数据是否符合指定的 JSON Schema。

from py_wisharetec_toolkit.saas.utils import json_is_valid

schema = {
    "type": "object",
    "properties": {"id": {"type": "string"}},
    "required": ["id"]
}
data = {"id": "123"}
result = json_is_valid(schema, data)
# 返回: True

json_find_first

使用 JSONPath 表达式从 JSON 数据中查找第一个匹配项。

from py_wisharetec_toolkit.saas.utils import json_find_first

data = {"userInfoV": {"id": "123"}}
result = json_find_first("$.userInfoV.id", data)
# 返回: "123"

json_find

使用 JSONPath 表达式从 JSON 数据中查找所有匹配项。

from py_wisharetec_toolkit.saas.utils import json_find

data = {"items": [{"id": "1"}, {"id": "2"}]}
result = json_find("$.items[*].id", data)
# 返回: [1, 2]

json_find_values

使用 JSONPath 表达式从 JSON 数据中提取所有匹配的值。

json_find_first_value

使用 JSONPath 表达式从 JSON 数据中提取第一个匹配的值。

build_paginator_instance

将 HTTP 响应或字典转换为 Paginator 响应模型。

from py_wisharetec_toolkit.saas.utils import build_paginator_instance

result = build_paginator_instance({
    "pageNum": 1, 
    "pageSize": 50, 
    "total": 100, 
    "last": False, 
    "records": []
})
print(result.records)  # []

build_base_instance

将 HTTP 响应或字典转换为 Base 响应模型。

models 数据模型

QueryCondition

查询条件模型,用于封装分页查询参数。

from py_wisharetec_toolkit.saas.models import QueryCondition

condition = QueryCondition(
    conditions={"name": "test"},
    pageNum=1,
    pageSize=50,
    orderBy=["-createTime"]
)

QueryConditionField

查询条件字段模型,用于定义单个查询条件。

from py_wisharetec_toolkit.saas.models import QueryConditionField

field = QueryConditionField(
    method=1,
    name="status",
    value="active"
)

responses 响应模型

Paginator

分页响应模型,继承自 py_httpx_toolkit.responses.Base。

from py_wisharetec_toolkit.saas.responses import Paginator

paginator = Paginator(
    pageNum=1,
    pageSize=50,
    total=100,
    last=False,
    records=[]
)

使用示例

同步方式

from py_wisharetec_toolkit.saas import Saas

saas = Saas(
    account="test",
    password="password"
)

# 登录获取 Token
saas.login()

# 发送请求
response = saas.request_with_token(
    method="GET",
    url="/api/data/list"
)
print(response.json())

异步方式

import asyncio
from py_wisharetec_toolkit.saas import Saas

async def main():
    saas = Saas(
        account="test",
        password="password"
    )
    
    # 异步登录
    await saas.async_login()
    
    # 异步发送请求
    response = await saas.async_request_with_token(
        method="GET",
        url="/api/data/list"
    )
    print(response.json())

asyncio.run(main())

使用缓存

import diskcache
from py_wisharetec_toolkit.saas import Saas

cache = diskcache.Cache("./cache")

saas = Saas(
    account="test",
    password="password",
    cache_config={
        "instance": cache,
        "key": "saas_token",
        "expire": 7100
    }
)

# 自动从缓存获取或刷新 Token
saas.refresh_token()

作者

Guolei

项目主页

许可证

MIT License

Metadata

Release files for py_wisharetec_toolkit 1.0.7

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

Source distribution (sdist)

Source distribution for py_wisharetec_toolkit 1.0.7
File Size Uploaded Provenance
py_wisharetec_toolkit-1.0.7.tar.gz 12.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for py_wisharetec_toolkit 1.0.7
File Interpreter ABI Platform
py_wisharetec_toolkit-1.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 23.9 kB

Release files / py_wisharetec_toolkit-1.0.7.tar.gz

Download URL py_wisharetec_toolkit-1.0.7.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a8cce819de1a341b88d2a78b394d314a836dfaaf1f2a858ba71e391d975aeabd
BLAKE2b-256 checksum
How to use checksums
6a2ee679c652cc8812d2045b2c05482904537c454663419ab6f28f50a722cd03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.11

Release files / py_wisharetec_toolkit-1.0.7-py3-none-any.whl

Download URL py_wisharetec_toolkit-1.0.7-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c2136e5f2527faff89b1477d97a3defbd0748b8c780f22f7746e602bfe919f1
BLAKE2b-256 checksum
How to use checksums
cf283c4a6c22272abc2e86f772dac5849148b43b62f59b2ee262638439667eb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.11

Security for py_wisharetec_toolkit 1.0.7

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.

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