Skip to content

Commit

Permalink
refactor: Lint agents-api (CI)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedantsahai18 authored and github-actions[bot] committed Dec 19, 2024
1 parent 116edf8 commit 47c3fc9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
7 changes: 1 addition & 6 deletions agents-api/agents_api/queries/files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@
from .get_file import get_file
from .list_files import list_files

__all__ = [
"create_file",
"delete_file",
"get_file",
"list_files"
]
__all__ = ["create_file", "delete_file", "get_file", "list_files"]
13 changes: 7 additions & 6 deletions agents-api/agents_api/queries/files/create_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
It includes functions to construct and execute SQL queries for inserting new file records.
"""

import base64
import hashlib
from typing import Any, Literal
from uuid import UUID

import asyncpg
from beartype import beartype
from fastapi import HTTPException
from sqlglot import parse_one
from uuid_extensions import uuid7
import asyncpg
from fastapi import HTTPException
import base64
import hashlib

from ...autogen.openapi_model import CreateFileRequest, File
from ...metrics.counters import increase_counter
from ..utils import pg_query, rewrap_exceptions, wrap_in_class, partialclass
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

# Create file
file_query = parse_one("""
Expand Down Expand Up @@ -63,6 +63,7 @@
ON CONFLICT (developer_id, agent_id, file_id) DO NOTHING; -- Uses primary key index
""").sql(pretty=True)


# Add error handling decorator
# @rewrap_exceptions(
# {
Expand Down Expand Up @@ -147,4 +148,4 @@ async def create_file(
else: # agent
queries.append((agent_file_query, assoc_params))

return queries
return queries
22 changes: 13 additions & 9 deletions agents-api/agents_api/queries/files/delete_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from typing import Literal
from uuid import UUID

from beartype import beartype
from sqlglot import parse_one
import asyncpg
from beartype import beartype
from fastapi import HTTPException
from sqlglot import parse_one

from ...autogen.openapi_model import ResourceDeletedResponse
from ...common.utils.datetime import utcnow
from ...metrics.counters import increase_counter
from ..utils import pg_query, rewrap_exceptions, wrap_in_class, partialclass
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

# Simple query to delete file (when no associations exist)
delete_file_query = parse_one("""
Expand Down Expand Up @@ -67,7 +67,7 @@
ResourceDeletedResponse,
one=True,
transform=lambda d: {
"id": d["file_id"],
"id": d["file_id"],
"deleted_at": utcnow(),
"jobs": [],
},
Expand All @@ -76,15 +76,15 @@
@pg_query
@beartype
async def delete_file(
*,
file_id: UUID,
*,
file_id: UUID,
developer_id: UUID,
owner_id: UUID | None = None,
owner_type: Literal["user", "agent"] | None = None,
) -> list[tuple[str, list] | tuple[str, list, str]]:
"""
Deletes a file and/or its association using simple, efficient queries.
If owner details provided:
1. Deletes the owner's association
2. Checks for remaining associations
Expand All @@ -106,9 +106,13 @@ async def delete_file(
if owner_id and owner_type:
# Delete specific association
assoc_params = [developer_id, file_id, owner_id]
assoc_query = delete_user_assoc_query if owner_type == "user" else delete_agent_assoc_query
assoc_query = (
delete_user_assoc_query
if owner_type == "user"
else delete_agent_assoc_query
)
queries.append((assoc_query, assoc_params))

# If no associations, delete file
queries.append((delete_file_query, [developer_id, file_id]))
else:
Expand Down
9 changes: 5 additions & 4 deletions agents-api/agents_api/queries/files/get_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

from uuid import UUID

import asyncpg
from beartype import beartype
from sqlglot import parse_one
from fastapi import HTTPException
import asyncpg
from sqlglot import parse_one

from ...autogen.openapi_model import File
from ..utils import pg_query, rewrap_exceptions, wrap_in_class, partialclass
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

# Define the raw SQL query
file_query = parse_one("""
Expand All @@ -31,6 +31,7 @@
LIMIT 1; -- Early termination once found
""").sql(pretty=True)


@rewrap_exceptions(
{
asyncpg.NoDataFoundError: partialclass(
Expand Down Expand Up @@ -66,4 +67,4 @@ async def get_file(*, file_id: UUID, developer_id: UUID) -> tuple[str, list]:
return (
file_query,
[developer_id, file_id], # Order matches index columns
)
)
10 changes: 6 additions & 4 deletions agents-api/agents_api/queries/files/list_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

from typing import Any, Literal
from uuid import UUID

import asyncpg
from beartype import beartype
from fastapi import HTTPException
from sqlglot import parse_one

from ...autogen.openapi_model import File
from ..utils import pg_query, rewrap_exceptions, wrap_in_class, partialclass
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

# Query to list all files for a developer (uses developer_id index)
developer_files_query = parse_one("""
Expand Down Expand Up @@ -92,8 +93,9 @@
OFFSET $3;
""").sql(pretty=True)


@wrap_in_class(
File,
File,
one=True,
transform=lambda d: {
**d,
Expand Down Expand Up @@ -135,10 +137,10 @@ async def list_files(
# Validate parameters
if direction.lower() not in ["asc", "desc"]:
raise HTTPException(status_code=400, detail="Invalid sort direction")

if limit > 100 or limit < 1:
raise HTTPException(status_code=400, detail="Limit must be between 1 and 100")

if offset < 0:
raise HTTPException(status_code=400, detail="Offset must be non-negative")

Expand Down
7 changes: 4 additions & 3 deletions agents-api/tests/test_files_queries.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# # Tests for entry queries


from fastapi import HTTPException
from uuid_extensions import uuid7
from ward import raises, test
from fastapi import HTTPException

from agents_api.autogen.openapi_model import CreateFileRequest
from agents_api.clients.pg import create_db_pool
from agents_api.queries.files.create_file import create_file
from agents_api.queries.files.delete_file import delete_file
from agents_api.queries.files.get_file import get_file
from tests.fixtures import pg_dsn, test_agent, test_developer_id
from agents_api.clients.pg import create_db_pool


@test("query: create file")
Expand Down Expand Up @@ -77,4 +78,4 @@ async def _(dsn=pg_dsn, developer_id=test_developer_id):
# )

# assert e.value.status_code == 404
# assert e.value.detail == "The specified file does not exist"
# assert e.value.detail == "The specified file does not exist"

0 comments on commit 47c3fc9

Please sign in to comment.