109 lines
3.1 KiB
Python
109 lines
3.1 KiB
Python
"""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,
|
|
PlatformFulfillmentCustomerSessionBatchRequest,
|
|
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(
|
|
"/events/order-sync",
|
|
dependencies=[Depends(require_integration_key)],
|
|
)
|
|
def sync_order_event(
|
|
payload: dict[str, Any],
|
|
db: Session = Depends(get_db),
|
|
) -> dict[str, Any]:
|
|
del payload
|
|
return service.sync_pending_tasks(db, limit=500)
|
|
|
|
|
|
@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/status/batch",
|
|
dependencies=[Depends(require_integration_key)],
|
|
)
|
|
def get_customer_binding_statuses(
|
|
req: PlatformFulfillmentCustomerSessionBatchRequest,
|
|
db: Session = Depends(get_db),
|
|
) -> dict[str, Any]:
|
|
return service.customer_refresh_bindings(db, task_ids=req.task_ids)
|
|
|
|
|
|
@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)
|