From 55500d97223c10913b751bc781003259a12b784e Mon Sep 17 00:00:00 2001 From: Vedantsahai18 Date: Sun, 15 Dec 2024 06:07:50 +0000 Subject: [PATCH] refactor: Lint agents-api (CI) --- .../agents_api/queries/users/__init__.py | 6 ++-- .../queries/users/create_or_update_user.py | 23 +++++++------- .../agents_api/queries/users/create_user.py | 31 ++++++++++--------- .../agents_api/queries/users/delete_user.py | 19 +++++++----- .../agents_api/queries/users/get_user.py | 19 +++++++----- .../agents_api/queries/users/list_users.py | 25 +++++++-------- .../agents_api/queries/users/patch_user.py | 24 +++++++------- .../agents_api/queries/users/update_user.py | 26 ++++++++-------- 8 files changed, 90 insertions(+), 83 deletions(-) diff --git a/agents-api/agents_api/queries/users/__init__.py b/agents-api/agents_api/queries/users/__init__.py index 4e810a4fe..d7988279e 100644 --- a/agents-api/agents_api/queries/users/__init__.py +++ b/agents-api/agents_api/queries/users/__init__.py @@ -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 @@ -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 @@ -25,4 +25,4 @@ "list_users_query", "patch_user_query", "update_user_query", -] \ No newline at end of file +] diff --git a/agents-api/agents_api/queries/users/create_or_update_user.py b/agents-api/agents_api/queries/users/create_or_update_user.py index a6312b243..67182d047 100644 --- a/agents-api/agents_api/queries/users/create_or_update_user.py +++ b/agents-api/agents_api/queries/users/create_or_update_user.py @@ -1,9 +1,9 @@ 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 @@ -11,22 +11,21 @@ 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. diff --git a/agents-api/agents_api/queries/users/create_user.py b/agents-api/agents_api/queries/users/create_user.py index 194d9bf03..0f979ebdd 100644 --- a/agents-api/agents_api/queries/users/create_user.py +++ b/agents-api/agents_api/queries/users/create_user.py @@ -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 @@ -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, diff --git a/agents-api/agents_api/queries/users/delete_user.py b/agents-api/agents_api/queries/users/delete_user.py index 551129f00..2dfb0b156 100644 --- a/agents-api/agents_api/queries/users/delete_user.py +++ b/agents-api/agents_api/queries/users/delete_user.py @@ -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 @@ -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} \ No newline at end of file + return [query], {"developer_id": developer_id, "user_id": user_id} diff --git a/agents-api/agents_api/queries/users/get_user.py b/agents-api/agents_api/queries/users/get_user.py index 3982ea46e..bccf70ad2 100644 --- a/agents-api/agents_api/queries/users/get_user.py +++ b/agents-api/agents_api/queries/users/get_user.py @@ -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 @@ -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} \ No newline at end of file + return query, {"developer_id": developer_id, "user_id": user_id} diff --git a/agents-api/agents_api/queries/users/list_users.py b/agents-api/agents_api/queries/users/list_users.py index 312299082..3c8a3690c 100644 --- a/agents-api/agents_api/queries/users/list_users.py +++ b/agents-api/agents_api/queries/users/list_users.py @@ -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 @@ -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" @@ -78,4 +77,4 @@ def list_users_query( OFFSET %(offset)s; """).sql() - return query, params \ No newline at end of file + return query, params diff --git a/agents-api/agents_api/queries/users/patch_user.py b/agents-api/agents_api/queries/users/patch_user.py index 468b38b00..40c6aff4d 100644 --- a/agents-api/agents_api/queries/users/patch_user.py +++ b/agents-api/agents_api/queries/users/patch_user.py @@ -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. @@ -70,4 +70,4 @@ def patch_user_query( updated_at; """).sql() - return query, params \ No newline at end of file + return query, params diff --git a/agents-api/agents_api/queries/users/update_user.py b/agents-api/agents_api/queries/users/update_user.py index ed33e3e42..58f7ae8b2 100644 --- a/agents-api/agents_api/queries/users/update_user.py +++ b/agents-api/agents_api/queries/users/update_user.py @@ -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. @@ -65,4 +65,4 @@ def update_user_query( "metadata": data.metadata or {}, } - return query, params \ No newline at end of file + return query, params