|
| 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进行数据验证,确保数据类型的正确性 |
0 commit comments