-
Notifications
You must be signed in to change notification settings - Fork 902
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add developer queries, create db connection pool
- Loading branch information
1 parent
6c37070
commit 8901a59
Showing
9 changed files
with
206 additions
and
53 deletions.
There are no files selected for viewing
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,35 @@ | ||
import json | ||
import asyncpg | ||
from contextlib import asynccontextmanager | ||
from fastapi import FastAPI | ||
from prometheus_fastapi_instrumentator import Instrumentator | ||
from .env import api_prefix, db_dsn | ||
from .clients.pg import create_db_pool | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
app.state.postgres_pool = await create_db_pool(db_dsn) | ||
yield | ||
await app.state.postgres_pool.close() | ||
|
||
|
||
app: FastAPI = FastAPI( | ||
docs_url="/swagger", | ||
openapi_prefix=api_prefix, | ||
redoc_url=None, | ||
title="Julep Agents API", | ||
description="API for Julep Agents", | ||
version="0.4.0", | ||
terms_of_service="https://www.julep.ai/terms", | ||
contact={ | ||
"name": "Julep", | ||
"url": "https://www.julep.ai", | ||
"email": "[email protected]", | ||
}, | ||
root_path=api_prefix, | ||
lifespan=lifespan, | ||
) | ||
|
||
# Enable metrics | ||
Instrumentator().instrument(app).expose(app, include_in_schema=False) |
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 |
---|---|---|
@@ -1,21 +1,15 @@ | ||
import json | ||
|
||
import asyncpg | ||
|
||
from ..env import db_dsn | ||
from ..web import app | ||
|
||
async def _init_conn(conn): | ||
await conn.set_type_codec( | ||
"jsonb", | ||
encoder=json.dumps, | ||
decoder=json.loads, | ||
schema="pg_catalog", | ||
) | ||
|
||
async def get_pg_client(): | ||
# TODO: Create a postgres connection pool | ||
client = getattr(app.state, "pg_client", await asyncpg.connect(db_dsn)) | ||
if not hasattr(app.state, "pg_client"): | ||
await client.set_type_codec( | ||
"jsonb", | ||
encoder=json.dumps, | ||
decoder=json.loads, | ||
schema="pg_catalog", | ||
) | ||
app.state.pg_client = client | ||
|
||
return client | ||
async def create_db_pool(dsn: str): | ||
return await asyncpg.create_pool(dsn, init=_init_conn) |
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
54 changes: 54 additions & 0 deletions
54
agents-api/agents_api/queries/developer/create_developer.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,54 @@ | ||
from uuid import UUID | ||
|
||
from beartype import beartype | ||
from sqlglot import parse_one | ||
from uuid_extensions import uuid7 | ||
|
||
from ...common.protocol.developers import Developer | ||
from ..utils import ( | ||
pg_query, | ||
wrap_in_class, | ||
) | ||
|
||
query = parse_one(""" | ||
INSERT INTO developers ( | ||
developer_id, | ||
email, | ||
active, | ||
tags, | ||
settings | ||
) | ||
VALUES ( | ||
$1, | ||
$2, | ||
$3, | ||
$4, | ||
$5::jsonb | ||
) | ||
RETURNING *; | ||
""").sql(pretty=True) | ||
|
||
|
||
# @rewrap_exceptions( | ||
# { | ||
# QueryException: partialclass(HTTPException, status_code=403), | ||
# ValidationError: partialclass(HTTPException, status_code=500), | ||
# } | ||
# ) | ||
@wrap_in_class(Developer, one=True, transform=lambda d: {**d, "id": d["developer_id"]}) | ||
@pg_query | ||
@beartype | ||
async def create_developer( | ||
*, | ||
email: str, | ||
active: bool = True, | ||
tags: list[str] | None = None, | ||
settings: dict | None = None, | ||
developer_id: UUID | None = None, | ||
) -> tuple[str, list]: | ||
developer_id = str(developer_id or uuid7()) | ||
|
||
return ( | ||
query, | ||
[developer_id, email, active, tags or [], settings or {}], | ||
) |
42 changes: 42 additions & 0 deletions
42
agents-api/agents_api/queries/developer/patch_developer.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,42 @@ | ||
from uuid import UUID | ||
|
||
from beartype import beartype | ||
from sqlglot import parse_one | ||
|
||
from ...common.protocol.developers import Developer | ||
from ..utils import ( | ||
pg_query, | ||
wrap_in_class, | ||
) | ||
|
||
query = parse_one(""" | ||
UPDATE developers | ||
SET email = $1, active = $2, tags = tags || $3, settings = settings || $4 | ||
WHERE developer_id = $5 | ||
RETURNING *; | ||
""").sql(pretty=True) | ||
|
||
|
||
# @rewrap_exceptions( | ||
# { | ||
# QueryException: partialclass(HTTPException, status_code=403), | ||
# ValidationError: partialclass(HTTPException, status_code=500), | ||
# } | ||
# ) | ||
@wrap_in_class(Developer, one=True, transform=lambda d: {**d, "id": d["developer_id"]}) | ||
@pg_query | ||
@beartype | ||
async def patch_developer( | ||
*, | ||
developer_id: UUID, | ||
email: str, | ||
active: bool = True, | ||
tags: list[str] | None = None, | ||
settings: dict | None = None, | ||
) -> tuple[str, list]: | ||
developer_id = str(developer_id) | ||
|
||
return ( | ||
query, | ||
[email, active, tags or [], settings or {}, developer_id], | ||
) |
42 changes: 42 additions & 0 deletions
42
agents-api/agents_api/queries/developer/update_developer.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,42 @@ | ||
from uuid import UUID | ||
|
||
from beartype import beartype | ||
from sqlglot import parse_one | ||
|
||
from ...common.protocol.developers import Developer | ||
from ..utils import ( | ||
pg_query, | ||
wrap_in_class, | ||
) | ||
|
||
query = parse_one(""" | ||
UPDATE developers | ||
SET email = $1, active = $2, tags = $3, settings = $4 | ||
WHERE developer_id = $5 | ||
RETURNING *; | ||
""").sql(pretty=True) | ||
|
||
|
||
# @rewrap_exceptions( | ||
# { | ||
# QueryException: partialclass(HTTPException, status_code=403), | ||
# ValidationError: partialclass(HTTPException, status_code=500), | ||
# } | ||
# ) | ||
@wrap_in_class(Developer, one=True, transform=lambda d: {**d, "id": d["developer_id"]}) | ||
@pg_query | ||
@beartype | ||
async def update_developer( | ||
*, | ||
developer_id: UUID, | ||
email: str, | ||
active: bool = True, | ||
tags: list[str] | None = None, | ||
settings: dict | None = None, | ||
) -> tuple[str, list]: | ||
developer_id = str(developer_id) | ||
|
||
return ( | ||
query, | ||
[email, active, tags or [], settings or {}, developer_id], | ||
) |
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 |
---|---|---|
|
@@ -14,12 +14,12 @@ | |
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi.responses import JSONResponse | ||
from litellm.exceptions import APIError | ||
from prometheus_fastapi_instrumentator import Instrumentator | ||
from pycozo.client import QueryException | ||
from pydantic import ValidationError | ||
from scalar_fastapi import get_scalar_api_reference | ||
from temporalio.service import RPCError | ||
|
||
from .app import app | ||
from .common.exceptions import BaseCommonException | ||
from .dependencies.auth import get_api_key | ||
from .env import api_prefix, hostname, protocol, public_port, sentry_dsn | ||
|
@@ -144,24 +144,7 @@ def register_exceptions(app: FastAPI) -> None: | |
# Because some routes don't require auth | ||
# See: https://fastapi.tiangolo.com/tutorial/bigger-applications/ | ||
# | ||
app: FastAPI = FastAPI( | ||
docs_url="/swagger", | ||
openapi_prefix=api_prefix, | ||
redoc_url=None, | ||
title="Julep Agents API", | ||
description="API for Julep Agents", | ||
version="0.4.0", | ||
terms_of_service="https://www.julep.ai/terms", | ||
contact={ | ||
"name": "Julep", | ||
"url": "https://www.julep.ai", | ||
"email": "[email protected]", | ||
}, | ||
root_path=api_prefix, | ||
) | ||
|
||
# Enable metrics | ||
Instrumentator().instrument(app).expose(app, include_in_schema=False) | ||
|
||
# Create a new router for the docs | ||
scalar_router = APIRouter() | ||
|