refactor: 拆分 agent 模块为 commands/agent/ 和 api/agent.py
This commit is contained in:
1 parent
3c4ad6256a
commit
cd78261cae
7 files changed
+44
-12
No files matched your search
@@ -0,0 +1,56 @@
|
||||
"""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"],
|
||||
)
|
||||
Reference in new issue
Block a user