Skip to content

Commit 5b2b0aa

Browse files
committed
feat(sdk): 添加 Python SDK代码模板
- 新增 Python SDK 相关的多个模块和文件 - 实现了基础 API客户端、Flexmodel 客户端和工厂类 - 添加了实体类和枚举类的生成器 - 提供了示例模块和使用示例
1 parent 1021c49 commit 5b2b0aa

23 files changed

+2107
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"packageName": "flexmodel-python-sdk",
3+
"version": "1.0.0",
4+
"author": "Flexmodel Team"
5+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# Flexmodel Python SDK 模板
2+
3+
该模板提供访问 Flexmodel 后端的简易 Python SDK,支持直接访问实体类型,无需通用封装。
4+
5+
## 特性
6+
7+
- **直接实体访问**`users = user_api.list_users()` 而不是 `ApiResponse[PageResult[Dict[str, Any]]]`
8+
- **类型安全**:强类型实体类,运行时类型检查
9+
- **便捷API**:为每个实体生成专门的API类
10+
- **灵活查询**:支持分页、过滤、排序等查询参数
11+
- **简单易用**:工厂模式创建客户端,一行代码开始使用
12+
- **Pydantic支持**:使用Pydantic进行数据验证和序列化
13+
14+
## 快速开始
15+
16+
### 1. 安装依赖
17+
18+
```bash
19+
pip install -e .
20+
```
21+
22+
### 2. 创建客户端
23+
24+
```python
25+
from src.client.flexmodel_client_factory import FlexmodelClientFactory
26+
27+
# 使用工厂方法创建客户端
28+
client = FlexmodelClientFactory.create_client(
29+
'http://localhost:8080',
30+
'myDatasource'
31+
)
32+
33+
# 或者手动创建
34+
from src.client.api_client import ApiClient
35+
from src.client.flexmodel_client import FlexmodelClient
36+
37+
api_client = ApiClient('http://localhost:8080')
38+
client = FlexmodelClient(api_client, 'myDatasource')
39+
```
40+
41+
### 3. 直接访问实体
42+
43+
```python
44+
from src.example.user_api import UserApi
45+
from src.example.user import User
46+
47+
# 获取用户API(示例)
48+
user_api = UserApi(client.api_client, 'myDatasource', 'user')
49+
50+
# 获取所有用户 - 直接返回User对象列表
51+
users = user_api.list_users_simple()
52+
53+
# 带条件查询
54+
active_users = user_api.list_users_as_list(
55+
filter_expr="status='active'",
56+
sort='name asc'
57+
)
58+
59+
# 分页查询
60+
user_page = user_api.list_users(current=1, page_size=10)
61+
62+
# 获取单个用户
63+
user = user_api.get_user('123')
64+
65+
# 创建用户
66+
new_user = User(
67+
name='张三',
68+
69+
age=25
70+
)
71+
created_user = user_api.create_user(new_user)
72+
73+
# 更新用户
74+
user.set_age(26)
75+
updated_user = user_api.update_user(user.get_id(), user)
76+
77+
# 删除用户
78+
user_api.delete_user(user.get_id())
79+
```
80+
81+
### 4. 访问多个实体
82+
83+
```python
84+
from src.example.product_api import ProductApi
85+
from src.example.order_api import OrderApi
86+
87+
# 用户相关操作(示例)
88+
user_api = UserApi(client.api_client, 'myDatasource', 'user')
89+
users = user_api.list_users_simple()
90+
91+
# 产品相关操作(示例)
92+
product_api = ProductApi(client.api_client, 'myDatasource', 'product')
93+
products = product_api.list_products_simple()
94+
95+
# 订单相关操作(示例)
96+
order_api = OrderApi(client.api_client, 'myDatasource', 'order')
97+
orders = order_api.list_orders_simple()
98+
```
99+
100+
## API 方法说明
101+
102+
每个实体API都提供以下方法:
103+
104+
- `list_{entity}s_simple()` - 获取所有记录(直接返回列表)
105+
- `list_{entity}s_as_list(options)` - 带条件查询
106+
- `list_{entity}s(options)` - 分页查询
107+
- `get_{entity}(id)` - 根据ID获取记录
108+
- `create_{entity}(entity)` - 创建记录
109+
- `update_{entity}(id, entity)` - 更新记录
110+
- `patch_{entity}(id, entity)` - 部分更新记录
111+
- `delete_{entity}(id)` - 删除记录
112+
113+
## 通用API
114+
115+
如果需要使用通用API(返回Dict类型),可以使用:
116+
117+
```python
118+
records_api = client.get_records_api()
119+
records = records_api.list_records(
120+
'datasource', 'model',
121+
current=1, page_size=10
122+
)
123+
```
124+
125+
## 配置
126+
127+
### 认证
128+
129+
```python
130+
# API Key认证
131+
client = FlexmodelClientFactory.create_client_with_api_key(
132+
'http://localhost:8080',
133+
'myDatasource',
134+
'your-api-key'
135+
)
136+
137+
# 用户名密码认证
138+
client = FlexmodelClientFactory.create_client_with_credentials(
139+
'http://localhost:8080',
140+
'myDatasource',
141+
'username',
142+
'password'
143+
)
144+
```
145+
146+
### 开发环境
147+
148+
```python
149+
# 使用默认配置(localhost:8080)
150+
client = FlexmodelClientFactory.create_default_client('myDatasource')
151+
```
152+
153+
## 实体生成
154+
155+
模板包含实体生成器,会根据模型定义自动生成:
156+
- 实体类(如User.py)
157+
- API类(如UserApi.py)
158+
159+
## 示例代码
160+
161+
所有示例代码都位于 `example` 包中:
162+
- `example/user.py` - 用户实体示例
163+
- `example/product.py` - 产品实体示例
164+
- `example/order.py` - 订单实体示例
165+
- `example/user_api.py` - 用户API示例
166+
- `example/product_api.py` - 产品API示例
167+
- `example/order_api.py` - 订单API示例
168+
- `example/flexmodel_example.py` - 完整使用示例
169+
170+
## 构建和开发
171+
172+
```bash
173+
# 安装开发依赖
174+
pip install -e ".[dev]"
175+
176+
# 代码格式化
177+
black src/
178+
179+
# 代码检查
180+
flake8 src/
181+
182+
# 类型检查
183+
mypy src/
184+
185+
# 运行测试
186+
pytest
187+
```
188+
189+
## 依赖
190+
191+
- requests: HTTP客户端
192+
- pydantic: 数据验证和序列化
193+
- Python 3.8+
194+
195+
## 注意事项
196+
197+
- 示例代码位于 `example` 包中,实际使用时需要根据您的实体类型进行调整
198+
- `FlexmodelClient` 中的便捷方法(如 `users()`, `products()` 等)是示例方法,需要根据实际生成的实体类型进行修改
199+
- 需要Python 3.8+环境
200+
- 使用Pydantic进行数据验证,确保数据类型的正确性
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[build-system]
2+
requires = ["setuptools>=45", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "${packageName}"
7+
version = "${version}"
8+
authors = [
9+
{name = "${author}", email = "${authorEmail}"}
10+
]
11+
description = "Flexmodel Python SDK - 直接访问实体类型,无需通用封装"
12+
readme = "README.md"
13+
license = {text = "MIT"}
14+
requires-python = ">=3.8"
15+
classifiers = [
16+
"Development Status :: 4 - Beta",
17+
"Intended Audience :: Developers",
18+
"License :: OSI Approved :: MIT License",
19+
"Operating System :: OS Independent",
20+
"Programming Language :: Python :: 3",
21+
"Programming Language :: Python :: 3.8",
22+
"Programming Language :: Python :: 3.9",
23+
"Programming Language :: Python :: 3.10",
24+
"Programming Language :: Python :: 3.11",
25+
"Programming Language :: Python :: 3.12",
26+
]
27+
keywords = ["flexmodel", "sdk", "python", "api", "client"]
28+
dependencies = [
29+
"requests>=2.25.0",
30+
"pydantic>=1.8.0",
31+
]
32+
33+
[project.optional-dependencies]
34+
dev = [
35+
"pytest>=6.0",
36+
"pytest-asyncio>=0.15.0",
37+
"black>=21.0",
38+
"flake8>=3.8",
39+
"mypy>=0.800",
40+
]
41+
42+
[project.urls]
43+
Homepage = "${repositoryUrl}"
44+
Documentation = "${documentationUrl}"
45+
Repository = "${repositoryUrl}"
46+
"Bug Reports" = "${bugReportsUrl}"
47+
48+
[tool.black]
49+
line-length = 88
50+
target-version = ['py38']
51+
52+
[tool.mypy]
53+
python_version = "3.8"
54+
warn_return_any = true
55+
warn_unused_configs = true
56+
disallow_untyped_defs = true
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from setuptools import setup, find_packages
2+
3+
with open("README.md", "r", encoding="utf-8") as fh:
4+
long_description = fh.read()
5+
6+
setup(
7+
name="${packageName}",
8+
version="${version}",
9+
author="${author}",
10+
author_email="${authorEmail}",
11+
description="Flexmodel Python SDK - 直接访问实体类型,无需通用封装",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="${repositoryUrl}",
15+
packages=find_packages(where="src"),
16+
package_dir={"": "src"},
17+
classifiers=[
18+
"Development Status :: 4 - Beta",
19+
"Intended Audience :: Developers",
20+
"License :: OSI Approved :: MIT License",
21+
"Operating System :: OS Independent",
22+
"Programming Language :: Python :: 3",
23+
"Programming Language :: Python :: 3.8",
24+
"Programming Language :: Python :: 3.9",
25+
"Programming Language :: Python :: 3.10",
26+
"Programming Language :: Python :: 3.11",
27+
"Programming Language :: Python :: 3.12",
28+
],
29+
python_requires=">=3.8",
30+
install_requires=[
31+
"requests>=2.25.0",
32+
"pydantic>=1.8.0",
33+
],
34+
extras_require={
35+
"dev": [
36+
"pytest>=6.0",
37+
"pytest-asyncio>=0.15.0",
38+
"black>=21.0",
39+
"flake8>=3.8",
40+
"mypy>=0.800",
41+
],
42+
},
43+
keywords="flexmodel sdk python api client",
44+
project_urls={
45+
"Bug Reports": "${bugReportsUrl}",
46+
"Source": "${repositoryUrl}",
47+
"Documentation": "${documentationUrl}",
48+
},
49+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Flexmodel Python SDK
3+
4+
直接访问实体类型,无需通用封装
5+
"""
6+
7+
__version__ = "${version}"
8+
__author__ = "${author}"
9+
10+
# 核心客户端
11+
from .client.api_client import ApiClient, ApiException
12+
from .client.flexmodel_client import FlexmodelClient
13+
from .client.flexmodel_client_factory import FlexmodelClientFactory
14+
15+
# API类
16+
from .api.records_api import RecordsApi
17+
from .api.page_result import PageResult
18+
19+
# 示例类(可选导入)
20+
from .example.user import User
21+
from .example.product import Product
22+
from .example.order import Order
23+
from .example.user_api import UserApi
24+
from .example.product_api import ProductApi
25+
from .example.order_api import OrderApi
26+
27+
__all__ = [
28+
# 版本信息
29+
"__version__",
30+
"__author__",
31+
32+
# 核心客户端
33+
"ApiClient",
34+
"ApiException",
35+
"FlexmodelClient",
36+
"FlexmodelClientFactory",
37+
38+
# API类
39+
"RecordsApi",
40+
"PageResult",
41+
42+
# 示例类
43+
"User",
44+
"Product",
45+
"Order",
46+
"UserApi",
47+
"ProductApi",
48+
"OrderApi",
49+
]

0 commit comments

Comments
 (0)