refactor: replace bare dict with UtmInfo TypedDict in operation_service (#35055)

This commit is contained in:
wdeveloper16 2026-04-13 15:05:47 +02:00 committed by GitHub
parent 671c5cdd84
commit d412cddf39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View File

@ -20,7 +20,7 @@ from models.account import AccountStatus
from models.dataset import RateLimitLog
from models.model import DifySetup
from services.feature_service import FeatureService, LicenseStatus
from services.operation_service import OperationService
from services.operation_service import OperationService, UtmInfo
from .error import NotInitValidateError, NotSetupError, UnauthorizedAndForceLogout
@ -205,7 +205,7 @@ def cloud_utm_record[**P, R](view: Callable[P, R]) -> Callable[P, R]:
utm_info = request.cookies.get("utm_info")
if utm_info:
utm_info_dict: dict = json.loads(utm_info)
utm_info_dict: UtmInfo = json.loads(utm_info)
OperationService.record_utm(current_tenant_id, utm_info_dict)
return view(*args, **kwargs)

View File

@ -1,8 +1,22 @@
import os
from typing import TypedDict
import httpx
class UtmInfo(TypedDict, total=False):
"""Expected shape of the utm_info dict passed to record_utm.
All fields are optional; missing keys default to an empty string.
"""
utm_source: str
utm_medium: str
utm_campaign: str
utm_content: str
utm_term: str
class OperationService:
base_url = os.environ.get("BILLING_API_URL", "BILLING_API_URL")
secret_key = os.environ.get("BILLING_API_SECRET_KEY", "BILLING_API_SECRET_KEY")
@ -17,7 +31,7 @@ class OperationService:
return response.json()
@classmethod
def record_utm(cls, tenant_id: str, utm_info: dict):
def record_utm(cls, tenant_id: str, utm_info: UtmInfo):
params = {
"tenant_id": tenant_id,
"utm_source": utm_info.get("utm_source", ""),