Skip to content

Commit d1756da

Browse files
committed
refactor: integrate CoderAPI into authentication and workspace management
- Added a new dependency function to provide a CoderAPI instance for use in routers. - Updated auth_router to utilize CoderAPI in the callback endpoint, enhancing authentication flow. - Modified workspace_router to incorporate CoderAPI in workspace state retrieval and management functions, improving workspace operations. - Streamlined dependency management across routers for better modularity and code organization.
1 parent de080bb commit d1756da

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

src/backend/dependencies.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from typing import Optional, Dict, Any
33
from uuid import UUID
44

5-
from fastapi import Request, HTTPException
5+
from fastapi import Request, HTTPException, Depends
66

77
from config import get_session, is_token_expired, refresh_token
88
from database.service import UserService
9+
from coder import CoderAPI
910

1011
class UserSession:
1112
"""
@@ -130,3 +131,9 @@ def _handle_auth_error(self, detail: str, status_code: int = 401) -> Optional[No
130131
require_auth = AuthDependency(auto_error=True)
131132
optional_auth = AuthDependency(auto_error=False)
132133
require_admin = AuthDependency(auto_error=True, require_admin=True)
134+
135+
def get_coder_api():
136+
"""
137+
Dependency that provides a CoderAPI instance.
138+
"""
139+
return CoderAPI()

src/backend/routers/auth_router.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import secrets
22
import jwt
33
import httpx
4-
from fastapi import APIRouter, Request, HTTPException
4+
from fastapi import APIRouter, Request, HTTPException, Depends
55
from fastapi.responses import RedirectResponse, FileResponse, JSONResponse
66
import os
77

88
from config import get_auth_url, get_token_url, OIDC_CONFIG, set_session, delete_session, STATIC_DIR, get_session
9+
from dependencies import get_coder_api
910
from coder import CoderAPI
1011

1112
auth_router = APIRouter()
12-
coder_api = CoderAPI()
1313

1414
@auth_router.get("/login")
1515
async def login(request: Request, kc_idp_hint: str = None, popup: str = None):
@@ -31,7 +31,12 @@ async def login(request: Request, kc_idp_hint: str = None, popup: str = None):
3131
return response
3232

3333
@auth_router.get("/callback")
34-
async def callback(request: Request, code: str, state: str = "default"):
34+
async def callback(
35+
request: Request,
36+
code: str,
37+
state: str = "default",
38+
coder_api: CoderAPI = Depends(get_coder_api)
39+
):
3540
session_id = request.cookies.get('session_id')
3641
if not session_id:
3742
raise HTTPException(status_code=400, detail="No session")

src/backend/routers/workspace_router.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
from fastapi import APIRouter, Depends, HTTPException
55
from fastapi.responses import JSONResponse
66

7-
from dependencies import UserSession, require_auth
7+
from dependencies import UserSession, require_auth, get_coder_api
88
from coder import CoderAPI
99

1010
workspace_router = APIRouter()
11-
coder_api = CoderAPI()
1211

1312
class WorkspaceState(BaseModel):
1413
id: str
@@ -19,7 +18,10 @@ class WorkspaceState(BaseModel):
1918
agent: str
2019

2120
@workspace_router.get("/state", response_model=WorkspaceState)
22-
async def get_workspace_state(user: UserSession = Depends(require_auth)):
21+
async def get_workspace_state(
22+
user: UserSession = Depends(require_auth),
23+
coder_api: CoderAPI = Depends(get_coder_api)
24+
):
2325
"""
2426
Get the current state of the user's workspace
2527
"""
@@ -52,12 +54,15 @@ async def get_workspace_state(user: UserSession = Depends(require_auth)):
5254

5355

5456
@workspace_router.post("/start")
55-
async def start_workspace(user: UserSession = Depends(require_auth)):
57+
async def start_workspace(
58+
user: UserSession = Depends(require_auth),
59+
coder_api: CoderAPI = Depends(get_coder_api)
60+
):
5661
"""
5762
Start a workspace for the authenticated user
5863
"""
5964

60-
workspace: WorkspaceState = await get_workspace_state(user)
65+
workspace: WorkspaceState = await get_workspace_state(user, coder_api)
6166

6267
try:
6368
response = coder_api.start_workspace(workspace.id)
@@ -67,15 +72,18 @@ async def start_workspace(user: UserSession = Depends(require_auth)):
6772

6873

6974
@workspace_router.post("/stop")
70-
async def stop_workspace(user: UserSession = Depends(require_auth)):
75+
async def stop_workspace(
76+
user: UserSession = Depends(require_auth),
77+
coder_api: CoderAPI = Depends(get_coder_api)
78+
):
7179
"""
7280
Stop a workspace for the authenticated user
7381
"""
7482

75-
workspace: WorkspaceState = await get_workspace_state(user)
83+
workspace: WorkspaceState = await get_workspace_state(user, coder_api)
7684

7785
try:
7886
response = coder_api.stop_workspace(workspace.id)
7987
return JSONResponse(content=response)
8088
except Exception as e:
81-
raise HTTPException(status_code=500, detail=str(e))
89+
raise HTTPException(status_code=500, detail=str(e))

0 commit comments

Comments
 (0)