重构第2批:API 层加固 + 新增 service 层

api/__init__.py:
- EkbClient.get/post 捕获 HTTP 错误,转换为 AuthenticationError /
  ApiResponseError;识别快报 errorCode=401 结构
- get_client() 改为抛出 NotLoggedInError(原为 RuntimeError)
- 新增 close() 方法便于非 with 场景使用

新增 services/ 目录(业务逻辑层):
- auth_service.py:企业认证配置的增删查改,返回 CorpInfo dataclass
- agent_service.py:Agent CRUD + copy + 高阶组合查询 get_dedup_rules_across_agents
  AgentCreateInput / AgentUpdateInput dataclass 统一输入结构
- rule_service.py:Rule 查询(支持按编码/ID 查找)+ CRUD
- __init__.py:统一 re-export,上层只从此处导入
This commit is contained in:
gitea committed 2026-04-14 11:06:06 +08:00
1 parent 4faba77234
commit 475ce518f9
5 files changed
+491 -10

No files matched your search

+41 -10
View File
@@ -1,8 +1,17 @@
"""HTTP 客户端:封装对快报 API 的请求,使用 accesstoken + corpid + staffid 认证"""
"""HTTP 客户端:封装对快报 API 的请求,使用 accesstoken + corpid + staffid 认证
异常约定:
- HTTP 401 → AuthenticationError
- HTTP 其他非 2xx → ApiResponseError
- 快报业务码非 200 → ApiResponseError
- 未登录(无配置)→ NotLoggedInError
"""
import httpx
from typing import Any
from ekb_cli.errors import AuthenticationError, ApiResponseError, NotLoggedInError
BASE_URL = "https://app.ekuaibao.com"
@@ -25,15 +34,34 @@ class EkbClient:
def get(self, path: str, **params) -> Any:
params["corpId"] = self.corp_id
resp = self._client.get(f"/api{path}", params=params)
resp.raise_for_status()
return resp.json()
try:
resp = self._client.get(f"/api{path}", params=params)
resp.raise_for_status()
except httpx.HTTPStatusError as e:
if e.response.status_code == 401:
raise AuthenticationError()
raise ApiResponseError(e.response.status_code, str(e))
data = resp.json()
# 快报 errorCode 结构(未登录等场景)
if isinstance(data, dict) and data.get("errorCode") == 401:
raise AuthenticationError(data.get("errorMessage", "认证失败"))
return data
def post(self, path: str, body: dict = None, **params) -> Any:
params["corpId"] = self.corp_id
resp = self._client.post(f"/api{path}", json=body or {}, params=params)
resp.raise_for_status()
return resp.json()
try:
resp = self._client.post(f"/api{path}", json=body or {}, params=params)
resp.raise_for_status()
except httpx.HTTPStatusError as e:
if e.response.status_code == 401:
raise AuthenticationError()
raise ApiResponseError(e.response.status_code, str(e))
data = resp.json()
if isinstance(data, dict) and data.get("errorCode") == 401:
raise AuthenticationError(data.get("errorMessage", "认证失败"))
return data
def __enter__(self):
return self
@@ -41,13 +69,16 @@ class EkbClient:
def __exit__(self, *args):
self._client.close()
def close(self):
self._client.close()
def get_client() -> EkbClient:
"""从当前配置获取已认证的客户端,未登录时抛出友好错误"""
"""从当前配置获取已认证的客户端,未登录时抛出 NotLoggedInError"""
from ekb_cli import config
corp = config.get_current_corp()
if not corp:
raise RuntimeError("未登录,请先执行 `ekb auth add` 添加企业认证")
raise NotLoggedInError()
corp_id = config.get_current_corp_id()
return EkbClient(
corp_id=corp_id,
@@ -56,7 +87,7 @@ def get_client() -> EkbClient:
)
# ── 统一 re-export:commands 只从这里导入,内部拆分对外透明 ──────────
# ── 统一 re-export:commands / services 只从这里导入 ──────────
from ekb_cli.api.agent import ( # noqa: E402
check_agent_name,
list_agents,