Skip to content

Commit

Permalink
feat(agents-api): Refactor routers.py files
Browse files Browse the repository at this point in the history
Signed-off-by: Diwank Tomer <[email protected]>
  • Loading branch information
Diwank Tomer committed Jul 1, 2024
1 parent c36b94c commit 31e6814
Show file tree
Hide file tree
Showing 25 changed files with 227 additions and 175 deletions.
2 changes: 1 addition & 1 deletion agents-api/agents_api/autogen/openapi_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ class ImageUrl(BaseModel):
"""
URL or base64 data url (e.g. `data:image/jpeg;base64,<the base64 encoded image>`)
"""
detail: Detail | None = "auto"
detail: Detail | None = "auto" # pytype: disable=annotation-type-mismatch
"""
image detail to feed into the model can be low | high | auto
"""
Expand Down
9 changes: 7 additions & 2 deletions agents-api/agents_api/routers/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from fastapi import APIRouter
from .router import router # noqa: F401

router = APIRouter()
from .create_agent import create_agent # noqa: F401
from .delete_agent import delete_agent # noqa: F401
from .get_agent_details import get_agent_details # noqa: F401
from .list_agents import list_agents # noqa: F401
from .update_agent import update_agent # noqa: F401
from .patch_agent import patch_agent # noqa: F401
17 changes: 10 additions & 7 deletions agents-api/agents_api/routers/agents/create_agent.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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
from fastapi import Depends
from pydantic import UUID4
from starlette.status import HTTP_201_CREATED

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

from .router import router

router = APIRouter()

@router.post("/agents", status_code=HTTP_201_CREATED, tags=["agents"])
async def create_agent(
Expand Down
23 changes: 12 additions & 11 deletions agents-api/agents_api/routers/agents/delete_agent.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from fastapi import APIRouter, Depends, HTTPException
from typing import Annotated

from fastapi import Depends, HTTPException
from pydantic import UUID4
from starlette.status import HTTP_202_ACCEPTED
from starlette.status import HTTP_202_ACCEPTED, HTTP_404_NOT_FOUND

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

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
from .router import router

router = APIRouter()

@router.delete("/agents/{agent_id}", status_code=HTTP_202_ACCEPTED, tags=["agents"])
async def delete_agent(
Expand All @@ -17,7 +20,5 @@ async def delete_agent(
try:
delete_agent_query(x_developer_id, agent_id)
except AgentNotFoundError as e:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND, detail=str(e)
)
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(e))
return ResourceDeletedResponse(id=agent_id, deleted_at=utcnow())
22 changes: 11 additions & 11 deletions agents-api/agents_api/routers/agents/get_agent_details.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from fastapi import APIRouter, Depends, HTTPException
from typing import Annotated

from fastapi import 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
from ...dependencies.developer_id import get_developer_id
from ...models.agent.get_agent import get_agent_query
from ...common.exceptions.agents import AgentNotFoundError
from ...autogen.openapi_model import Agent

from .router import router

router = APIRouter()

@router.get("/agents/{agent_id}", tags=["agents"])
async def get_agent_details(
Expand All @@ -17,10 +20,7 @@ async def get_agent_details(
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")
raise AgentNotFoundError(x_developer_id, agent_id)
return Agent(**agent)
except AgentNotFoundError as e:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=str(e)
)
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(e))
14 changes: 8 additions & 6 deletions agents-api/agents_api/routers/agents/list_agents.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from fastapi import APIRouter, Depends
from typing import List, Annotated

from fastapi import 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
from ...dependencies.developer_id import get_developer_id
from ...models.agent.list_agents import list_agents_query
from ...autogen.openapi_model import Agent

from .router import router

router = APIRouter()

@router.get("/agents", tags=["agents"])
async def list_agents(
Expand Down
27 changes: 16 additions & 11 deletions agents-api/agents_api/routers/agents/patch_agent.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from fastapi import APIRouter, Depends, HTTPException
from typing import Annotated

from fastapi import 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
from ...dependencies.developer_id import get_developer_id
from ...models.agent.patch_agent import patch_agent_query
from ...common.exceptions.agents import AgentNotFoundError
from ...autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse

from .router import router

router = APIRouter()

@router.patch("/agents/{agent_id}", response_model=ResourceUpdatedResponse, status_code=HTTP_200_OK, tags=["agents"])
@router.patch(
"/agents/{agent_id}",
response_model=ResourceUpdatedResponse,
status_code=HTTP_200_OK,
tags=["agents"],
)
async def patch_agent(
agent_id: UUID4,
request: PatchAgentRequest,
Expand All @@ -28,7 +36,4 @@ async def patch_agent(
)
return ResourceUpdatedResponse(**updated_agent)
except AgentNotFoundError as e:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=str(e)
)
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(e))
3 changes: 3 additions & 0 deletions agents-api/agents_api/routers/agents/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from fastapi import APIRouter

router = APIRouter()
27 changes: 16 additions & 11 deletions agents-api/agents_api/routers/agents/update_agent.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from fastapi import APIRouter, Depends, HTTPException
from typing import Annotated

from fastapi import 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.update_agent import update_agent_query
from agents_api.common.exceptions.agents import AgentNotFoundError
from agents_api.autogen.openapi_model import UpdateAgentRequest, ResourceUpdatedResponse
from ...dependencies.developer_id import get_developer_id
from ...models.agent.update_agent import update_agent_query
from ...common.exceptions.agents import AgentNotFoundError
from ...autogen.openapi_model import UpdateAgentRequest, ResourceUpdatedResponse

from .router import router

router = APIRouter()

@router.put("/agents/{agent_id}", response_model=ResourceUpdatedResponse, status_code=HTTP_200_OK, tags=["agents"])
@router.put(
"/agents/{agent_id}",
response_model=ResourceUpdatedResponse,
status_code=HTTP_200_OK,
tags=["agents"],
)
async def update_agent(
agent_id: UUID4,
request: UpdateAgentRequest,
Expand All @@ -28,7 +36,4 @@ async def update_agent(
)
return ResourceUpdatedResponse(**updated_agent)
except AgentNotFoundError as e:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=str(e)
)
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(e))
9 changes: 7 additions & 2 deletions agents-api/agents_api/routers/sessions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from fastapi import APIRouter
from .router import router # noqa: F401

router = APIRouter()
from .create_session import create_session # noqa: F401
from .delete_session import delete_session # noqa: F401
from .get_session import get_session # noqa: F401
from .list_sessions import list_sessions # noqa: F401
from .update_session import update_session # noqa: F401
from .patch_session import patch_session # noqa: F401
19 changes: 13 additions & 6 deletions agents-api/agents_api/routers/sessions/create_session.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from fastapi import APIRouter, HTTPException, status, Depends
from pydantic import BaseModel, UUID4
from typing import Annotated
from uuid import uuid4

from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.session.create_session import create_session_query
from agents_api.autogen.openapi_model import CreateSessionRequest, ResourceCreatedResponse
from fastapi import Depends
from pydantic import UUID4
import pandas as pd
from starlette.status import HTTP_201_CREATED

from ...dependencies.developer_id import get_developer_id
from ...models.session.create_session import create_session_query
from ...autogen.openapi_model import (
CreateSessionRequest,
ResourceCreatedResponse,
)

from .router import router

router = APIRouter()

@router.post("/sessions", status_code=HTTP_201_CREATED, tags=["sessions"])
async def create_session(
Expand Down
28 changes: 15 additions & 13 deletions agents-api/agents_api/routers/sessions/delete_session.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
from fastapi import APIRouter, Depends, HTTPException
from typing import Annotated

from fastapi import Depends, HTTPException
from pydantic import UUID4
from starlette.status import HTTP_202_ACCEPTED
from starlette.status import HTTP_202_ACCEPTED, HTTP_404_NOT_FOUND

from ...autogen.openapi_model import ResourceDeletedResponse
from ...common.exceptions.sessions import SessionNotFoundError
from ...common.utils.datetime import utcnow
from ...dependencies.developer_id import get_developer_id
from ...models.session.delete_session import delete_session_query

from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.session.delete_session import delete_session_query
from agents_api.common.exceptions.sessions import SessionNotFoundError
from agents_api.autogen.openapi_model import ResourceDeletedResponse
from agents_api.common.utils.datetime import utcnow
from .router import router

router = APIRouter()

@router.delete("/sessions/{session_id}", status_code=HTTP_202_ACCEPTED, tags=["sessions"])
@router.delete(
"/sessions/{session_id}", status_code=HTTP_202_ACCEPTED, tags=["sessions"]
)
async def delete_session(
session_id: UUID4, x_developer_id: Annotated[UUID4, Depends(get_developer_id)]
) -> ResourceDeletedResponse:
try:
delete_session_query(x_developer_id, session_id)
except SessionNotFoundError as e:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=str(e)
)
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(e))

return ResourceDeletedResponse(id=session_id, deleted_at=utcnow())
14 changes: 8 additions & 6 deletions agents-api/agents_api/routers/sessions/get_session.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from fastapi import APIRouter, Depends, HTTPException
from pydantic import UUID4
from typing import Annotated

from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.session.get_session import get_session_query
from agents_api.autogen.openapi_model import Session
from fastapi import Depends, HTTPException
from pydantic import UUID4

from ...dependencies.developer_id import get_developer_id
from ...models.session.get_session import get_session_query
from ...autogen.openapi_model import Session

from .router import router

router = APIRouter()

@router.get("/sessions/{session_id}", tags=["sessions"])
async def get_session(
Expand Down
18 changes: 12 additions & 6 deletions agents-api/agents_api/routers/sessions/list_sessions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
from fastapi import APIRouter, Depends
import json
from json import JSONDecodeError
from typing import Annotated
from pydantic import UUID4
from agents_api.dependencies.developer_id import get_developer_id
from agents_api.models.session.list_sessions import list_sessions_query
from agents_api.autogen.openapi_model import Session

router = APIRouter()
from fastapi import Depends, HTTPException, status
from pydantic import UUID4, BaseModel

from ...dependencies.developer_id import get_developer_id
from ...models.session.list_sessions import list_sessions_query
from ...autogen.openapi_model import Session

from .router import router


class SessionList(BaseModel):
items: list[Session]


@router.get("/sessions", tags=["sessions"])
async def list_sessions(
x_developer_id: Annotated[UUID4, Depends(get_developer_id)],
Expand Down
18 changes: 12 additions & 6 deletions agents-api/agents_api/routers/sessions/patch_session.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from fastapi import APIRouter, Depends, HTTPException
from typing import Annotated

from fastapi import 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.session.patch_session import patch_session_query
from agents_api.autogen.openapi_model import PatchSessionRequest, ResourceUpdatedResponse
from agents_api.common.exceptions.sessions import SessionNotFoundError
from ...common.exceptions.sessions import SessionNotFoundError
from ...dependencies.developer_id import get_developer_id
from ...models.session.patch_session import patch_session_query
from ...autogen.openapi_model import (
PatchSessionRequest,
ResourceUpdatedResponse,
)

from .router import router

router = APIRouter()

@router.patch("/sessions/{session_id}", tags=["sessions"])
async def patch_session(
Expand Down
3 changes: 3 additions & 0 deletions agents-api/agents_api/routers/sessions/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from fastapi import APIRouter

router = APIRouter()
Loading

0 comments on commit 31e6814

Please sign in to comment.