Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [
"arq>=0.25.0",
"bcrypt>=4.1.1",
"psycopg2-binary>=2.9.9",
"fastcrud>=0.19.0",
"fastcrud>=0.19.1",
"crudadmin>=0.4.2",
"gunicorn>=23.0.0",
"ruff>=0.11.13",
Expand Down
12 changes: 6 additions & 6 deletions src/app/api/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated, Any, cast
from typing import Annotated, Any

from fastapi import Depends, HTTPException, Request
from sqlalchemy.ext.asyncio import AsyncSession
Expand Down Expand Up @@ -83,14 +83,14 @@ async def rate_limiter_dependency(
user_id = user["id"]
tier = await crud_tiers.get(db, id=user["tier_id"], schema_to_select=TierRead)
if tier:
tier = cast(TierRead, tier)
rate_limit = await crud_rate_limits.get(db=db, tier_id=tier.id, path=path, schema_to_select=RateLimitRead)
rate_limit = await crud_rate_limits.get(
db=db, tier_id=tier["id"], path=path, schema_to_select=RateLimitRead
)
if rate_limit:
rate_limit = cast(RateLimitRead, rate_limit)
limit, period = rate_limit.limit, rate_limit.period
limit, period = rate_limit["limit"], rate_limit["period"]
else:
logger.warning(
f"User {user_id} with tier '{tier.name}' has no specific rate limit for path '{path}'. \
f"User {user_id} with tier '{tier['name']}' has no specific rate limit for path '{path}'. \
Applying default rate limit."
)
limit, period = DEFAULT_LIMIT, DEFAULT_PERIOD
Expand Down
23 changes: 3 additions & 20 deletions src/app/api/v1/posts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated, Any, cast
from typing import Annotated, Any

from fastapi import APIRouter, Depends, Request
from fastcrud import PaginatedListResponse, compute_offset, paginated_response
Expand Down Expand Up @@ -28,8 +28,6 @@ async def write_post(
if db_user is None:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)

if current_user["id"] != db_user["id"]:
raise ForbiddenException()

Expand All @@ -46,7 +44,7 @@ async def write_post(
if post_read is None:
raise NotFoundException("Created post not found")

return cast(dict[str, Any], post_read)
return post_read


@router.get("/{username}/posts", response_model=PaginatedListResponse[PostRead])
Expand All @@ -66,7 +64,6 @@ async def read_posts(
if not db_user:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)
posts_data = await crud_posts.get_multi(
db=db,
offset=compute_offset(page, items_per_page),
Expand All @@ -88,15 +85,13 @@ async def read_post(
if db_user is None:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)

db_post = await crud_posts.get(
db=db, id=id, created_by_user_id=db_user["id"], is_deleted=False, schema_to_select=PostRead
)
if db_post is None:
raise NotFoundException("Post not found")

return cast(dict[str, Any], db_post)
return db_post


@router.patch("/{username}/post/{id}")
Expand All @@ -113,17 +108,13 @@ async def patch_post(
if db_user is None:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)

if current_user["id"] != db_user["id"]:
raise ForbiddenException()

db_post = await crud_posts.get(db=db, id=id, is_deleted=False, schema_to_select=PostRead)
if db_post is None:
raise NotFoundException("Post not found")

db_post = cast(dict[str, Any], db_post)

await crud_posts.update(db=db, object=values, id=id)
return {"message": "Post updated"}

Expand All @@ -141,17 +132,13 @@ async def erase_post(
if db_user is None:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)

if current_user["id"] != db_user["id"]:
raise ForbiddenException()

db_post = await crud_posts.get(db=db, id=id, is_deleted=False, schema_to_select=PostRead)
if db_post is None:
raise NotFoundException("Post not found")

db_post = cast(dict[str, Any], db_post)

await crud_posts.delete(db=db, id=id)

return {"message": "Post deleted"}
Expand All @@ -166,13 +153,9 @@ async def erase_db_post(
if db_user is None:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)

db_post = await crud_posts.get(db=db, id=id, is_deleted=False, schema_to_select=PostRead)
if db_post is None:
raise NotFoundException("Post not found")

db_post = cast(dict[str, Any], db_post)

await crud_posts.db_delete(db=db, id=id)
return {"message": "Post deleted from the database"}
17 changes: 3 additions & 14 deletions src/app/api/v1/rate_limits.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated, Any, cast
from typing import Annotated, Any

from fastapi import APIRouter, Depends, Request
from fastcrud import PaginatedListResponse, compute_offset, paginated_response
Expand All @@ -23,9 +23,6 @@ async def write_rate_limit(
if not db_tier:
raise NotFoundException("Tier not found")

db_tier = cast(dict[str, Any], db_tier)

db_tier = cast(dict[str, Any], db_tier)
rate_limit_internal_dict = rate_limit.model_dump()
rate_limit_internal_dict["tier_id"] = db_tier["id"]

Expand All @@ -43,7 +40,7 @@ async def write_rate_limit(
if rate_limit_read is None:
raise NotFoundException("Created rate limit not found")

return cast(dict[str, Any], rate_limit_read)
return rate_limit_read


@router.get("/tier/{tier_name}/rate_limits", response_model=PaginatedListResponse[RateLimitRead])
Expand All @@ -58,8 +55,6 @@ async def read_rate_limits(
if not db_tier:
raise NotFoundException("Tier not found")

db_tier = cast(dict[str, Any], db_tier)

rate_limits_data = await crud_rate_limits.get_multi(
db=db,
offset=compute_offset(page, items_per_page),
Expand All @@ -79,13 +74,11 @@ async def read_rate_limit(
if not db_tier:
raise NotFoundException("Tier not found")

db_tier = cast(dict[str, Any], db_tier)

db_rate_limit = await crud_rate_limits.get(db=db, tier_id=db_tier["id"], id=id, schema_to_select=RateLimitRead)
if db_rate_limit is None:
raise NotFoundException("Rate Limit not found")

return cast(dict[str, Any], db_rate_limit)
return db_rate_limit


@router.patch("/tier/{tier_name}/rate_limit/{id}", dependencies=[Depends(get_current_superuser)])
Expand All @@ -100,8 +93,6 @@ async def patch_rate_limit(
if not db_tier:
raise NotFoundException("Tier not found")

db_tier = cast(dict[str, Any], db_tier)

db_rate_limit = await crud_rate_limits.get(db=db, tier_id=db_tier["id"], id=id, schema_to_select=RateLimitRead)
if db_rate_limit is None:
raise NotFoundException("Rate Limit not found")
Expand All @@ -118,8 +109,6 @@ async def erase_rate_limit(
if not db_tier:
raise NotFoundException("Tier not found")

db_tier = cast(dict[str, Any], db_tier)

db_rate_limit = await crud_rate_limits.get(db=db, tier_id=db_tier["id"], id=id, schema_to_select=RateLimitRead)
if db_rate_limit is None:
raise NotFoundException("Rate Limit not found")
Expand Down
6 changes: 3 additions & 3 deletions src/app/api/v1/tiers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated, Any, cast
from typing import Annotated, Any

from fastapi import APIRouter, Depends, Request
from fastcrud import PaginatedListResponse, compute_offset, paginated_response
Expand Down Expand Up @@ -32,7 +32,7 @@ async def write_tier(
if tier_read is None:
raise NotFoundException("Created tier not found")

return cast(dict[str, Any], tier_read)
return tier_read


@router.get("/tiers", response_model=PaginatedListResponse[TierRead])
Expand All @@ -51,7 +51,7 @@ async def read_tier(request: Request, name: str, db: Annotated[AsyncSession, Dep
if db_tier is None:
raise NotFoundException("Tier not found")

return cast(dict[str, Any], db_tier)
return db_tier


@router.patch("/tier/{name}", dependencies=[Depends(get_current_superuser)])
Expand Down
11 changes: 3 additions & 8 deletions src/app/api/v1/users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated, Any, cast
from typing import Annotated, Any

from fastapi import APIRouter, Depends, Request
from fastcrud import PaginatedListResponse, compute_offset, paginated_response
Expand Down Expand Up @@ -43,7 +43,7 @@ async def write_user(
if user_read is None:
raise NotFoundException("Created user not found")

return cast(dict[str, Any], user_read)
return user_read


@router.get("/users", response_model=PaginatedListResponse[UserRead])
Expand Down Expand Up @@ -74,7 +74,7 @@ async def read_user(
if db_user is None:
raise NotFoundException("User not found")

return cast(dict[str, Any], db_user)
return db_user


@router.patch("/user/{username}")
Expand Down Expand Up @@ -151,7 +151,6 @@ async def read_user_rate_limits(
if db_user is None:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)
user_dict = dict(db_user)
if db_user["tier_id"] is None:
user_dict["tier_rate_limits"] = []
Expand All @@ -161,7 +160,6 @@ async def read_user_rate_limits(
if db_tier is None:
raise NotFoundException("Tier not found")

db_tier = cast(dict[str, Any], db_tier)
db_rate_limits = await crud_rate_limits.get_multi(db=db, tier_id=db_tier["id"])

user_dict["tier_rate_limits"] = db_rate_limits["data"]
Expand All @@ -177,15 +175,13 @@ async def read_user_tier(
if db_user is None:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)
if db_user["tier_id"] is None:
return None

db_tier = await crud_tiers.get(db=db, id=db_user["tier_id"], schema_to_select=TierRead)
if not db_tier:
raise NotFoundException("Tier not found")

db_tier = cast(dict[str, Any], db_tier)
user_dict = dict(db_user)
tier_dict = dict(db_tier)

Expand All @@ -203,7 +199,6 @@ async def patch_user_tier(
if db_user is None:
raise NotFoundException("User not found")

db_user = cast(dict[str, Any], db_user)
db_tier = await crud_tiers.get(db=db, id=values.tier_id, schema_to_select=TierRead)
if db_tier is None:
raise NotFoundException("Tier not found")
Expand Down
3 changes: 1 addition & 2 deletions src/app/core/security.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import UTC, datetime, timedelta
from enum import Enum
from typing import Any, Literal, cast
from typing import Any, Literal

import bcrypt
from fastapi.security import OAuth2PasswordBearer
Expand Down Expand Up @@ -45,7 +45,6 @@ async def authenticate_user(username_or_email: str, password: str, db: AsyncSess
if not db_user:
return False

db_user = cast(dict[str, Any], db_user)
if not await verify_password(password, db_user["hashed_password"]):
return False

Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.