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 15, 2024
1 parent f4e6b48 commit 55500d9
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 83 deletions.
6 changes: 3 additions & 3 deletions agents-api/agents_api/queries/users/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
The `user` module within the `queries` package provides SQL query functions for managing users
The `user` module within the `queries` package provides SQL query functions for managing users
in the TimescaleDB database. This includes operations for:
- Creating new users
Expand All @@ -9,8 +9,8 @@
- Deleting users
"""

from .create_user import create_user
from .create_or_update_user import create_or_update_user_query
from .create_user import create_user
from .delete_user import delete_user_query
from .get_user import get_user_query
from .list_users import list_users_query
Expand All @@ -25,4 +25,4 @@
"list_users_query",
"patch_user_query",
"update_user_query",
]
]
23 changes: 11 additions & 12 deletions agents-api/agents_api/queries/users/create_or_update_user.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
from typing import Any
from uuid import UUID

from asyncpg import exceptions as asyncpg_exceptions
from beartype import beartype
from fastapi import HTTPException
from asyncpg import exceptions as asyncpg_exceptions
from sqlglot import parse_one

from ...autogen.openapi_model import CreateUserRequest, User
from ...metrics.counters import increase_counter
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class


@rewrap_exceptions({
asyncpg_exceptions.ForeignKeyViolationError: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
})
@rewrap_exceptions(
{
asyncpg_exceptions.ForeignKeyViolationError: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
}
)
@wrap_in_class(User)
@increase_counter("create_or_update_user")
@pg_query
@beartype
def create_or_update_user_query(
*,
developer_id: UUID,
user_id: UUID,
data: CreateUserRequest
*, developer_id: UUID, user_id: UUID, data: CreateUserRequest
) -> tuple[str, dict]:
"""
Constructs an SQL query to create or update a user.
Expand Down
31 changes: 17 additions & 14 deletions agents-api/agents_api/queries/users/create_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@
from beartype import beartype
from fastapi import HTTPException
from psycopg import errors as psycopg_errors
from sqlglot import parse_one
from pydantic import ValidationError
from sqlglot import parse_one
from uuid_extensions import uuid7

from ...autogen.openapi_model import CreateUserRequest, User
from ...metrics.counters import increase_counter
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

@rewrap_exceptions({
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
),
ValidationError: partialclass(
HTTPException,
status_code=400,
detail="Input validation failed. Please check the provided data.",
),
})

@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
),
ValidationError: partialclass(
HTTPException,
status_code=400,
detail="Input validation failed. Please check the provided data.",
),
}
)
@wrap_in_class(User)
@increase_counter("create_user")
@pg_query
Expand All @@ -46,7 +49,7 @@ def create_user(
tuple[str, dict]: A tuple containing the SQL query and its parameters.
"""
user_id = user_id or uuid7()

query = parse_one("""
INSERT INTO users (
developer_id,
Expand Down
19 changes: 11 additions & 8 deletions agents-api/agents_api/queries/users/delete_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
from ...metrics.counters import increase_counter
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

@rewrap_exceptions({
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
})

@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
}
)
@wrap_in_class(ResourceDeletedResponse, one=True)
@increase_counter("delete_user")
@pg_query
Expand All @@ -42,4 +45,4 @@ def delete_user_query(*, developer_id: UUID, user_id: UUID) -> tuple[list[str],
COMMIT;
""").sql()

return [query], {"developer_id": developer_id, "user_id": user_id}
return [query], {"developer_id": developer_id, "user_id": user_id}
19 changes: 11 additions & 8 deletions agents-api/agents_api/queries/users/get_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
from ...metrics.counters import increase_counter
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

@rewrap_exceptions({
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
})

@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
}
)
@wrap_in_class(User, one=True)
@increase_counter("get_user")
@pg_query
Expand Down Expand Up @@ -47,4 +50,4 @@ def get_user_query(*, developer_id: UUID, user_id: UUID) -> tuple[str, dict]:
AND user_id = %(user_id)s;
""").sql()

return query, {"developer_id": developer_id, "user_id": user_id}
return query, {"developer_id": developer_id, "user_id": user_id}
25 changes: 12 additions & 13 deletions agents-api/agents_api/queries/users/list_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
from ...metrics.counters import increase_counter
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

@rewrap_exceptions({
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
})

@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
}
)
@wrap_in_class(User)
@increase_counter("list_users")
@pg_query
Expand Down Expand Up @@ -51,11 +54,7 @@ def list_users_query(
raise HTTPException(status_code=400, detail="Offset must be non-negative")

metadata_clause = ""
params = {
"developer_id": developer_id,
"limit": limit,
"offset": offset
}
params = {"developer_id": developer_id, "limit": limit, "offset": offset}

if metadata_filter:
metadata_clause = "AND metadata @> %(metadata_filter)s"
Expand All @@ -78,4 +77,4 @@ def list_users_query(
OFFSET %(offset)s;
""").sql()

return query, params
return query, params
24 changes: 12 additions & 12 deletions agents-api/agents_api/queries/users/patch_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
from ...metrics.counters import increase_counter
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

@rewrap_exceptions({
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
})

@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
}
)
@wrap_in_class(ResourceUpdatedResponse, one=True)
@increase_counter("patch_user")
@pg_query
@beartype
def patch_user_query(
*,
developer_id: UUID,
user_id: UUID,
data: PatchUserRequest
*, developer_id: UUID, user_id: UUID, data: PatchUserRequest
) -> tuple[str, dict]:
"""
Constructs an optimized SQL query for partial user updates.
Expand Down Expand Up @@ -70,4 +70,4 @@ def patch_user_query(
updated_at;
""").sql()

return query, params
return query, params
26 changes: 13 additions & 13 deletions agents-api/agents_api/queries/users/update_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
from psycopg import errors as psycopg_errors
from sqlglot import parse_one

from ...autogen.openapi_model import UpdateUserRequest, ResourceUpdatedResponse
from ...autogen.openapi_model import ResourceUpdatedResponse, UpdateUserRequest
from ...metrics.counters import increase_counter
from ..utils import partialclass, pg_query, rewrap_exceptions, wrap_in_class

@rewrap_exceptions({
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
})

@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
)
}
)
@wrap_in_class(ResourceUpdatedResponse, one=True)
@increase_counter("update_user")
@pg_query
@beartype
def update_user_query(
*,
developer_id: UUID,
user_id: UUID,
data: UpdateUserRequest
*, developer_id: UUID, user_id: UUID, data: UpdateUserRequest
) -> tuple[str, dict]:
"""
Constructs an optimized SQL query to update a user's details.
Expand Down Expand Up @@ -65,4 +65,4 @@ def update_user_query(
"metadata": data.metadata or {},
}

return query, params
return query, params

0 comments on commit 55500d9

Please sign in to comment.