|
| 1 | +from fastapi import APIRouter, Response |
| 2 | +from fastapi.exceptions import HTTPException |
| 3 | +from fastapi.routing import APIRoute |
| 4 | + |
| 5 | +from codegate.api import v1_models |
| 6 | +from codegate.pipeline.workspace import commands as wscmd |
| 7 | + |
| 8 | +v1 = APIRouter() |
| 9 | +wscrud = wscmd.WorkspaceCrud() |
| 10 | + |
| 11 | + |
| 12 | +def uniq_name(route: APIRoute): |
| 13 | + return f"v1_{route.name}" |
| 14 | + |
| 15 | + |
| 16 | +@v1.get("/workspaces", tags=["Workspaces"], generate_unique_id_function=uniq_name) |
| 17 | +async def list_workspaces() -> v1_models.ListWorkspacesResponse: |
| 18 | + """List all workspaces.""" |
| 19 | + wslist = await wscrud.get_workspaces() |
| 20 | + |
| 21 | + resp = v1_models.ListWorkspacesResponse.from_db_workspaces(wslist) |
| 22 | + |
| 23 | + return resp |
| 24 | + |
| 25 | + |
| 26 | +@v1.get("/workspaces/active", tags=["Workspaces"], generate_unique_id_function=uniq_name) |
| 27 | +async def list_active_workspaces() -> v1_models.ListActiveWorkspacesResponse: |
| 28 | + """List all active workspaces. |
| 29 | +
|
| 30 | + In it's current form, this function will only return one workspace. That is, |
| 31 | + the globally active workspace.""" |
| 32 | + activews = await wscrud.get_active_workspace() |
| 33 | + |
| 34 | + resp = v1_models.ListActiveWorkspacesResponse.from_db_workspaces(activews) |
| 35 | + |
| 36 | + return resp |
| 37 | + |
| 38 | + |
| 39 | +@v1.post("/workspaces/active", tags=["Workspaces"], generate_unique_id_function=uniq_name) |
| 40 | +async def activate_workspace(request: v1_models.ActivateWorkspaceRequest, status_code=204): |
| 41 | + """Activate a workspace by name.""" |
| 42 | + activated = await wscrud.activate_workspace(request.name) |
| 43 | + |
| 44 | + # TODO: Refactor |
| 45 | + if not activated: |
| 46 | + return HTTPException(status_code=409, detail="Workspace already active") |
| 47 | + |
| 48 | + return Response(status_code=204) |
| 49 | + |
| 50 | + |
| 51 | +@v1.post("/workspaces", tags=["Workspaces"], generate_unique_id_function=uniq_name, status_code=201) |
| 52 | +async def create_workspace(request: v1_models.CreateWorkspaceRequest): |
| 53 | + """Create a new workspace.""" |
| 54 | + # Input validation is done in the model |
| 55 | + created = await wscrud.add_workspace(request.name) |
| 56 | + |
| 57 | + # TODO: refactor to use a more specific exception |
| 58 | + if not created: |
| 59 | + raise HTTPException(status_code=400, detail="Failed to create workspace") |
| 60 | + |
| 61 | + return v1_models.Workspace(name=request.name) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +@v1.delete("/workspaces/{workspace_name}", tags=["Workspaces"], |
| 66 | + generate_unique_id_function=uniq_name, status_code=204) |
| 67 | +async def delete_workspace(workspace_name: str): |
| 68 | + """Delete a workspace by name.""" |
| 69 | + raise NotImplementedError |
0 commit comments