-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50a9fc7
commit 1dcc0f8
Showing
12 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
lumigator/backend/backend/alembic/versions/54e483b63d8a_add_keys_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""Add keys table | ||
Revision ID: 54e483b63d8a | ||
Revises: 9b5d04b45a85 | ||
Create Date: 2025-02-20 17:03:30.394254 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '54e483b63d8a' # pragma: allowlist secret | ||
down_revision: Union[str, None] = '9b5d04b45a85' # pragma: allowlist secret | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table( | ||
"keys", | ||
sa.Column("id", sa.Uuid(), nullable=False), | ||
sa.Column("key_name", sa.String(), nullable=False, unique=True), | ||
sa.Column("key_value", sa.String(), nullable=False), | ||
sa.Column("description", sa.String(), nullable=False), | ||
sa.Column( | ||
"created_at", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("(CURRENT_TIMESTAMP)"), | ||
nullable=False, | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_table("keys") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from http import HTTPStatus | ||
|
||
from fastapi import APIRouter, status | ||
from lumigator_schemas.keys import Key | ||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
|
||
from loguru import logger | ||
|
||
from backend.api.deps import KeyServiceDep | ||
from backend.api.http_headers import HttpHeaders | ||
from backend.services.exceptions.base_exceptions import ServiceError | ||
from backend.settings import settings | ||
|
||
router = APIRouter() | ||
|
||
def key_exception_mappings() -> dict[type[ServiceError], HTTPStatus]: | ||
return { | ||
} | ||
|
||
|
||
|
||
@router.put( | ||
"/{key_name}", | ||
status_code=status.HTTP_200_OK, | ||
responses={ | ||
status.HTTP_201_CREATED: {"description": "Dataset successfully uploaded"}, | ||
}, | ||
) | ||
def upload_key( | ||
service: KeyServiceDep, | ||
key: Key, | ||
key_name: str, | ||
request: Request, | ||
response: Response, | ||
) -> None: | ||
"""Uploads a key for use in Lumigator. | ||
Lumigator uses different keys for purposes such as external API calls. | ||
The user can upload new values for these keys, but they cannot retrieve | ||
them. | ||
""" | ||
service.upload_key(key, key_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
from backend.records.base import BaseRecord | ||
from backend.records.mixins import CreatedAtMixin | ||
|
||
|
||
class KeyRecord(BaseRecord, CreatedAtMixin): | ||
__tablename__ = "keys" | ||
key_name: Mapped[str] = mapped_column(unique=True) | ||
key_value: Mapped[str] | ||
description: Mapped[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from uuid import UUID | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from backend.records.keys import KeyRecord | ||
from backend.repositories.base import BaseRepository | ||
|
||
|
||
class KeyRepository(BaseRepository[KeyRecord]): | ||
def __init__(self, session: Session): | ||
super().__init__(KeyRecord, session) | ||
|
||
def get_by_key_name(self, key_name: str) -> KeyRecord | None: | ||
return self.session.query(KeyRecord).where(KeyRecord.key_name == key_name).all().first() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import csv | ||
from pathlib import Path | ||
from uuid import UUID | ||
|
||
from datasets import load_dataset | ||
from fastapi import UploadFile | ||
from loguru import logger | ||
from mypy_boto3_s3.client import S3Client | ||
from pydantic import ByteSize | ||
from s3fs import S3FileSystem | ||
|
||
from backend.repositories.keys import KeyRepository | ||
from backend.settings import settings | ||
from lumigator_schemas.keys import Key | ||
|
||
class KeyService: | ||
def __init__( | ||
self, key_repo: KeyRepository | ||
): | ||
self._key_repo = key_repo | ||
|
||
def upload_key( | ||
self, | ||
key: Key, | ||
key_name: str | ||
) -> None: | ||
"""Uploads a key under a certain name | ||
""" | ||
self._key_repo.create(**key.model_dump(), key_name=key_name) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
lumigator/backend/backend/tests/unit/api/routes/test_key.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from fastapi import status | ||
from fastapi.testclient import TestClient | ||
from lumigator_schemas.keys import Key | ||
|
||
from backend.repositories.keys import KeyRepository | ||
from backend.services.keys import KeyService | ||
|
||
def test_put_key(app_client: TestClient, key_service: KeyService, key_repository: KeyRepository, dependency_overrides_fakes): | ||
new_key = Key(key_value="123456", description="test key") | ||
new_key_name = 'TEST_AI_CLIENT_KEY' | ||
assert key_repository.list() == [] | ||
response = app_client.put(f'/keys/{new_key_name}', json=new_key.model_dump()) | ||
assert response.status_code == status.HTTP_200_OK | ||
assert key_repository.list() != [] | ||
db_key = key_repository.list()[0] | ||
assert db_key.key_name == new_key_name | ||
assert db_key.key_value == new_key.key_value | ||
assert db_key.description == new_key.description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from pydantic import BaseModel | ||
|
||
class Key(BaseModel): | ||
key_value: str | ||
description: str = "" |