Skip to content

Commit

Permalink
refactor(backend): move general things into utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbloxhub committed Jan 27, 2025
1 parent 3f9a18b commit 007f9ef
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 59 deletions.
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
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)]

0 comments on commit 007f9ef

Please sign in to comment.