Skip to content

Commit

Permalink
refactor: Lint agents-api (CI)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad-mtos authored and github-actions[bot] committed Dec 14, 2024
1 parent 418a504 commit b11247f
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 79 deletions.
6 changes: 4 additions & 2 deletions agents-api/agents_api/queries/agent/create_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from ...metrics.counters import increase_counter
from ..utils import (
generate_canonical_name,
pg_query,
partialclass,
pg_query,
rewrap_exceptions,
wrap_in_class,
)
Expand Down Expand Up @@ -91,7 +91,9 @@ def create_agent(
)

# Convert default_settings to dict if it exists
default_settings = data.default_settings.model_dump() if data.default_settings else None
default_settings = (
data.default_settings.model_dump() if data.default_settings else None
)

# Set default values
data.metadata = data.metadata or None
Expand Down
24 changes: 12 additions & 12 deletions agents-api/agents_api/queries/agent/create_or_update_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@
from typing import Any, TypeVar
from uuid import UUID

from ...autogen.openapi_model import Agent, CreateOrUpdateAgentRequest
from beartype import beartype
from fastapi import HTTPException
from psycopg import errors as psycopg_errors

from ...autogen.openapi_model import Agent, CreateOrUpdateAgentRequest
from ...metrics.counters import increase_counter
from ..utils import (
generate_canonical_name,
pg_query,
partialclass,
pg_query,
rewrap_exceptions,
wrap_in_class,
)

from beartype import beartype
from psycopg import errors as psycopg_errors

ModelT = TypeVar("ModelT", bound=Any)
T = TypeVar("T")


@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
HTTPException,
status_code=404,
detail="The specified developer does not exist."
detail="The specified developer does not exist.",
)
}
)
Expand All @@ -42,10 +43,7 @@
@increase_counter("create_or_update_agent")
@beartype
def create_or_update_agent_query(
*,
agent_id: UUID,
developer_id: UUID,
data: CreateOrUpdateAgentRequest
*, agent_id: UUID, developer_id: UUID, data: CreateOrUpdateAgentRequest
) -> tuple[list[str], dict]:
"""
Constructs the SQL queries to create a new agent or update an existing agent's details.
Expand All @@ -67,7 +65,9 @@ def create_or_update_agent_query(
)

# Convert default_settings to dict if it exists
default_settings = data.default_settings.model_dump() if data.default_settings else None
default_settings = (
data.default_settings.model_dump() if data.default_settings else None
)

# Set default values
data.metadata = data.metadata or None
Expand Down
18 changes: 10 additions & 8 deletions agents-api/agents_api/queries/agent/delete_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@
from typing import Any, TypeVar
from uuid import UUID

from beartype import beartype
from fastapi import HTTPException
from psycopg import errors as psycopg_errors

from ...autogen.openapi_model import ResourceDeletedResponse
from ...common.utils.datetime import utcnow
from ...metrics.counters import increase_counter
from ..utils import (
pg_query,
partialclass,
pg_query,
rewrap_exceptions,
wrap_in_class,
)
from beartype import beartype
from psycopg import errors as psycopg_errors
from ...autogen.openapi_model import ResourceDeletedResponse
from ...common.utils.datetime import utcnow

ModelT = TypeVar("ModelT", bound=Any)
T = TypeVar("T")


@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
HTTPException,
status_code=404,
detail="The specified developer does not exist."
detail="The specified developer does not exist.",
)
}
# TODO: Add more exceptions
Expand Down Expand Up @@ -83,7 +85,7 @@ def delete_agent_query(*, agent_id: UUID, developer_id: UUID) -> tuple[list[str]
-- Delete the agent
DELETE FROM agents
WHERE agent_id = %(agent_id)s AND developer_id = %(developer_id)s;
"""
""",
]

params = {
Expand Down
15 changes: 8 additions & 7 deletions agents-api/agents_api/queries/agent/get_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@
from typing import Any, TypeVar
from uuid import UUID

from beartype import beartype
from fastapi import HTTPException
from psycopg import errors as psycopg_errors

from ...autogen.openapi_model import Agent
from ...metrics.counters import increase_counter
from ..utils import (
pg_query,
partialclass,
pg_query,
rewrap_exceptions,
wrap_in_class,
)
from beartype import beartype
from psycopg import errors as psycopg_errors

from ...autogen.openapi_model import Agent

ModelT = TypeVar("ModelT", bound=Any)
T = TypeVar("T")


@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
HTTPException,
status_code=404,
detail="The specified developer does not exist."
detail="The specified developer does not exist.",
)
}
# TODO: Add more exceptions
Expand Down
33 changes: 15 additions & 18 deletions agents-api/agents_api/queries/agent/list_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@
from typing import Any, Literal, TypeVar
from uuid import UUID

from beartype import beartype
from fastapi import HTTPException
from psycopg import errors as psycopg_errors

from ...autogen.openapi_model import Agent
from ...metrics.counters import increase_counter
from ..utils import (
pg_query,
partialclass,
pg_query,
rewrap_exceptions,
wrap_in_class,
)
from beartype import beartype
from psycopg import errors as psycopg_errors

from ...autogen.openapi_model import Agent

ModelT = TypeVar("ModelT", bound=Any)
T = TypeVar("T")


@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
HTTPException,
status_code=404,
detail="The specified developer does not exist."
detail="The specified developer does not exist.",
)
}
# TODO: Add more exceptions
Expand All @@ -47,15 +48,15 @@ def list_agents_query(
) -> tuple[str, dict]:
"""
Constructs query to list agents for a developer with pagination.
Args:
developer_id: UUID of the developer
limit: Maximum number of records to return
offset: Number of records to skip
sort_by: Field to sort by
direction: Sort direction ('asc' or 'desc')
metadata_filter: Optional metadata filters
Returns:
Tuple of (query, params)
"""
Expand All @@ -67,7 +68,7 @@ def list_agents_query(
metadata_clause = ""
if metadata_filter:
metadata_clause = "AND metadata @> %(metadata_filter)s::jsonb"

query = f"""
SELECT
agent_id,
Expand All @@ -87,14 +88,10 @@ def list_agents_query(
ORDER BY {sort_by} {direction}
LIMIT %(limit)s OFFSET %(offset)s;
"""

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


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

if metadata_filter:
params["metadata_filter"] = metadata_filter

return query, params
21 changes: 10 additions & 11 deletions agents-api/agents_api/queries/agent/patch_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@
from typing import Any, TypeVar
from uuid import UUID

from ...autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse
from beartype import beartype
from fastapi import HTTPException
from psycopg import errors as psycopg_errors

from ...autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse
from ...metrics.counters import increase_counter
from ..utils import (
pg_query,
partialclass,
pg_query,
rewrap_exceptions,
wrap_in_class,
)
from beartype import beartype
from psycopg import errors as psycopg_errors

ModelT = TypeVar("ModelT", bound=Any)
T = TypeVar("T")


@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
HTTPException,
status_code=404,
detail="The specified developer does not exist."
detail="The specified developer does not exist.",
)
}
# TODO: Add more exceptions
Expand All @@ -41,10 +43,7 @@
@increase_counter("patch_agent")
@beartype
def patch_agent_query(
*,
agent_id: UUID,
developer_id: UUID,
data: PatchAgentRequest
*, agent_id: UUID, developer_id: UUID, data: PatchAgentRequest
) -> tuple[str, dict]:
"""
Constructs the SQL query to partially update an agent's details.
Expand All @@ -67,7 +66,7 @@ def patch_agent_query(
params[key] = value

set_clause = ", ".join(set_clauses)

query = f"""
UPDATE agents
SET {set_clause}
Expand Down
23 changes: 12 additions & 11 deletions agents-api/agents_api/queries/agent/update_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@
from typing import Any, TypeVar
from uuid import UUID

from ...autogen.openapi_model import ResourceUpdatedResponse, UpdateAgentRequest
from beartype import beartype
from fastapi import HTTPException
from psycopg import errors as psycopg_errors

from ...autogen.openapi_model import ResourceUpdatedResponse, UpdateAgentRequest
from ...metrics.counters import increase_counter
from ..utils import (
pg_query,
partialclass,
pg_query,
rewrap_exceptions,
wrap_in_class,
)
from beartype import beartype
from psycopg import errors as psycopg_errors

ModelT = TypeVar("ModelT", bound=Any)
T = TypeVar("T")


@rewrap_exceptions(
{
psycopg_errors.ForeignKeyViolation: partialclass(
HTTPException,
HTTPException,
status_code=404,
detail="The specified developer does not exist."
detail="The specified developer does not exist.",
)
}
# TODO: Add more exceptions
Expand All @@ -41,10 +43,7 @@
@increase_counter("update_agent")
@beartype
def update_agent_query(
*,
agent_id: UUID,
developer_id: UUID,
data: UpdateAgentRequest
*, agent_id: UUID, developer_id: UUID, data: UpdateAgentRequest
) -> tuple[str, dict]:
"""
Constructs the SQL query to fully update an agent's details.
Expand All @@ -57,7 +56,9 @@ def update_agent_query(
Returns:
tuple[str, dict]: A tuple containing the SQL query and its parameters.
"""
fields = ", ".join([f"{key} = %({key})s" for key in data.model_dump(exclude_unset=True).keys()])
fields = ", ".join(
[f"{key} = %({key})s" for key in data.model_dump(exclude_unset=True).keys()]
)
params = {key: value for key, value in data.model_dump(exclude_unset=True).items()}

query = f"""
Expand Down
Loading

0 comments on commit b11247f

Please sign in to comment.