新增 SealAI 子模块:认证层 + API 传输层 + 服务层
架构:融入现有三层结构,SealAI 作为各层的子目录与 HAB 模块并列 api/sealai/ - SealAiClient(Bearer JWT)+ auth_api + billing_api + schemas services/sealai/ - auth_service(SSO换token/缓存)+ billing_service config.py - 新增 get_corp(corp_id) 按ID查询企业配置 认证流程:快报 accesstoken → SSO 换 Bearer JWT → 缓存到 sealai_tokens.json 已验证:auth/me 和 billing/status 端点正常返回数据
This commit is contained in:
1 parent
bfce1fce77
commit
9d517defe3
10 files changed
+405
No files matched your search
@@ -0,0 +1,14 @@
|
||||
"""SealAI 传输层统一 re-export。
|
||||
|
||||
使用方(services/sealai/)只从这里导入,内部文件如何拆分对上层透明。
|
||||
"""
|
||||
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.auth_api import get_me
|
||||
from ekb_cli.api.sealai.billing_api import get_billing_status
|
||||
|
||||
__all__ = [
|
||||
"SealAiClient",
|
||||
"get_me",
|
||||
"get_billing_status",
|
||||
]
|
||||
@@ -0,0 +1,14 @@
|
||||
"""SealAI 认证相关 API 封装(传输层)。
|
||||
|
||||
已逆向确认端点:
|
||||
GET /api/v1/auth/me 当前用户及租户信息
|
||||
"""
|
||||
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.schemas import MeData, SealAiResponse
|
||||
|
||||
|
||||
def get_me(client: SealAiClient) -> MeData:
|
||||
"""获取当前登录用户及租户信息。"""
|
||||
raw = client.get("/api/v1/auth/me")
|
||||
return SealAiResponse[MeData].model_validate(raw).data
|
||||
@@ -0,0 +1,14 @@
|
||||
"""SealAI 计费 / 订阅相关 API 封装(传输层)。
|
||||
|
||||
已逆向确认端点:
|
||||
GET /api/v1/billing/status 订阅状态 + 功能配额列表
|
||||
"""
|
||||
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.schemas import BillingStatusData, SealAiResponse
|
||||
|
||||
|
||||
def get_billing_status(client: SealAiClient) -> BillingStatusData:
|
||||
"""获取订阅状态及各功能配额使用情况。"""
|
||||
raw = client.get("/api/v1/billing/status")
|
||||
return SealAiResponse[BillingStatusData].model_validate(raw).data
|
||||
@@ -0,0 +1,82 @@
|
||||
"""SealAI HTTP 客户端(传输层)。
|
||||
|
||||
与快报主站的 EkbClient 并列,同属 api/ 传输层。
|
||||
区别:SealAI 用 Bearer JWT 认证,域名按企业 corpId 动态生成。
|
||||
|
||||
使用方:services/sealai/ 通过 get_sealai_client() 获取实例,
|
||||
不直接 import 此模块。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
import httpx
|
||||
from typing import Any
|
||||
|
||||
from ekb_cli.errors import AuthenticationError, ApiResponseError
|
||||
|
||||
|
||||
class SealAiClient:
|
||||
"""SealAI REST 客户端,所有请求携带 Authorization: Bearer {token}。"""
|
||||
|
||||
def __init__(self, corp_id: str, bearer_token: str):
|
||||
self.corp_id = corp_id
|
||||
self._base_url = f"https://{corp_id.lower()}.sealai.cc"
|
||||
self._client = httpx.Client(
|
||||
base_url=self._base_url,
|
||||
headers={
|
||||
"Authorization": f"Bearer {bearer_token}",
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/146.0.0.0 Safari/537.36"
|
||||
),
|
||||
},
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
def _handle_error(self, e: httpx.HTTPStatusError) -> None:
|
||||
if e.response.status_code == 401:
|
||||
raise AuthenticationError("SealAI token 已过期,请重新登录")
|
||||
raise ApiResponseError(e.response.status_code, str(e))
|
||||
|
||||
def get(self, path: str, **params) -> Any:
|
||||
try:
|
||||
resp = self._client.get(path, params=params or None)
|
||||
resp.raise_for_status()
|
||||
except httpx.HTTPStatusError as e:
|
||||
self._handle_error(e)
|
||||
return resp.json()
|
||||
|
||||
def post(self, path: str, body: dict | None = None, **params) -> Any:
|
||||
try:
|
||||
resp = self._client.post(path, json=body or {}, params=params or None)
|
||||
resp.raise_for_status()
|
||||
except httpx.HTTPStatusError as e:
|
||||
self._handle_error(e)
|
||||
return resp.json()
|
||||
|
||||
def patch(self, path: str, body: dict | None = None) -> Any:
|
||||
try:
|
||||
resp = self._client.patch(path, json=body or {})
|
||||
resp.raise_for_status()
|
||||
except httpx.HTTPStatusError as e:
|
||||
self._handle_error(e)
|
||||
return resp.json()
|
||||
|
||||
def delete(self, path: str) -> Any:
|
||||
try:
|
||||
resp = self._client.delete(path)
|
||||
resp.raise_for_status()
|
||||
except httpx.HTTPStatusError as e:
|
||||
self._handle_error(e)
|
||||
return resp.json()
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self._client.close()
|
||||
|
||||
def close(self):
|
||||
self._client.close()
|
||||
@@ -0,0 +1,124 @@
|
||||
"""SealAI 平台的 Pydantic 响应模型。
|
||||
|
||||
SealAI({corpId}.sealai.cc)是独立于快报主站的 Next.js 平台,
|
||||
认证使用 Bearer JWT,数据结构与快报 HAB 模块完全独立。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import Generic, Optional, TypeVar
|
||||
from pydantic import BaseModel
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
# ── 通用包装 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
class SealAiResponse(BaseModel, Generic[T]):
|
||||
"""SealAI API 统一响应包装:{ data: T }"""
|
||||
data: T
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
# ── 认证 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
class HosecloudStaff(BaseModel):
|
||||
staffId: str
|
||||
name: str
|
||||
code: str = ""
|
||||
bindTime: int
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class SealAiUser(BaseModel):
|
||||
id: str
|
||||
tenantId: str
|
||||
name: str
|
||||
isAdmin: bool
|
||||
permissions: list[str]
|
||||
providerUserId: str # 对应快报 staffId
|
||||
hosecloudStaff: Optional[HosecloudStaff] = None
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class HoseCorpConfig(BaseModel):
|
||||
apiKey: str
|
||||
corporationId: str
|
||||
corporationName: str
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class SealAiTenant(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
tenantSlug: str # corpId 小写
|
||||
isDemo: bool
|
||||
logoUrl: Optional[str] = None
|
||||
hoseCorpConfig: HoseCorpConfig
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class MeData(BaseModel):
|
||||
user: SealAiUser
|
||||
tenant: SealAiTenant
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
# ── 计费 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
class Subscription(BaseModel):
|
||||
id: str
|
||||
tenantId: str
|
||||
planId: str
|
||||
status: str # "active" | "expired"
|
||||
startDate: str
|
||||
createdAt: str
|
||||
updatedAt: str
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class Feature(BaseModel):
|
||||
"""订阅功能项,三种形态:开关型 / 配额型 / 余额型"""
|
||||
code: str
|
||||
name: str
|
||||
# 开关型
|
||||
enabled: Optional[bool] = None
|
||||
# 配额型
|
||||
quota: Optional[int] = None
|
||||
used: Optional[int] = None
|
||||
remaining: Optional[int] = None
|
||||
# 余额型
|
||||
balance: Optional[int] = None
|
||||
totalQuota: Optional[int] = None
|
||||
totalUsed: Optional[int] = None
|
||||
nextResetDate: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class BillingStatusData(BaseModel):
|
||||
subscriptions: list[Subscription]
|
||||
features: list[Feature]
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
def get_feature(self, code: str) -> Optional[Feature]:
|
||||
for f in self.features:
|
||||
if f.code == code:
|
||||
return f
|
||||
return None
|
||||
Reference in new issue
Block a user