72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
"""HTTP 客户端:封装对快报 API 的请求,使用 accesstoken + corpid + staffid 认证"""
|
||
|
||
import httpx
|
||
from typing import Any
|
||
|
||
BASE_URL = "https://app.ekuaibao.com"
|
||
|
||
|
||
class EkbClient:
|
||
def __init__(self, corp_id: str, access_token: str, staff_id: str):
|
||
self.corp_id = corp_id
|
||
self._client = httpx.Client(
|
||
base_url=BASE_URL,
|
||
headers={
|
||
"accesstoken": access_token,
|
||
"corpid": corp_id,
|
||
"staffid": staff_id,
|
||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
|
||
"Accept": "application/json",
|
||
"Content-Type": "application/json",
|
||
"Accept-Language": "zh-CN,zh;q=0.9",
|
||
},
|
||
timeout=30,
|
||
)
|
||
|
||
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()
|
||
|
||
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()
|
||
|
||
def __enter__(self):
|
||
return self
|
||
|
||
def __exit__(self, *args):
|
||
self._client.close()
|
||
|
||
|
||
def get_client() -> EkbClient:
|
||
"""从当前配置获取已认证的客户端,未登录时抛出友好错误"""
|
||
from ekb_cli import config
|
||
corp = config.get_current_corp()
|
||
if not corp:
|
||
raise RuntimeError("未登录,请先执行 `ekb auth add` 添加企业认证")
|
||
corp_id = config.get_current_corp_id()
|
||
return EkbClient(
|
||
corp_id=corp_id,
|
||
access_token=corp["access_token"],
|
||
staff_id=corp["staff_id"],
|
||
)
|
||
|
||
|
||
# ── 统一 re-export:commands 只从这里导入,内部拆分对外透明 ──────────
|
||
from ekb_cli.api.agent import ( # noqa: E402
|
||
check_agent_name,
|
||
list_agents,
|
||
get_agent,
|
||
save_agent,
|
||
delete_agent,
|
||
)
|
||
from ekb_cli.api.rule import ( # noqa: E402
|
||
list_rules,
|
||
save_rule,
|
||
update_rule,
|
||
)
|