feat(fulfillment): 新增 order_site 客户绑定集成
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""order_site -> live-hub 的直播账号履约服务端集成接口。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from hmac import compare_digest
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..schemas import (
|
||||
PlatformFulfillmentCustomerBindingAction,
|
||||
PlatformFulfillmentCustomerSessionRequest,
|
||||
)
|
||||
from ..services.platform_fulfillment import service
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/integration/platform-fulfillment",
|
||||
tags=["直播账号履约集成"],
|
||||
)
|
||||
|
||||
|
||||
def require_integration_key(
|
||||
x_integration_key: str = Header("", alias="X-Integration-Key"),
|
||||
) -> None:
|
||||
expected = os.getenv("PLATFORM_FULFILLMENT_INTEGRATION_KEY", "").strip()
|
||||
if not expected:
|
||||
raise HTTPException(status_code=503, detail="直播账号履约集成密钥未配置")
|
||||
if not x_integration_key or not compare_digest(x_integration_key, expected):
|
||||
raise HTTPException(status_code=401, detail="直播账号履约集成鉴权失败")
|
||||
|
||||
|
||||
@router.post(
|
||||
"/customer-sessions/ensure", dependencies=[Depends(require_integration_key)]
|
||||
)
|
||||
def ensure_customer_session(
|
||||
req: PlatformFulfillmentCustomerSessionRequest,
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict[str, Any]:
|
||||
return service.ensure_customer_session(
|
||||
db,
|
||||
source_order_item_id=req.source_order_item_id,
|
||||
platform_order_id=req.platform_order_id,
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/customer-sessions/{task_id}/binding/start",
|
||||
dependencies=[Depends(require_integration_key)],
|
||||
)
|
||||
async def start_customer_binding(
|
||||
task_id: int,
|
||||
req: PlatformFulfillmentCustomerBindingAction,
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict[str, Any]:
|
||||
return await service.customer_start_binding(
|
||||
db,
|
||||
task_id=task_id,
|
||||
force_refresh=req.force_refresh,
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/customer-sessions/{task_id}/status",
|
||||
dependencies=[Depends(require_integration_key)],
|
||||
)
|
||||
def get_customer_binding_status(
|
||||
task_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict[str, Any]:
|
||||
return service.customer_refresh_binding(db, task_id=task_id)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/customer-sessions/{task_id}/binding/confirm",
|
||||
dependencies=[Depends(require_integration_key)],
|
||||
)
|
||||
async def confirm_customer_binding(
|
||||
task_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict[str, Any]:
|
||||
return await service.customer_confirm_binding(db, task_id=task_id)
|
||||
Reference in New Issue
Block a user