From 2cf1dda3bc9e126daa5138ce54a28a57bd527736 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Thu, 16 Jan 2025 16:33:33 +0200 Subject: [PATCH] Initial CRUD API for workspaces This adds a simple and unimplemented REST API for workspaces. Workspaces will be the base for all other resources in terms of REST resource mapping, so these go first. These are initially left entirely unimplemented as https://github.com/stacklok/codegate/pull/600 needs to merge Signed-off-by: Juan Antonio Osorio --- src/codegate/api/__init__.py | 0 src/codegate/api/v1.py | 40 +++++++++++++++++++++++++++++++++++ src/codegate/api/v1_models.py | 20 ++++++++++++++++++ src/codegate/server.py | 4 ++++ 4 files changed, 64 insertions(+) create mode 100644 src/codegate/api/__init__.py create mode 100644 src/codegate/api/v1.py create mode 100644 src/codegate/api/v1_models.py diff --git a/src/codegate/api/__init__.py b/src/codegate/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/codegate/api/v1.py b/src/codegate/api/v1.py new file mode 100644 index 00000000..10e274b7 --- /dev/null +++ b/src/codegate/api/v1.py @@ -0,0 +1,40 @@ +from fastapi import APIRouter +from fastapi.routing import APIRoute + +from codegate.api import v1_models + +v1 = APIRouter() + + +def uniq_name(route: APIRoute): + return f"v1_{route.name}" + + +@v1.get("/workspaces", tags=["Workspaces"], generate_unique_id_function=uniq_name) +async def list_workspaces() -> v1_models.ListWorkspacesResponse: + """List all workspaces.""" + raise NotImplementedError + +@v1.get("/workspaces/active", tags=["Workspaces"], generate_unique_id_function=uniq_name) +async def list_active_workspaces() -> v1_models.ListActiveWorkspacesResponse: + """List all active workspaces. + + In it's current form, this function will only return one workspace. That is, + the globally active workspace.""" + raise NotImplementedError + +@v1.post("/workspaces", tags=["Workspaces"], generate_unique_id_function=uniq_name, status_code=201) +async def create_workspace(request: v1_models.CreateWorkspaceRequest): + """Create a new workspace.""" + raise NotImplementedError + +@v1.get("/workspaces/{workspace_name}", tags=["Workspaces"], generate_unique_id_function=uniq_name) +async def get_workspace(workspace_name: str) -> v1_models.Workspace: + """Get a workspace by name.""" + raise NotImplementedError + +@v1.delete("/workspaces/{workspace_name}", tags=["Workspaces"], + generate_unique_id_function=uniq_name, status_code=204) +async def delete_workspace(workspace_name: str): + """Delete a workspace by name.""" + raise NotImplementedError diff --git a/src/codegate/api/v1_models.py b/src/codegate/api/v1_models.py new file mode 100644 index 00000000..5254defc --- /dev/null +++ b/src/codegate/api/v1_models.py @@ -0,0 +1,20 @@ +from typing import Any + +import pydantic + + +class Workspace(pydantic.BaseModel): + name: str + +class ActiveWorkspace(Workspace): + # TODO: use a more specific type for last_updated + last_updated: Any + +class ListWorkspacesResponse(pydantic.BaseModel): + workspaces: list[Workspace] + +class ListActiveWorkspacesResponse(pydantic.BaseModel): + workspaces: list[ActiveWorkspace] + +class CreateWorkspaceRequest(pydantic.BaseModel): + name: str diff --git a/src/codegate/server.py b/src/codegate/server.py index b995fdd7..d1da668e 100644 --- a/src/codegate/server.py +++ b/src/codegate/server.py @@ -7,6 +7,7 @@ from starlette.middleware.errors import ServerErrorMiddleware from codegate import __description__, __version__ +from codegate.api.v1 import v1 from codegate.dashboard.dashboard import dashboard_router from codegate.pipeline.factory import PipelineFactory from codegate.providers.anthropic.provider import AnthropicProvider @@ -97,4 +98,7 @@ async def health_check(): app.include_router(system_router) app.include_router(dashboard_router) + # CodeGate API + app.include_router(v1, prefix="/api/v1", tags=["CodeGate API"]) + return app