Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(backend): move general things into utils.py #61

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 2 additions & 58 deletions src/aigis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import os
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from typing import Annotated

from fastapi import Depends, FastAPI, Request
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from openai import AsyncOpenAI
from psycopg import AsyncConnection

from supabase import AsyncClient, AsyncClientOptions, create_async_client
from aigis.utils import SupabaseDep

supabase_url: str = os.environ.get("AIGIS_SUPABASE_URL", "http://localhost:8068")
supabase_key: str = os.environ.get(
"AIGIS_SUPABASE_KEY",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0",
)
openai_url = os.environ.get("OPENAI_URL", "https://api.openai.com/v1")
openai_key = os.environ.get("OPENAI_API_KEY", "your_api_key_here")
openai_model = os.environ.get("OPENAI_MODEL", "gpt-4o")
postgres_url = os.environ.get(
"POSTGRES_URL", "postgresql://postgres:postgres@127.0.0.1:8067/postgres"
)
app = FastAPI()

origins = [os.environ.get("AIGIS_FRONTEND_URL", "http://localhost:8071")]
Expand All @@ -34,46 +18,6 @@
)


async def supabase_client(request: Request) -> AsyncGenerator[AsyncClient]:
supa = await create_async_client(
supabase_url=supabase_url,
supabase_key=supabase_key,
options=AsyncClientOptions(
headers={
"Authorization": request.headers.get(
"Authorization", f"Bearer {supabase_key}"
)
}
),
)
try:
yield supa
finally:
pass


async def openai_client(_request: Request) -> AsyncGenerator[AsyncOpenAI]:
openai = AsyncOpenAI(api_key=openai_key, base_url=openai_url)
try:
yield openai
finally:
pass


@asynccontextmanager
async def postgres_client(_request: Request) -> AsyncGenerator[AsyncConnection]:
conn = await AsyncConnection.connect(postgres_url)
try:
yield conn
finally:
await conn.close()


SupabaseDep = Annotated[AsyncClient, Depends(supabase_client)]
OpenAIDep = Annotated[AsyncOpenAI, Depends(openai_client)]
PostgresDep = Annotated[AsyncConnection, Depends(postgres_client)]


@app.get("/")
async def hello_world(request: Request, _supa: SupabaseDep):
return request.headers.get("Authorization")
3 changes: 2 additions & 1 deletion src/aigis/test_something.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from httpx import ASGITransport, AsyncClient
from psycopg import AsyncConnection

from aigis import app, postgres_client
from aigis import app
from aigis.utils import postgres_client

pytestmark = [pytest.mark.anyio, pytest.mark.parametrize("anyio_backend", ["asyncio"])]

Expand Down
62 changes: 62 additions & 0 deletions src/aigis/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from typing import Annotated

from fastapi import Depends, Request
from openai import AsyncOpenAI
from psycopg import AsyncConnection

from supabase import AsyncClient, AsyncClientOptions, create_async_client

supabase_url: str = os.environ.get("AIGIS_SUPABASE_URL", "http://localhost:8068")
supabase_key: str = os.environ.get(
"AIGIS_SUPABASE_KEY",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0",
)
openai_url = os.environ.get("OPENAI_URL", "https://api.openai.com/v1")
openai_key = os.environ.get("OPENAI_API_KEY", "your_api_key_here")
openai_model = os.environ.get("OPENAI_MODEL", "gpt-4o")
postgres_url = os.environ.get(
"POSTGRES_URL", "postgresql://postgres:postgres@127.0.0.1:8067/postgres"
)


async def supabase_client(request: Request) -> AsyncGenerator[AsyncClient]:
supa = await create_async_client(
supabase_url=supabase_url,
supabase_key=supabase_key,
options=AsyncClientOptions(
headers={
"Authorization": request.headers.get(
"Authorization", f"Bearer {supabase_key}"
)
}
),
)
try:
yield supa
finally:
pass


async def openai_client(_request: Request) -> AsyncGenerator[AsyncOpenAI]:
openai = AsyncOpenAI(api_key=openai_key, base_url=openai_url)
try:
yield openai

Check warning on line 46 in src/aigis/utils.py

View check run for this annotation

Codecov / codecov/patch

src/aigis/utils.py#L44-L46

Added lines #L44 - L46 were not covered by tests
finally:
pass

Check warning on line 48 in src/aigis/utils.py

View check run for this annotation

Codecov / codecov/patch

src/aigis/utils.py#L48

Added line #L48 was not covered by tests


@asynccontextmanager
async def postgres_client(_request: Request) -> AsyncGenerator[AsyncConnection]:
conn = await AsyncConnection.connect(postgres_url)
try:
yield conn
finally:
await conn.close()


SupabaseDep = Annotated[AsyncClient, Depends(supabase_client)]
OpenAIDep = Annotated[AsyncOpenAI, Depends(openai_client)]
PostgresDep = Annotated[AsyncConnection, Depends(postgres_client)]
Loading