mirror of
https://github.com/langgenius/dify.git
synced 2026-05-03 07:46:31 +08:00
feat: add billing subscription plan api
This commit is contained in:
parent
bffd67f3a4
commit
1a2f37e7c7
@ -4,6 +4,7 @@ from collections.abc import Sequence
|
|||||||
from typing import Literal, TypedDict
|
from typing import Literal, TypedDict
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
from pydantic import TypeAdapter
|
||||||
from tenacity import retry, retry_if_exception_type, stop_before_delay, wait_fixed
|
from tenacity import retry, retry_if_exception_type, stop_before_delay, wait_fixed
|
||||||
from werkzeug.exceptions import InternalServerError
|
from werkzeug.exceptions import InternalServerError
|
||||||
|
|
||||||
@ -287,6 +288,7 @@ class BillingService:
|
|||||||
Mapping of tenant_id -> {plan: str, expiration_date: int}
|
Mapping of tenant_id -> {plan: str, expiration_date: int}
|
||||||
"""
|
"""
|
||||||
results: dict[str, SubscriptionPlan] = {}
|
results: dict[str, SubscriptionPlan] = {}
|
||||||
|
subscription_adapter = TypeAdapter(SubscriptionPlan)
|
||||||
|
|
||||||
chunk_size = 200
|
chunk_size = 200
|
||||||
for i in range(0, len(tenant_ids), chunk_size):
|
for i in range(0, len(tenant_ids), chunk_size):
|
||||||
@ -294,13 +296,10 @@ class BillingService:
|
|||||||
try:
|
try:
|
||||||
resp = cls._send_request("POST", "/subscription/plan/batch", json={"tenant_ids": chunk})
|
resp = cls._send_request("POST", "/subscription/plan/batch", json={"tenant_ids": chunk})
|
||||||
data = resp.get("data", {})
|
data = resp.get("data", {})
|
||||||
|
|
||||||
for tenant_id, plan in data.items():
|
for tenant_id, plan in data.items():
|
||||||
if isinstance(plan, dict) and "plan" in plan and "expiration_date" in plan:
|
subscription_plan = subscription_adapter.validate_python(plan)
|
||||||
subscription_plan: SubscriptionPlan = {
|
results[tenant_id] = subscription_plan
|
||||||
"plan": str(plan["plan"]),
|
|
||||||
"expiration_date": int(plan["expiration_date"]),
|
|
||||||
}
|
|
||||||
results[tenant_id] = subscription_plan
|
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to fetch billing info batch for tenants: %s", chunk)
|
logger.exception("Failed to fetch billing info batch for tenants: %s", chunk)
|
||||||
continue
|
continue
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user