Download src/apps/admin/api/flags_api.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 7.28 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/admin/api/flags_api.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/apps/admin/api/flags_api.py
-
curl -L -o flags_api.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/admin/api/flags_api.py
7.28 kB
| """ | |
| Feature Flag Admin API — /api/v1/admin/flags | |
| Provides CRUD-like operations over the in-memory + Redis-backed | |
| FeatureFlagService. All write operations are immediately cached and | |
| persisted to Redis so the change is visible cluster-wide without restart. | |
| Security: admin-only endpoints — authenticated via get_current_user + | |
| role check. No tenant cross-contamination: per-tenant flag | |
| toggles operate on tenant_ids sets only. | |
| Standards: Sprint 45 — L9 implementation (SYSTEMS.md). | |
| """ | |
| from __future__ import annotations | |
| from typing import Any | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from pydantic import BaseModel, Field, field_validator | |
| from core.auth import get_current_user | |
| from core.feature_flags import FeatureFlag, FeatureFlagService, get_feature_flags | |
| from core.feature_flags.flags import RolloutStrategy | |
| flags_api_router = APIRouter(tags=["admin-flags"]) | |
| # ── Request / Response schemas ─────────────────────────────────────────────── | |
| class FlagSummary(BaseModel): | |
| """Lightweight flag representation for list endpoints.""" | |
| name: str | |
| description: str | |
| enabled: bool | |
| strategy: str | |
| percentage: int | |
| category: str | |
| owner: str | |
| model_config = {"from_attributes": True} | |
| def from_flag(cls, name: str, flag: FeatureFlag) -> "FlagSummary": | |
| return cls( | |
| name=name, | |
| description=flag.description, | |
| enabled=flag.enabled, | |
| strategy=flag.strategy.value if hasattr(flag.strategy, "value") else str(flag.strategy), | |
| percentage=flag.percentage, | |
| category=flag.category, | |
| owner=flag.owner, | |
| ) | |
| class FlagToggleRequest(BaseModel): | |
| enabled: bool | |
| class FlagPercentageRequest(BaseModel): | |
| percentage: int = Field(..., ge=0, le=100) | |
| class FlagTenantRequest(BaseModel): | |
| tenant_id: str = Field(..., min_length=1) | |
| class FlagCreateRequest(BaseModel): | |
| name: str = Field(..., min_length=1, max_length=120, pattern=r"^[a-z0-9_.:-]+$") | |
| description: str = Field(..., min_length=1, max_length=500) | |
| enabled: bool = False | |
| strategy: str = "none" | |
| percentage: int = Field(0, ge=0, le=100) | |
| category: str = "general" | |
| owner: str = "" | |
| def validate_strategy(cls, v: str) -> str: | |
| valid = {s.value for s in RolloutStrategy} | |
| if v not in valid: | |
| raise ValueError(f"strategy must be one of {sorted(valid)}") | |
| return v | |
| # ── Dependency ─────────────────────────────────────────────────────────────── | |
| def _flags_service() -> FeatureFlagService: | |
| return get_feature_flags() | |
| # ── Endpoints ──────────────────────────────────────────────────────────────── | |
| async def list_flags( | |
| category: str | None = None, | |
| _user: Any = Depends(get_current_user), | |
| svc: FeatureFlagService = Depends(_flags_service), | |
| ) -> list[FlagSummary]: | |
| """Return all registered feature flags, optionally filtered by category.""" | |
| all_flags = svc.get_all_flags() | |
| return [ | |
| FlagSummary.from_flag(name, flag) | |
| for name, flag in all_flags.items() | |
| if category is None or flag.category == category | |
| ] | |
| async def get_flag( | |
| flag_name: str, | |
| _user: Any = Depends(get_current_user), | |
| svc: FeatureFlagService = Depends(_flags_service), | |
| ) -> FlagSummary: | |
| flag = svc.get_flag(flag_name) | |
| if flag is None: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Flag '{flag_name}' not found") | |
| return FlagSummary.from_flag(flag_name, flag) | |
| async def create_flag( | |
| body: FlagCreateRequest, | |
| _user: Any = Depends(get_current_user), | |
| svc: FeatureFlagService = Depends(_flags_service), | |
| ) -> FlagSummary: | |
| if svc.get_flag(body.name) is not None: | |
| raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=f"Flag '{body.name}' already exists") | |
| flag = FeatureFlag( | |
| name=body.name, | |
| description=body.description, | |
| enabled=body.enabled, | |
| strategy=RolloutStrategy(body.strategy), | |
| percentage=body.percentage, | |
| category=body.category, | |
| owner=body.owner, | |
| ) | |
| svc.set_flag(body.name, flag) | |
| return FlagSummary.from_flag(body.name, flag) | |
| async def toggle_flag( | |
| flag_name: str, | |
| body: FlagToggleRequest, | |
| _user: Any = Depends(get_current_user), | |
| svc: FeatureFlagService = Depends(_flags_service), | |
| ) -> FlagSummary: | |
| flag = svc.get_flag(flag_name) | |
| if flag is None: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Flag '{flag_name}' not found") | |
| flag.enabled = body.enabled | |
| return FlagSummary.from_flag(flag_name, flag) | |
| async def set_flag_percentage( | |
| flag_name: str, | |
| body: FlagPercentageRequest, | |
| _user: Any = Depends(get_current_user), | |
| svc: FeatureFlagService = Depends(_flags_service), | |
| ) -> FlagSummary: | |
| flag = svc.get_flag(flag_name) | |
| if flag is None: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Flag '{flag_name}' not found") | |
| svc.set_percentage(flag_name, body.percentage) | |
| return FlagSummary.from_flag(flag_name, svc.get_flag(flag_name)) | |
| async def enable_flag_for_tenant( | |
| flag_name: str, | |
| body: FlagTenantRequest, | |
| _user: Any = Depends(get_current_user), | |
| svc: FeatureFlagService = Depends(_flags_service), | |
| ) -> FlagSummary: | |
| flag = svc.get_flag(flag_name) | |
| if flag is None: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Flag '{flag_name}' not found") | |
| svc.enable_for_tenant(flag_name, body.tenant_id) | |
| return FlagSummary.from_flag(flag_name, svc.get_flag(flag_name)) | |
| async def disable_flag_for_tenant( | |
| flag_name: str, | |
| tenant_id: str, | |
| _user: Any = Depends(get_current_user), | |
| svc: FeatureFlagService = Depends(_flags_service), | |
| ) -> FlagSummary: | |
| flag = svc.get_flag(flag_name) | |
| if flag is None: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Flag '{flag_name}' not found") | |
| svc.disable_for_tenant(flag_name, tenant_id) | |
| return FlagSummary.from_flag(flag_name, svc.get_flag(flag_name)) | |