Improve the frontend 2
This commit is contained in:
@@ -463,7 +463,7 @@ class AuthMiddleware(BaseHTTPMiddleware):
|
||||
|
||||
async def _get_tenant_subscription_tier(self, tenant_id: str, request: Request) -> Optional[str]:
|
||||
"""
|
||||
Get tenant subscription tier from tenant service
|
||||
Get tenant subscription tier using fast cached endpoint
|
||||
|
||||
Args:
|
||||
tenant_id: Tenant ID
|
||||
@@ -473,48 +473,27 @@ class AuthMiddleware(BaseHTTPMiddleware):
|
||||
Subscription tier string or None
|
||||
"""
|
||||
try:
|
||||
# Check cache first
|
||||
if self.redis_client:
|
||||
cache_key = f"tenant:tier:{tenant_id}"
|
||||
try:
|
||||
cached_tier = await self.redis_client.get(cache_key)
|
||||
if cached_tier:
|
||||
if isinstance(cached_tier, bytes):
|
||||
cached_tier = cached_tier.decode()
|
||||
logger.debug("Subscription tier from cache", tenant_id=tenant_id, tier=cached_tier)
|
||||
return cached_tier
|
||||
except Exception as e:
|
||||
logger.warning(f"Cache lookup failed for tenant tier: {e}")
|
||||
|
||||
# Get from tenant service
|
||||
async with httpx.AsyncClient(timeout=5.0) as client:
|
||||
# Use fast cached subscription tier endpoint (has its own Redis caching)
|
||||
async with httpx.AsyncClient(timeout=3.0) as client:
|
||||
headers = {"Authorization": request.headers.get("Authorization", "")}
|
||||
response = await client.get(
|
||||
f"{settings.TENANT_SERVICE_URL}/api/v1/tenants/{tenant_id}",
|
||||
f"{settings.TENANT_SERVICE_URL}/api/v1/subscriptions/{tenant_id}/tier",
|
||||
headers=headers
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
tenant_data = response.json()
|
||||
subscription_tier = tenant_data.get("subscription_tier", "basic")
|
||||
tier_data = response.json()
|
||||
subscription_tier = tier_data.get("tier", "starter")
|
||||
|
||||
# Cache for 5 minutes
|
||||
if self.redis_client:
|
||||
try:
|
||||
await self.redis_client.setex(
|
||||
f"tenant:tier:{tenant_id}",
|
||||
300, # 5 minutes
|
||||
subscription_tier
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to cache tenant tier: {e}")
|
||||
|
||||
logger.debug("Subscription tier from service", tenant_id=tenant_id, tier=subscription_tier)
|
||||
logger.debug("Subscription tier from cached endpoint",
|
||||
tenant_id=tenant_id,
|
||||
tier=subscription_tier,
|
||||
cached=tier_data.get("cached", False))
|
||||
return subscription_tier
|
||||
else:
|
||||
logger.warning(f"Failed to get tenant subscription tier: {response.status_code}")
|
||||
return "basic" # Default to basic
|
||||
return "starter" # Default to starter
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting tenant subscription tier: {e}")
|
||||
return "basic" # Default to basic on error
|
||||
return "starter" # Default to starter on error
|
||||
Reference in New Issue
Block a user