Skip to main content

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

Project description

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 HTTP 客户端库,支持同步和异步请求
pydantic >=2.0 数据验证和序列化
jsonpath-ng >=1.5.3 JSONPath 查询工具
jsonschema >=4.21.0 JSON Schema 校验
diskcache >=5.6.3 本地磁盘缓存
redis >=4.6.0 Redis 缓存支持
py-httpx-toolkit >=1.0.0 HTTP 请求工具封装

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_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_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

使用示例

同步方式

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

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

py_wisharetec_toolkit-1.0.4.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

py_wisharetec_toolkit-1.0.4-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file py_wisharetec_toolkit-1.0.4.tar.gz.

File metadata

File hashes

Hashes for py_wisharetec_toolkit-1.0.4.tar.gz
Algorithm Hash digest
SHA256 fdc6ffdcab87defcf67b245da7980eec4b6b6fdfcfe9b4fdf7a3c573d588ad8f
MD5 ca5bd0dd45391921c8b80276a670c701
BLAKE2b-256 5d77d9f0bbe335148bd0b13fb6f4e241cdac74f0f5b3c744ad50ae6dd16c785c

See more details on using hashes here.

File details

Details for the file py_wisharetec_toolkit-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for py_wisharetec_toolkit-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fc675f43385e2dd51e6fcf90d841fb35403347cefa23a38c32457c6b8d2e7fba
MD5 0054c1c82c5877a7708fdff682dcde76
BLAKE2b-256 b6ecd7b8557a842100d92f8e6f4a0d638b3627115d57f23281a8b55120048aa0

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