Skip to content

Commit 29acc81

Browse files
committed
refactor: api_key module uses integer ids
1 parent 2710a5d commit 29acc81

6 files changed

Lines changed: 18 additions & 25 deletions

File tree

src/modules/api_key/domain/entities.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from dataclasses import dataclass, field
22
from datetime import datetime
3-
from uuid import UUID, uuid4
43

54

65
@dataclass
76
class ApiKey:
8-
id: UUID = field(default_factory=uuid4)
7+
id: int | None = None
98
key_prefix: str = ""
109
key_hash: str = ""
1110
name: str = ""

src/modules/api_key/domain/repository.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from abc import ABC, abstractmethod
2-
from uuid import UUID
32

43
from src.modules.api_key.domain.entities import ApiKey
54

@@ -9,7 +8,7 @@ class ApiKeyRepository(ABC):
98
async def create(self, api_key: ApiKey) -> ApiKey: ...
109

1110
@abstractmethod
12-
async def get_by_id(self, id: UUID) -> ApiKey | None: ...
11+
async def get_by_id(self, id: int) -> ApiKey | None: ...
1312

1413
@abstractmethod
1514
async def get_by_key_hash(self, key_hash: str) -> ApiKey | None: ...
@@ -23,7 +22,7 @@ async def list(
2322
async def count(self) -> int: ...
2423

2524
@abstractmethod
26-
async def revoke(self, id: UUID) -> None: ...
25+
async def revoke(self, id: int) -> None: ...
2726

2827
@abstractmethod
29-
async def update_last_used(self, id: UUID) -> None: ...
28+
async def update_last_used(self, id: int) -> None: ...

src/modules/api_key/infrastructure/models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
class ApiKeyModel(Base, TenantMixin):
1111
__tablename__ = "api_keys"
1212

13-
id: Mapped[str] = mapped_column(String(36), primary_key=True)
1413
key_prefix: Mapped[str] = mapped_column(String(8), nullable=False)
1514
key_hash: Mapped[str] = mapped_column(String(128), nullable=False)
1615
name: Mapped[str] = mapped_column(String(255), nullable=False)

src/modules/api_key/infrastructure/repository.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
from datetime import datetime, timezone
3-
from uuid import UUID
43

54
from sqlalchemy import func, select
65
from sqlalchemy.ext.asyncio import AsyncSession
@@ -11,13 +10,12 @@
1110

1211

1312
class SQLAlchemyApiKeyRepository(ApiKeyRepository):
14-
def __init__(self, session: AsyncSession, tenant_id: UUID | None = None):
13+
def __init__(self, session: AsyncSession, tenant_id: int | None = None):
1514
self._session = session
1615
self._tenant_id = tenant_id
1716

1817
async def create(self, api_key: ApiKey) -> ApiKey:
1918
model = ApiKeyModel(
20-
id=str(api_key.id),
2119
key_prefix=api_key.key_prefix,
2220
key_hash=api_key.key_hash,
2321
name=api_key.name,
@@ -28,10 +26,12 @@ async def create(self, api_key: ApiKey) -> ApiKey:
2826
)
2927
self._session.add(model)
3028
await self._session.flush()
29+
await self._session.refresh(model)
30+
api_key.id = model.id
3131
return api_key
3232

33-
async def get_by_id(self, id: UUID) -> ApiKey | None:
34-
stmt = select(ApiKeyModel).where(ApiKeyModel.id == str(id))
33+
async def get_by_id(self, id: int) -> ApiKey | None:
34+
stmt = select(ApiKeyModel).where(ApiKeyModel.id == id)
3535
if self._tenant_id:
3636
stmt = stmt.where(ApiKeyModel.tenant_id == self._tenant_id)
3737
result = await self._session.execute(stmt)
@@ -60,20 +60,20 @@ async def count(self) -> int:
6060
result = await self._session.execute(stmt)
6161
return result.scalar() or 0
6262

63-
async def revoke(self, id: UUID) -> None:
64-
model = await self._session.get(ApiKeyModel, str(id))
63+
async def revoke(self, id: int) -> None:
64+
model = await self._session.get(ApiKeyModel, id)
6565
if model:
6666
model.is_active = False
6767

68-
async def update_last_used(self, id: UUID) -> None:
69-
model = await self._session.get(ApiKeyModel, str(id))
68+
async def update_last_used(self, id: int) -> None:
69+
model = await self._session.get(ApiKeyModel, id)
7070
if model:
7171
model.last_used_at = datetime.now(timezone.utc)
7272

7373
@staticmethod
7474
def _to_entity(model: ApiKeyModel) -> ApiKey:
7575
return ApiKey(
76-
id=UUID(model.id),
76+
id=model.id,
7777
key_prefix=model.key_prefix,
7878
key_hash=model.key_hash,
7979
name=model.name,

src/modules/api_key/presentation/dependencies.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from uuid import UUID
2-
31
from fastapi import Depends
42
from sqlalchemy.ext.asyncio import AsyncSession
53

@@ -14,7 +12,7 @@
1412

1513
async def get_api_key_repository(
1614
db: AsyncSession = Depends(get_db),
17-
tenant_id: UUID = Depends(get_current_tenant_id),
15+
tenant_id: int = Depends(get_current_tenant_id),
1816
) -> ApiKeyRepository:
1917
return SQLAlchemyApiKeyRepository(db, tenant_id)
2018

src/modules/api_key/presentation/routers.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from uuid import UUID
2-
31
from fastapi import APIRouter, Depends, HTTPException, status
42

53
from src.modules.api_key.application.service import ApiKeyService
@@ -33,7 +31,7 @@ async def create_api_key(
3331
expires_at=request.expires_at,
3432
)
3533
return ApiKeyCreatedResponse(
36-
id=str(api_key.id),
34+
id=api_key.id,
3735
name=api_key.name,
3836
key_prefix=api_key.key_prefix,
3937
key=raw_key,
@@ -53,7 +51,7 @@ async def list_api_keys(
5351
return ApiKeyListResponse(
5452
items=[
5553
ApiKeyResponse(
56-
id=str(k.id),
54+
id=k.id,
5755
key_prefix=k.key_prefix,
5856
name=k.name,
5957
permissions=k.permissions,
@@ -70,7 +68,7 @@ async def list_api_keys(
7068

7169
@router.delete("/{api_key_id}", status_code=status.HTTP_204_NO_CONTENT)
7270
async def revoke_api_key(
73-
api_key_id: UUID,
71+
api_key_id: int,
7472
repo: ApiKeyRepository = Depends(get_api_key_repository),
7573
):
7674
existing = await repo.get_by_id(api_key_id)

0 commit comments

Comments
 (0)