Skip to content

Commit

Permalink
refactor(agents-api): Rework routers to split routes into individual …
Browse files Browse the repository at this point in the history
…files
  • Loading branch information
creatorrr committed Jun 30, 2024
1 parent 7c7ba7c commit c36b94c
Show file tree
Hide file tree
Showing 24 changed files with 583 additions and 1,251 deletions.
4 changes: 3 additions & 1 deletion agents-api/agents_api/routers/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .routers import router # noqa: F401
from fastapi import APIRouter

router = APIRouter()
26 changes: 26 additions & 0 deletions agents-api/agents_api/routers/agents/create_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import UUID4
from typing import Annotated

from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.agent.create_agent import create_agent_query
from agents_api.autogen.openapi_model import CreateAgentRequest, ResourceCreatedResponse
from agents_api.common.utils.datetime import utcnow

router = APIRouter()

@router.post("/agents", status_code=HTTP_201_CREATED, tags=["agents"])
async def create_agent(
request: CreateAgentRequest,
x_developer_id: Annotated[UUID4, Depends(get_developer_id)],
) -> ResourceCreatedResponse:
agent_id = create_agent_query(
developer_id=x_developer_id,
name=request.name,
about=request.about,
instructions=request.instructions,
model=request.model,
default_settings=request.default_settings,
metadata=request.metadata,
)
return ResourceCreatedResponse(id=agent_id, created_at=utcnow())
23 changes: 23 additions & 0 deletions agents-api/agents_api/routers/agents/delete_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from fastapi import APIRouter, Depends, HTTPException
from pydantic import UUID4
from starlette.status import HTTP_202_ACCEPTED

from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.agent.delete_agent import delete_agent_query
from agents_api.common.exceptions.agents import AgentNotFoundError
from agents_api.common.utils.datetime import utcnow
from agents_api.autogen.openapi_model import ResourceDeletedResponse

router = APIRouter()

@router.delete("/agents/{agent_id}", status_code=HTTP_202_ACCEPTED, tags=["agents"])
async def delete_agent(
agent_id: UUID4, x_developer_id: Annotated[UUID4, Depends(get_developer_id)]
) -> ResourceDeletedResponse:
try:
delete_agent_query(x_developer_id, agent_id)
except AgentNotFoundError as e:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND, detail=str(e)
)
return ResourceDeletedResponse(id=agent_id, deleted_at=utcnow())
26 changes: 26 additions & 0 deletions agents-api/agents_api/routers/agents/get_agent_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from fastapi import APIRouter, Depends, HTTPException
from pydantic import UUID4
from starlette.status import HTTP_404_NOT_FOUND

from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.agent.get_agent import get_agent_query
from agents_api.common.exceptions.agents import AgentNotFoundError
from agents_api.autogen.openapi_model import Agent

router = APIRouter()

@router.get("/agents/{agent_id}", tags=["agents"])
async def get_agent_details(
agent_id: UUID4,
x_developer_id: Annotated[UUID4, Depends(get_developer_id)],
) -> Agent:
try:
agent = get_agent_query(developer_id=x_developer_id, agent_id=agent_id)
if not agent:
raise AgentNotFoundError(f"Agent with ID {agent_id} not found")
return Agent(**agent)
except AgentNotFoundError as e:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=str(e)
)
24 changes: 24 additions & 0 deletions agents-api/agents_api/routers/agents/list_agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from fastapi import APIRouter, Depends
from pydantic import UUID4
from typing import List

from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.agent.list_agents import list_agents_query
from agents_api.autogen.openapi_model import Agent

router = APIRouter()

@router.get("/agents", tags=["agents"])
async def list_agents(
x_developer_id: Annotated[UUID4, Depends(get_developer_id)],
limit: int = 100,
offset: int = 0,
metadata_filter: str = "{}",
) -> List[Agent]:
agents = list_agents_query(
developer_id=x_developer_id,
limit=limit,
offset=offset,
metadata_filter=metadata_filter,
)
return [Agent(**agent) for agent in agents]
34 changes: 34 additions & 0 deletions agents-api/agents_api/routers/agents/patch_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from fastapi import APIRouter, Depends, HTTPException
from pydantic import UUID4
from starlette.status import HTTP_404_NOT_FOUND, HTTP_200_OK

from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.agent.patch_agent import patch_agent_query
from agents_api.common.exceptions.agents import AgentNotFoundError
from agents_api.autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse

router = APIRouter()

@router.patch("/agents/{agent_id}", response_model=ResourceUpdatedResponse, status_code=HTTP_200_OK, tags=["agents"])
async def patch_agent(
agent_id: UUID4,
request: PatchAgentRequest,
x_developer_id: Annotated[UUID4, Depends(get_developer_id)],
) -> ResourceUpdatedResponse:
try:
updated_agent = patch_agent_query(
agent_id=agent_id,
developer_id=x_developer_id,
default_settings=request.default_settings,
name=request.name,
about=request.about,
model=request.model,
metadata=request.metadata,
instructions=request.instructions,
)
return ResourceUpdatedResponse(**updated_agent)
except AgentNotFoundError as e:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=str(e)
)
Loading

0 comments on commit c36b94c

Please sign in to comment.