Files
hose-use/src/ekb_cli/api/README.api.md
T

85 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# API 层说明(`ekb_cli/api/`)
## 三层架构
```
api/
├── __init__.py # 第1层:HTTP 客户端(EkbClient)+ 统一 re-export
├── schemas.py # 第2层辅助:Pydantic 响应模型,所有接口响应在此校验
├── agent.py # 第2层:Agent 相关接口封装
└── rule.py # 第2层:Rule(审核规则)相关接口封装
```
**commands 只从 `ekb_cli.api` 导入**,内部文件如何拆分对上层透明:
```python
from ekb_cli.api import get_client, list_agents, get_agent, list_rules, ...
```
---
## `__init__.py` — HTTP 客户端
`EkbClient` 负责:带认证头(`accesstoken` / `corpid` / `staffid`)发 HTTP 请求,返回原始 JSON。它不理解业务。
| 方法 | 说明 |
|------|------|
| `get(path, **params)` | GET 请求,自动附加 `corpId` |
| `post(path, body, **params)` | POST 请求,body 为 JSON |
`get_client()` 从 `~/.config/ekb-cli/config.json` 读取当前企业认证信息,构建 `EkbClient`。
---
## `schemas.py` — Pydantic 响应模型
所有模型使用 `extra="allow"`(允许快报新增字段,但已知字段强制类型校验)。API 格式变更时,Pydantic 会抛出 `ValidationError`,精确定位到字段名。
| 模型 | 用途 |
|------|------|
| `EkbResponse[T]` | 通用响应包装(`code` / `msg` / `data`) |
| `PageData[T]` | 通用分页(`totalItems` / `items`) |
| `AgentSummary` | Agent 列表中的摘要条目(无 `agentRule`) |
| `AgentDetail` | Agent 详情(含 `agentRule` 列表) |
| `AgentRuleRef` | `AgentDetail` 中内嵌的规则完整字段 |
| `Rule` | 规则新建/更新的响应体 |
| `RuleWithAgents` | 规则列表中的条目(含 `agents` 关联摘要) |
| `RuleAgentRef` | `RuleWithAgents` 中关联 Agent 摘要(`id/name/icon`) |
---
## `agent.py` — Agent 接口
| 函数 | 方法 | 路径 | 返回 |
|------|------|------|------|
| `check_agent_name(client, name)` | GET | `/engine/hab/agent/checkName` | `bool`(`True`=可用) |
| `list_agents(client, page, size)` | GET | `/engine/hab/agent/list` | `PageData[AgentSummary]` |
| `get_agent(client, agent_id)` | GET | `/engine/hab/agent/detail` | `AgentDetail` |
| `save_agent(client, payload)` | POST | `/engine/hab/agent/save` | `AgentDetail` |
> `save_agent` 同时承担新建(无 `id`)和更新(有 `id`)。
---
## `rule.py` — Rule 接口
| 函数 | 方法 | 路径 | 返回 |
|------|------|------|------|
| `list_rules(client, page, size)` | GET | `/engine/hab/agent/rule/list` | `PageData[RuleWithAgents]` |
| `save_rule(client, payload)` | POST | `/engine/hab/agent/rule/save` | `Rule` |
| `update_rule(client, payload)` | POST | `/engine/hab/agent/rule/update` | `Rule` |
---
## 接口格式变更应对
快报前端更新后若接口格式改变,Pydantic 会在第一次调用时抛出 `ValidationError`,输出类似:
```
pydantic_core._pydantic_core.ValidationError: 1 validation error for AgentDetail
agentRule
Field required [type=missing, ...]
```
定位方式:字段名 → `schemas.py` 对应模型 → 对照实际响应 JSON 更新字段定义。