wecom-doc-sdk 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wecom_doc_sdk-0.1.0/.gitattributes +20 -0
- wecom_doc_sdk-0.1.0/.github/workflows/publish.yml +40 -0
- wecom_doc_sdk-0.1.0/.gitignore +13 -0
- wecom_doc_sdk-0.1.0/.python-version +1 -0
- wecom_doc_sdk-0.1.0/.vscode/settings.json +3 -0
- wecom_doc_sdk-0.1.0/AGENTS.md +54 -0
- wecom_doc_sdk-0.1.0/CONTRIBUTING.md +89 -0
- wecom_doc_sdk-0.1.0/PKG-INFO +236 -0
- wecom_doc_sdk-0.1.0/README.md +218 -0
- wecom_doc_sdk-0.1.0/pyproject.toml +53 -0
- wecom_doc_sdk-0.1.0/references/wecom-docs.md +56 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/__init__.py +93 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/apis/__init__.py +5 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/apis/smartsheet.py +345 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/client.py +212 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/exceptions.py +44 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/models/__init__.py +253 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/models/common.py +47 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/models/enums.py +264 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/models/fields.py +337 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/models/groups.py +88 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/models/records.py +231 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/models/sheets.py +101 -0
- wecom_doc_sdk-0.1.0/src/wecom_doc_sdk/models/views.py +243 -0
- wecom_doc_sdk-0.1.0/uv.lock +390 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
.agents/** linguist-generated=true
|
|
2
|
+
|
|
3
|
+
# 统一使用 LF 行尾符(与 Mac/Linux 一致)
|
|
4
|
+
* text=auto eol=lf
|
|
5
|
+
|
|
6
|
+
# 确保这些文件类型始终使用 LF
|
|
7
|
+
*.ts text eol=lf
|
|
8
|
+
*.tsx text eol=lf
|
|
9
|
+
*.js text eol=lf
|
|
10
|
+
*.jsx text eol=lf
|
|
11
|
+
*.json text eol=lf
|
|
12
|
+
*.md text eol=lf
|
|
13
|
+
*.mdx text eol=lf
|
|
14
|
+
*.yml text eol=lf
|
|
15
|
+
*.yaml text eol=lf
|
|
16
|
+
*.toml text eol=lf
|
|
17
|
+
*.css text eol=lf
|
|
18
|
+
*.scss text eol=lf
|
|
19
|
+
*.html text eol=lf
|
|
20
|
+
*.sh text eol=lf
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types:
|
|
6
|
+
- published
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
publish:
|
|
13
|
+
name: Build and publish to PyPI
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
environment:
|
|
16
|
+
name: pypi
|
|
17
|
+
url: https://pypi.org/p/wecom-doc-sdk
|
|
18
|
+
permissions:
|
|
19
|
+
contents: read
|
|
20
|
+
id-token: write
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- name: Check out repository
|
|
24
|
+
uses: actions/checkout@v6
|
|
25
|
+
|
|
26
|
+
- name: Set up Python
|
|
27
|
+
uses: actions/setup-python@v6
|
|
28
|
+
with:
|
|
29
|
+
python-version-file: ".python-version"
|
|
30
|
+
|
|
31
|
+
- name: Install uv
|
|
32
|
+
uses: astral-sh/setup-uv@v7
|
|
33
|
+
with:
|
|
34
|
+
enable-cache: true
|
|
35
|
+
|
|
36
|
+
- name: Build distributions
|
|
37
|
+
run: uv build
|
|
38
|
+
|
|
39
|
+
- name: Publish distributions to PyPI
|
|
40
|
+
run: uv publish --trusted-publishing always
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.10
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Agent Guide
|
|
2
|
+
|
|
3
|
+
本文件用于统一协作式代码生成/修改的约定,帮助快速上手本仓库。
|
|
4
|
+
|
|
5
|
+
## 项目概览
|
|
6
|
+
|
|
7
|
+
- 项目:企业微信文档 SDK(Python)
|
|
8
|
+
- 目标:封装企业微信文档相关 API(当前聚焦“管理智能表格内容”)
|
|
9
|
+
- 技术栈:`httpx` + `pydantic` + 完整类型标注
|
|
10
|
+
- 发布包名:`wecom-doc-sdk`
|
|
11
|
+
- Python 导入名:`wecom_doc_sdk`
|
|
12
|
+
|
|
13
|
+
## 目录与结构
|
|
14
|
+
|
|
15
|
+
- `src/`:SDK 源码(使用 `src` 布局)
|
|
16
|
+
- `references/`:版本化维护的官方文档入口与参考索引
|
|
17
|
+
- `docs/`:企业微信接口文档参考
|
|
18
|
+
- `tests/`:pytest 测试(如不存在,新增时遵循该目录)
|
|
19
|
+
|
|
20
|
+
建议包结构(新增代码优先遵循):
|
|
21
|
+
`src/wecom_doc_sdk/`
|
|
22
|
+
`src/wecom_doc_sdk/client.py`(HTTP 客户端封装)
|
|
23
|
+
`src/wecom_doc_sdk/models/`(Pydantic 模型)
|
|
24
|
+
`src/wecom_doc_sdk/apis/`(接口封装)
|
|
25
|
+
|
|
26
|
+
## 开发规范要点
|
|
27
|
+
|
|
28
|
+
- 中文注释:关键逻辑、边界条件与业务语义必须有中文说明
|
|
29
|
+
- 类型标注:函数/方法需完整类型标注,公开接口必须明确输入输出
|
|
30
|
+
- HTTP:使用 `httpx.Client/AsyncClient` 复用连接;统一超时与错误处理
|
|
31
|
+
- Pydantic:v2 写法;序列化用 `model_dump()`,校验用 `model_validate()`
|
|
32
|
+
- Pydantic 命名冲突:若模块内需要保留业务别名 `Field`,统一将 Pydantic 的 `Field` 导入为 `PydanticField`
|
|
33
|
+
- 错误处理:统一沿用 `WeComAPIError` / `WeComRequestError`,并以 `errcode == 0` 作为业务成功判断标准
|
|
34
|
+
- 扩展性:新增接口按模块拆分,避免把逻辑堆在单文件
|
|
35
|
+
|
|
36
|
+
详细规范见 `CONTRIBUTING.md`。
|
|
37
|
+
|
|
38
|
+
## 常用命令
|
|
39
|
+
|
|
40
|
+
- 安装依赖(开发):`uv sync --dev`
|
|
41
|
+
- IDE/ty 解释器:优先使用仓库内 `.venv`,VS Code 建议绑定 `${workspaceFolder}\\.venv\\Scripts\\python.exe`
|
|
42
|
+
- 代码风格:`uv run ruff check .`
|
|
43
|
+
- 代码格式:`uv run black --check .`
|
|
44
|
+
- 类型检查:`uv run ty check`
|
|
45
|
+
- 测试:`uv run pytest`
|
|
46
|
+
- 本地构建:`uv build`
|
|
47
|
+
- 分发包检查:`uvx twine check dist/*`
|
|
48
|
+
|
|
49
|
+
## 变更约定
|
|
50
|
+
|
|
51
|
+
- 尽量小步提交,改动保持聚焦
|
|
52
|
+
- 不随意重排/重写与本次任务无关的代码
|
|
53
|
+
- 修改公共接口需同步补充类型与文档
|
|
54
|
+
- 涉及发布相关改动时,同步检查 `pyproject.toml`、README 安装命令、导入示例与公共导出是否一致
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# 贡献指南(开发规范)
|
|
2
|
+
|
|
3
|
+
本文档用于统一企业微信文档 SDK 的基础开发规范,便于后续扩展与协作。
|
|
4
|
+
|
|
5
|
+
## 开发环境
|
|
6
|
+
|
|
7
|
+
- Python 版本:3.10(与项目 `pyproject.toml` 对齐)
|
|
8
|
+
- 建议使用 `uv` 或虚拟环境(`venv`/`conda`)隔离依赖
|
|
9
|
+
- 依赖安装(开发环境):`uv sync --dev`
|
|
10
|
+
- 发布包名为 `wecom-doc-sdk`,Python 导入名为 `wecom_doc_sdk`
|
|
11
|
+
- 如使用 VS Code/ty,解释器应绑定到仓库内 `.venv`,建议使用 `${workspaceFolder}\\.venv\\Scripts\\python.exe`
|
|
12
|
+
|
|
13
|
+
## 目录结构与职责边界
|
|
14
|
+
|
|
15
|
+
- `src/`:SDK 源码,所有业务代码放在此处
|
|
16
|
+
- `references/`:版本化维护的官方文档入口与参考索引
|
|
17
|
+
- `docs/`:企业微信文档与接口说明,仅作参考
|
|
18
|
+
- `tests/`:单元测试目录(如不存在,新增时保持该结构)
|
|
19
|
+
- 建议包结构示例:`src/wecom_doc_sdk/`、`src/wecom_doc_sdk/client.py`、`src/wecom_doc_sdk/models/`、`src/wecom_doc_sdk/apis/`
|
|
20
|
+
- 扩展约束:新增接口优先在 `apis/` 下增量扩展,避免接口逻辑堆在单一文件中
|
|
21
|
+
|
|
22
|
+
## 代码风格
|
|
23
|
+
|
|
24
|
+
- 命名:类用 `PascalCase`,函数/变量用 `snake_case`,常量用 `UPPER_SNAKE_CASE`
|
|
25
|
+
- 导入顺序:标准库 > 第三方库 > 本地模块,使用 `ruff` 自动整理
|
|
26
|
+
- 中文注释:关键逻辑、边界条件与业务语义需配中文注释
|
|
27
|
+
- Docstring:公开 API(对外可调用的函数/类)必须写简短 docstring
|
|
28
|
+
|
|
29
|
+
## 类型规范
|
|
30
|
+
|
|
31
|
+
- 所有函数与方法需要完整类型标注
|
|
32
|
+
- 公开接口必须明确输入与返回类型
|
|
33
|
+
- 优先使用 `typing` 中的显式类型(如 `Sequence`、`Mapping`)
|
|
34
|
+
|
|
35
|
+
## Pydantic 规范(v2)
|
|
36
|
+
|
|
37
|
+
- 模型统一继承 `pydantic.BaseModel`
|
|
38
|
+
- 字段别名、默认值、描述使用 `Field`
|
|
39
|
+
- 序列化/反序列化:输出用 `model_dump()`,输入用 `model_validate()`
|
|
40
|
+
- 如果模块内需要保留业务别名 `Field`,则统一使用 `from pydantic import Field as PydanticField`,避免与业务模型命名冲突
|
|
41
|
+
|
|
42
|
+
## HTTP 规范(httpx)
|
|
43
|
+
|
|
44
|
+
- 使用 `httpx.Client`/`httpx.AsyncClient` 复用连接,避免频繁创建
|
|
45
|
+
- 统一超时策略(例如:连接/读取超时都设定)
|
|
46
|
+
- 统一请求封装入口,避免在接口层直接拼接请求细节
|
|
47
|
+
- 需要时添加重试策略(可后续扩展,不在本次强制实现)
|
|
48
|
+
|
|
49
|
+
## 错误处理
|
|
50
|
+
|
|
51
|
+
- 统一使用 `WeComAPIError` 表示企业微信返回的业务错误,使用 `WeComRequestError` 表示网络、HTTP 状态或响应解析错误
|
|
52
|
+
- 业务成功始终以 `errcode == 0` 判断,不依赖 `errmsg` 文案做逻辑判断
|
|
53
|
+
- 对外暴露的错误信息应尽量保留:错误码、错误信息、原始响应、底层异常原因(如有)
|
|
54
|
+
- SDK 层不吞异常,必要时包装后抛出
|
|
55
|
+
|
|
56
|
+
## 测试规范
|
|
57
|
+
|
|
58
|
+
- 使用 `pytest`,测试文件以 `test_*.py` 命名
|
|
59
|
+
- 测试目录统一放在 `tests/`
|
|
60
|
+
- 新增接口应配套至少 1 个核心路径测试
|
|
61
|
+
|
|
62
|
+
## Git 流程与提交规范
|
|
63
|
+
|
|
64
|
+
- 分支命名:`feature/*` 新功能,`fix/*` 缺陷修复,`docs/*` 文档更新
|
|
65
|
+
- 提交信息格式:`type: summary`,示例:`feat: add smart table client`,类型可用 `feat`/`fix`/`docs`/`refactor`/`test`/`chore`
|
|
66
|
+
- 版本发布:遵循 SemVer(`MAJOR.MINOR.PATCH`),打 tag 时与版本号一致
|
|
67
|
+
|
|
68
|
+
## 发布前检查
|
|
69
|
+
|
|
70
|
+
- 确认 `pyproject.toml` 中的 `project.name`、`version`、`description` 与 README 描述一致
|
|
71
|
+
- 确认 README 中安装命令使用 `wecom-doc-sdk`,导入示例使用 `wecom_doc_sdk`
|
|
72
|
+
- 确认公共导出与 README 示例保持一致
|
|
73
|
+
- 发布前至少执行一次完整质量检查:`uv run ruff check .`、`uv run black --check .`、`uv run ty check`、`uv run pytest`
|
|
74
|
+
- 发布前至少执行一次本地打包与元数据检查:`uv build`、`uvx twine check dist/*`
|
|
75
|
+
|
|
76
|
+
## 发布命令(uv)
|
|
77
|
+
|
|
78
|
+
- 本地构建:`uv build`
|
|
79
|
+
- 分发包检查:`uvx twine check dist/*`
|
|
80
|
+
- 发布到 TestPyPI:`uv publish --index testpypi`
|
|
81
|
+
- 发布到 PyPI:`uv publish`
|
|
82
|
+
- 仓库已提供 GitHub Actions Trusted Publishing 工作流;发布 GitHub Release 后会触发 `.github/workflows/publish.yml` 自动上传到 PyPI
|
|
83
|
+
|
|
84
|
+
## 质量检查
|
|
85
|
+
|
|
86
|
+
- 代码风格:`uv run ruff check .`
|
|
87
|
+
- 代码格式:`uv run black --check .`
|
|
88
|
+
- 类型检查:`uv run ty check`
|
|
89
|
+
- 测试:`uv run pytest`
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wecom-doc-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 企业微信文档 SDK
|
|
5
|
+
Project-URL: Homepage, https://github.com/zzwtsy/wecom-doc-sdk
|
|
6
|
+
Project-URL: Repository, https://github.com/zzwtsy/wecom-doc-sdk
|
|
7
|
+
Author-email: zzwtsy <zzwtsy@yumdeb.top>
|
|
8
|
+
Keywords: docs,sdk,smartsheet,wechat-work,wecom
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Typing :: Typed
|
|
14
|
+
Requires-Python: ~=3.10.0
|
|
15
|
+
Requires-Dist: httpx>=0.28.1
|
|
16
|
+
Requires-Dist: pydantic>=2.12.5
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# 企业微信文档 SDK
|
|
20
|
+
|
|
21
|
+
`wecom-doc-sdk` 是一个面向企业微信文档相关服务端 API 的 Python SDK,当前聚焦“文档 -> 管理智能表格内容”能力,并为后续扩展更多文档接口保留了清晰的客户端与模型结构。
|
|
22
|
+
|
|
23
|
+
## 特性
|
|
24
|
+
|
|
25
|
+
- 基于 `httpx.Client` 的同步客户端,复用连接并统一处理超时
|
|
26
|
+
- 内置 `access_token` 获取、缓存和提前刷新逻辑
|
|
27
|
+
- 使用 `pydantic v2` 建模请求与响应,便于校验和序列化
|
|
28
|
+
- 完整类型标注,适合编辑器补全与静态检查
|
|
29
|
+
- 对企业微信业务错误与请求错误做了统一异常封装
|
|
30
|
+
- 代码和公开模型带中文注释,尽量降低接入与维护成本
|
|
31
|
+
|
|
32
|
+
## 当前支持
|
|
33
|
+
|
|
34
|
+
当前已封装企业微信“管理智能表格内容”中的以下能力:
|
|
35
|
+
|
|
36
|
+
- 子表:添加、删除、更新、查询
|
|
37
|
+
- 视图:添加、删除、更新、查询
|
|
38
|
+
- 字段:添加、删除、更新、查询
|
|
39
|
+
- 记录:添加、删除、更新、查询
|
|
40
|
+
- 编组:添加、更新、删除、查询
|
|
41
|
+
|
|
42
|
+
对应入口为 `WeComClient.smartsheet`。
|
|
43
|
+
|
|
44
|
+
## 安装
|
|
45
|
+
|
|
46
|
+
要求 Python `3.10+`。
|
|
47
|
+
|
|
48
|
+
使用 `pip`:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install wecom-doc-sdk
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
使用 `uv`:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
uv add wecom-doc-sdk
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
如果是本地开发安装:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
uv sync --dev
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## 快速开始
|
|
67
|
+
|
|
68
|
+
### 创建客户端
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from wecom_doc_sdk import WeComClient
|
|
72
|
+
|
|
73
|
+
with WeComClient(
|
|
74
|
+
corp_id="YOUR_CORP_ID",
|
|
75
|
+
corp_secret="YOUR_CORP_SECRET",
|
|
76
|
+
) as client:
|
|
77
|
+
sheets = client.smartsheet.get_sheet({"docid": "DOCID"})
|
|
78
|
+
print(sheets.ok, sheets.sheet_list)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 使用 Pydantic 模型查询字段
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from wecom_doc_sdk import GetFieldsRequest, WeComClient
|
|
85
|
+
|
|
86
|
+
with WeComClient(
|
|
87
|
+
corp_id="YOUR_CORP_ID",
|
|
88
|
+
corp_secret="YOUR_CORP_SECRET",
|
|
89
|
+
) as client:
|
|
90
|
+
response = client.smartsheet.get_fields(
|
|
91
|
+
GetFieldsRequest(
|
|
92
|
+
docid="DOCID",
|
|
93
|
+
sheet_id="SHEET_ID",
|
|
94
|
+
limit=100,
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
if response.fields:
|
|
99
|
+
for field in response.fields:
|
|
100
|
+
print(field.field_id, field.field_title, field.field_type)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 直接使用 `dict` 新增记录
|
|
104
|
+
|
|
105
|
+
SDK 的公开 API 同时接受 Pydantic 模型或 `dict`。
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from wecom_doc_sdk import WeComClient
|
|
109
|
+
|
|
110
|
+
with WeComClient(
|
|
111
|
+
corp_id="YOUR_CORP_ID",
|
|
112
|
+
corp_secret="YOUR_CORP_SECRET",
|
|
113
|
+
) as client:
|
|
114
|
+
response = client.smartsheet.add_records(
|
|
115
|
+
{
|
|
116
|
+
"docid": "DOCID",
|
|
117
|
+
"sheet_id": "SHEET_ID",
|
|
118
|
+
"records": [
|
|
119
|
+
{
|
|
120
|
+
"values": {
|
|
121
|
+
"标题": [{"type": "text", "text": "第一条任务"}],
|
|
122
|
+
"进度": 50,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
}
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
print(response.ok, response.records)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## 错误处理
|
|
133
|
+
|
|
134
|
+
SDK 将错误分成两类:
|
|
135
|
+
|
|
136
|
+
- `WeComAPIError`:企业微信接口已返回响应,但 `errcode != 0`
|
|
137
|
+
- `WeComRequestError`:网络异常、HTTP 状态异常或响应解析失败
|
|
138
|
+
|
|
139
|
+
推荐按下面的方式处理:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from wecom_doc_sdk import WeComAPIError, WeComRequestError, WeComClient
|
|
143
|
+
|
|
144
|
+
try:
|
|
145
|
+
with WeComClient(
|
|
146
|
+
corp_id="YOUR_CORP_ID",
|
|
147
|
+
corp_secret="YOUR_CORP_SECRET",
|
|
148
|
+
) as client:
|
|
149
|
+
client.smartsheet.get_sheet({"docid": "DOCID"})
|
|
150
|
+
except WeComAPIError as exc:
|
|
151
|
+
print("业务错误", exc.errcode, exc.errmsg)
|
|
152
|
+
print("原始响应", exc.raw)
|
|
153
|
+
except WeComRequestError as exc:
|
|
154
|
+
print("请求失败", exc)
|
|
155
|
+
print("底层原因", exc.cause)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## 设计约定
|
|
159
|
+
|
|
160
|
+
- `access_token` 仅在服务端获取和缓存,不应返回给前端
|
|
161
|
+
- 业务成功与否始终以 `errcode` 判断,不应依赖 `errmsg` 文案
|
|
162
|
+
- 所有请求统一通过 `WeComClient.request_json()` 注入 `access_token`
|
|
163
|
+
- 所有模型统一通过 `model_dump(by_alias=True, exclude_none=True)` 序列化
|
|
164
|
+
- 当前客户端为同步版;如后续需要,可在现有结构上扩展异步能力
|
|
165
|
+
|
|
166
|
+
## 常用导出
|
|
167
|
+
|
|
168
|
+
根包 `wecom_doc_sdk` 已导出常用客户端、异常和请求/响应模型,例如:
|
|
169
|
+
|
|
170
|
+
- `WeComClient`
|
|
171
|
+
- `AccessTokenProvider`
|
|
172
|
+
- `WeComAPIError`
|
|
173
|
+
- `WeComRequestError`
|
|
174
|
+
- `AddSheetRequest`
|
|
175
|
+
- `GetFieldsRequest`
|
|
176
|
+
- `AddRecordsRequest`
|
|
177
|
+
- `UpdateViewRequest`
|
|
178
|
+
|
|
179
|
+
更多模型可从 `wecom_doc_sdk.models` 导入。
|
|
180
|
+
|
|
181
|
+
## 项目结构
|
|
182
|
+
|
|
183
|
+
```text
|
|
184
|
+
references/ # 官方文档入口与实现参考索引
|
|
185
|
+
src/wecom_doc_sdk/
|
|
186
|
+
├── apis/ # 接口封装
|
|
187
|
+
├── models/ # Pydantic 模型与枚举
|
|
188
|
+
├── client.py # 客户端、鉴权与统一请求入口
|
|
189
|
+
└── exceptions.py # 异常定义
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
维护时可参考 `references/wecom-docs.md` 中整理的官方文档入口与当前实现对应条目。
|
|
193
|
+
|
|
194
|
+
## 开发
|
|
195
|
+
|
|
196
|
+
安装开发依赖:
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
uv sync --dev
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
常用检查命令:
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
uv run ruff check .
|
|
206
|
+
uv run black --check .
|
|
207
|
+
uv run ty check
|
|
208
|
+
uv run pytest
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## 发布
|
|
212
|
+
|
|
213
|
+
本地构建与检查:
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
uv build
|
|
217
|
+
uvx twine check dist/*
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
发布到 TestPyPI:
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
uv publish --index testpypi
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
发布到 PyPI:
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
uv publish
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
项目约定与贡献规范见 `CONTRIBUTING.md`。
|
|
233
|
+
|
|
234
|
+
## 适用范围
|
|
235
|
+
|
|
236
|
+
这个库当前聚焦企业微信文档能力,尤其是智能表格内容管理,但项目结构已经按“可扩展库”来组织。后续可以在保持现有客户端与模型风格一致的前提下,继续扩展更多企业微信文档相关 API 模块。
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# 企业微信文档 SDK
|
|
2
|
+
|
|
3
|
+
`wecom-doc-sdk` 是一个面向企业微信文档相关服务端 API 的 Python SDK,当前聚焦“文档 -> 管理智能表格内容”能力,并为后续扩展更多文档接口保留了清晰的客户端与模型结构。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 基于 `httpx.Client` 的同步客户端,复用连接并统一处理超时
|
|
8
|
+
- 内置 `access_token` 获取、缓存和提前刷新逻辑
|
|
9
|
+
- 使用 `pydantic v2` 建模请求与响应,便于校验和序列化
|
|
10
|
+
- 完整类型标注,适合编辑器补全与静态检查
|
|
11
|
+
- 对企业微信业务错误与请求错误做了统一异常封装
|
|
12
|
+
- 代码和公开模型带中文注释,尽量降低接入与维护成本
|
|
13
|
+
|
|
14
|
+
## 当前支持
|
|
15
|
+
|
|
16
|
+
当前已封装企业微信“管理智能表格内容”中的以下能力:
|
|
17
|
+
|
|
18
|
+
- 子表:添加、删除、更新、查询
|
|
19
|
+
- 视图:添加、删除、更新、查询
|
|
20
|
+
- 字段:添加、删除、更新、查询
|
|
21
|
+
- 记录:添加、删除、更新、查询
|
|
22
|
+
- 编组:添加、更新、删除、查询
|
|
23
|
+
|
|
24
|
+
对应入口为 `WeComClient.smartsheet`。
|
|
25
|
+
|
|
26
|
+
## 安装
|
|
27
|
+
|
|
28
|
+
要求 Python `3.10+`。
|
|
29
|
+
|
|
30
|
+
使用 `pip`:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install wecom-doc-sdk
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
使用 `uv`:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
uv add wecom-doc-sdk
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
如果是本地开发安装:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
uv sync --dev
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 快速开始
|
|
49
|
+
|
|
50
|
+
### 创建客户端
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from wecom_doc_sdk import WeComClient
|
|
54
|
+
|
|
55
|
+
with WeComClient(
|
|
56
|
+
corp_id="YOUR_CORP_ID",
|
|
57
|
+
corp_secret="YOUR_CORP_SECRET",
|
|
58
|
+
) as client:
|
|
59
|
+
sheets = client.smartsheet.get_sheet({"docid": "DOCID"})
|
|
60
|
+
print(sheets.ok, sheets.sheet_list)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 使用 Pydantic 模型查询字段
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from wecom_doc_sdk import GetFieldsRequest, WeComClient
|
|
67
|
+
|
|
68
|
+
with WeComClient(
|
|
69
|
+
corp_id="YOUR_CORP_ID",
|
|
70
|
+
corp_secret="YOUR_CORP_SECRET",
|
|
71
|
+
) as client:
|
|
72
|
+
response = client.smartsheet.get_fields(
|
|
73
|
+
GetFieldsRequest(
|
|
74
|
+
docid="DOCID",
|
|
75
|
+
sheet_id="SHEET_ID",
|
|
76
|
+
limit=100,
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
if response.fields:
|
|
81
|
+
for field in response.fields:
|
|
82
|
+
print(field.field_id, field.field_title, field.field_type)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 直接使用 `dict` 新增记录
|
|
86
|
+
|
|
87
|
+
SDK 的公开 API 同时接受 Pydantic 模型或 `dict`。
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from wecom_doc_sdk import WeComClient
|
|
91
|
+
|
|
92
|
+
with WeComClient(
|
|
93
|
+
corp_id="YOUR_CORP_ID",
|
|
94
|
+
corp_secret="YOUR_CORP_SECRET",
|
|
95
|
+
) as client:
|
|
96
|
+
response = client.smartsheet.add_records(
|
|
97
|
+
{
|
|
98
|
+
"docid": "DOCID",
|
|
99
|
+
"sheet_id": "SHEET_ID",
|
|
100
|
+
"records": [
|
|
101
|
+
{
|
|
102
|
+
"values": {
|
|
103
|
+
"标题": [{"type": "text", "text": "第一条任务"}],
|
|
104
|
+
"进度": 50,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
}
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
print(response.ok, response.records)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## 错误处理
|
|
115
|
+
|
|
116
|
+
SDK 将错误分成两类:
|
|
117
|
+
|
|
118
|
+
- `WeComAPIError`:企业微信接口已返回响应,但 `errcode != 0`
|
|
119
|
+
- `WeComRequestError`:网络异常、HTTP 状态异常或响应解析失败
|
|
120
|
+
|
|
121
|
+
推荐按下面的方式处理:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from wecom_doc_sdk import WeComAPIError, WeComRequestError, WeComClient
|
|
125
|
+
|
|
126
|
+
try:
|
|
127
|
+
with WeComClient(
|
|
128
|
+
corp_id="YOUR_CORP_ID",
|
|
129
|
+
corp_secret="YOUR_CORP_SECRET",
|
|
130
|
+
) as client:
|
|
131
|
+
client.smartsheet.get_sheet({"docid": "DOCID"})
|
|
132
|
+
except WeComAPIError as exc:
|
|
133
|
+
print("业务错误", exc.errcode, exc.errmsg)
|
|
134
|
+
print("原始响应", exc.raw)
|
|
135
|
+
except WeComRequestError as exc:
|
|
136
|
+
print("请求失败", exc)
|
|
137
|
+
print("底层原因", exc.cause)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## 设计约定
|
|
141
|
+
|
|
142
|
+
- `access_token` 仅在服务端获取和缓存,不应返回给前端
|
|
143
|
+
- 业务成功与否始终以 `errcode` 判断,不应依赖 `errmsg` 文案
|
|
144
|
+
- 所有请求统一通过 `WeComClient.request_json()` 注入 `access_token`
|
|
145
|
+
- 所有模型统一通过 `model_dump(by_alias=True, exclude_none=True)` 序列化
|
|
146
|
+
- 当前客户端为同步版;如后续需要,可在现有结构上扩展异步能力
|
|
147
|
+
|
|
148
|
+
## 常用导出
|
|
149
|
+
|
|
150
|
+
根包 `wecom_doc_sdk` 已导出常用客户端、异常和请求/响应模型,例如:
|
|
151
|
+
|
|
152
|
+
- `WeComClient`
|
|
153
|
+
- `AccessTokenProvider`
|
|
154
|
+
- `WeComAPIError`
|
|
155
|
+
- `WeComRequestError`
|
|
156
|
+
- `AddSheetRequest`
|
|
157
|
+
- `GetFieldsRequest`
|
|
158
|
+
- `AddRecordsRequest`
|
|
159
|
+
- `UpdateViewRequest`
|
|
160
|
+
|
|
161
|
+
更多模型可从 `wecom_doc_sdk.models` 导入。
|
|
162
|
+
|
|
163
|
+
## 项目结构
|
|
164
|
+
|
|
165
|
+
```text
|
|
166
|
+
references/ # 官方文档入口与实现参考索引
|
|
167
|
+
src/wecom_doc_sdk/
|
|
168
|
+
├── apis/ # 接口封装
|
|
169
|
+
├── models/ # Pydantic 模型与枚举
|
|
170
|
+
├── client.py # 客户端、鉴权与统一请求入口
|
|
171
|
+
└── exceptions.py # 异常定义
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
维护时可参考 `references/wecom-docs.md` 中整理的官方文档入口与当前实现对应条目。
|
|
175
|
+
|
|
176
|
+
## 开发
|
|
177
|
+
|
|
178
|
+
安装开发依赖:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
uv sync --dev
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
常用检查命令:
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
uv run ruff check .
|
|
188
|
+
uv run black --check .
|
|
189
|
+
uv run ty check
|
|
190
|
+
uv run pytest
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## 发布
|
|
194
|
+
|
|
195
|
+
本地构建与检查:
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
uv build
|
|
199
|
+
uvx twine check dist/*
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
发布到 TestPyPI:
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
uv publish --index testpypi
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
发布到 PyPI:
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
uv publish
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
项目约定与贡献规范见 `CONTRIBUTING.md`。
|
|
215
|
+
|
|
216
|
+
## 适用范围
|
|
217
|
+
|
|
218
|
+
这个库当前聚焦企业微信文档能力,尤其是智能表格内容管理,但项目结构已经按“可扩展库”来组织。后续可以在保持现有客户端与模型风格一致的前提下,继续扩展更多企业微信文档相关 API 模块。
|