Metadata-Version: 2.4
Name: py-wisharetec-toolkit
Version: 1.0.9
Summary: Wisharetec SaaS 系统的 Python 客户端库，提供同步和异步接口调用能力。
Author-email: Guolei <174000902@qq.com>
License: MIT License
        
        Copyright (c) 2026 郭磊
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://gitee.com/guolei19850528/py_wisharetec_toolkit
Project-URL: Repository, https://gitee.com/guolei19850528/py_wisharetec_toolkit.git
Keywords: wisharetec,python,client,api,wisharetec saas,saas
Classifier: License :: OSI Approved :: MIT License
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: httpx>=0.27.0
Requires-Dist: pydantic>=2.0
Requires-Dist: diskcache>=5.6.3
Requires-Dist: redis>=4.6.0
Requires-Dist: py-httpx-toolkit>=1.0.0
Requires-Dist: jsonschema>=4.26.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: setuptools>=61.0; extra == "dev"
Requires-Dist: twine>=4.0; extra == "dev"
Dynamic: license-file

# py-wisharetec-toolkit

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

## 功能特性

- 封装 SaaS 系统的认证机制，包括登录、令牌刷新和令牌缓存
- 提供同步和异步两种 HTTP 请求模式
- 支持 diskcache 和 Redis 两种缓存后端
- 集成 Pydantic 数据模型，支持类型校验

## 安装

### 使用 uv（推荐）

```bash
uv add py-wisharetec-toolkit
```

### 使用 pip

```bash
pip install py-wisharetec-toolkit
```

## 依赖包

```text
httpx>=0.27.0
pydantic>=2.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         # 工具函数（响应模型构建等）
```

## API 说明

### Saas 类

#### 初始化

```python
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 工具函数

#### build_paginator_instance

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

```python
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 响应模型。

```python
from py_wisharetec_toolkit.saas.utils import build_base_instance

result = build_base_instance(response)
```

### models 数据模型

#### QueryCondition

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

```python
from py_wisharetec_toolkit.saas.models import QueryCondition

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

#### QueryConditionField

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

```python
from py_wisharetec_toolkit.saas.models import QueryConditionField

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

### responses 响应模型

#### Paginator

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

```python
from py_wisharetec_toolkit.saas.responses import Paginator

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

## 使用示例

### 同步方式

```python
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())
```

### 异步方式

```python
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())
```

### 使用缓存

```python
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**

- 邮箱: 174000902@qq.com

## 项目主页

- **Gitee**: [https://gitee.com/guolei19850528/py_wisharetec_toolkit](https://gitee.com/guolei19850528/py_wisharetec_toolkit)

## 许可证

MIT License
