|
| 1 | +from typing import Annotated |
| 2 | + |
| 3 | +from fastapi import Request, Depends, HTTPException |
| 4 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 5 | +import fastapi |
| 6 | + |
| 7 | +from app.schemas.rate_limit import ( |
| 8 | + RateLimitRead, |
| 9 | + RateLimitCreate, |
| 10 | + RateLimitCreateInternal, |
| 11 | + RateLimitUpdate |
| 12 | +) |
| 13 | +from app.api.dependencies import get_current_superuser |
| 14 | +from app.core.database import async_get_db |
| 15 | +from app.crud.crud_rate_limit import crud_rate_limits |
| 16 | +from app.crud.crud_tier import crud_tiers |
| 17 | +from app.api.paginated import PaginatedListResponse, paginated_response, compute_offset |
| 18 | + |
| 19 | +router = fastapi.APIRouter(tags=["rate_limits"]) |
| 20 | + |
| 21 | +@router.post("/tier/{tier_id}/rate_limit", dependencies=[Depends(get_current_superuser)], status_code=201) |
| 22 | +async def write_rate_limit( |
| 23 | + request: Request, |
| 24 | + tier_id: int, |
| 25 | + rate_limit: RateLimitCreate, |
| 26 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 27 | +): |
| 28 | + db_tier = await crud_tiers.exists(db=db, id=tier_id) |
| 29 | + if not db_tier: |
| 30 | + raise HTTPException(status_code=404, detail="Tier not found") |
| 31 | + |
| 32 | + rate_limit_internal_dict = rate_limit.model_dump() |
| 33 | + rate_limit_internal_dict["tier_id"] = tier_id |
| 34 | + |
| 35 | + db_rate_limit = await crud_rate_limits.exists(db=db, name=rate_limit_internal_dict["name"]) |
| 36 | + if db_rate_limit: |
| 37 | + raise HTTPException(status_code=400, detail="Rate Limit Name not available") |
| 38 | + |
| 39 | + rate_limit_internal = RateLimitCreateInternal(**rate_limit_internal_dict) |
| 40 | + return await crud_rate_limits.create(db=db, object=rate_limit_internal) |
| 41 | + |
| 42 | + |
| 43 | +@router.get("/tier/{tier_id}/rate_limits", response_model=PaginatedListResponse[RateLimitRead]) |
| 44 | +async def read_rate_limits( |
| 45 | + request: Request, |
| 46 | + tier_id: int, |
| 47 | + db: Annotated[AsyncSession, Depends(async_get_db)], |
| 48 | + page: int = 1, |
| 49 | + items_per_page: int = 10 |
| 50 | +): |
| 51 | + db_tier = await crud_tiers.exists(db=db, id=tier_id) |
| 52 | + if not db_tier: |
| 53 | + raise HTTPException(status_code=404, detail="Tier not found") |
| 54 | + |
| 55 | + rate_limits_data = await crud_rate_limits.get_multi( |
| 56 | + db=db, |
| 57 | + offset=compute_offset(page, items_per_page), |
| 58 | + limit=items_per_page, |
| 59 | + schema_to_select=RateLimitRead, |
| 60 | + tier_id=tier_id |
| 61 | + ) |
| 62 | + |
| 63 | + return paginated_response( |
| 64 | + crud_data=rate_limits_data, |
| 65 | + page=page, |
| 66 | + items_per_page=items_per_page |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@router.get("/tier/{tier_id}/rate_limit/{id}", response_model=RateLimitRead) |
| 71 | +async def read_rate_limit( |
| 72 | + request: Request, |
| 73 | + tier_id: int, |
| 74 | + id: int, |
| 75 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 76 | +): |
| 77 | + db_tier = await crud_tiers.exists(db=db, id=tier_id) |
| 78 | + if not db_tier: |
| 79 | + raise HTTPException(status_code=404, detail="Tier not found") |
| 80 | + |
| 81 | + db_rate_limit = await crud_rate_limits.get(db=db, schema_to_select=RateLimitRead, id=id) |
| 82 | + if db_rate_limit is None: |
| 83 | + raise HTTPException(status_code=404, detail="Rate Limit not found") |
| 84 | + |
| 85 | + return db_rate_limit |
| 86 | + |
| 87 | + |
| 88 | +@router.patch("/tier/{tier_id}/rate_limit/{id}", dependencies=[Depends(get_current_superuser)]) |
| 89 | +async def patch_rate_limit( |
| 90 | + request: Request, |
| 91 | + tier_id: int, |
| 92 | + id: int, |
| 93 | + values: RateLimitUpdate, |
| 94 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 95 | +): |
| 96 | + db_tier = await crud_tiers.exists(db=db, id=tier_id) |
| 97 | + if not db_tier: |
| 98 | + raise HTTPException(status_code=404, detail="Tier not found") |
| 99 | + |
| 100 | + db_rate_limit = await crud_rate_limits.get(db=db, schema_to_select=RateLimitRead, id=id) |
| 101 | + if db_rate_limit is None: |
| 102 | + raise HTTPException(status_code=404, detail="Rate Limit not found") |
| 103 | + |
| 104 | + await crud_rate_limits.update(db=db, object=values, id=id) |
| 105 | + return {"message": "Rate Limit updated"} |
| 106 | + |
| 107 | + |
| 108 | +@router.delete("/tier/{tier_id}/rate_limit/{id}", dependencies=[Depends(get_current_superuser)]) |
| 109 | +async def erase_rate_limit( |
| 110 | + request: Request, |
| 111 | + tier_id: int, |
| 112 | + id: int, |
| 113 | + db: Annotated[AsyncSession, Depends(async_get_db)] |
| 114 | +): |
| 115 | + db_tier = await crud_tiers.exists(db=db, id=tier_id) |
| 116 | + if not db_tier: |
| 117 | + raise HTTPException(status_code=404, detail="Tier not found") |
| 118 | + |
| 119 | + db_rate_limit = await crud_rate_limits.get(db=db, schema_to_select=RateLimitRead, id=id) |
| 120 | + if db_rate_limit is None: |
| 121 | + raise HTTPException(status_code=404, detail="Rate Limit not found") |
| 122 | + |
| 123 | + await crud_rate_limits.delete(db=db, db_row=db_rate_limit, id=id) |
| 124 | + return {"message": "Rate Limit deleted"} |
0 commit comments