ref #8: [feat] add http endpoint
This commit is contained in:
parent
bae540427a
commit
d2506e0c63
7 changed files with 87 additions and 20 deletions
|
|
@ -1,9 +1,21 @@
|
|||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
from adapter.di.container import AppContainer
|
||||
from adapter.http.fastapi.dependencies import get_container, get_get_user
|
||||
from adapter.http.fastapi.schemas import ErrorResponse, HealthResponse, UserResponse
|
||||
from domain.error import UserNotFoundError
|
||||
from adapter.http.fastapi.dependencies import (
|
||||
get_container,
|
||||
get_create_sandbox,
|
||||
get_get_user,
|
||||
)
|
||||
from adapter.http.fastapi.schemas import (
|
||||
CreateSandboxRequest,
|
||||
ErrorResponse,
|
||||
HealthResponse,
|
||||
SandboxSessionResponse,
|
||||
UserResponse,
|
||||
)
|
||||
from domain.error import SandboxError, SandboxStartError, UserNotFoundError
|
||||
from domain.sandbox import SandboxSession
|
||||
from usecase.sandbox import CreateSandbox, CreateSandboxCommand
|
||||
from usecase.user import GetUser, GetUserQuery
|
||||
|
||||
router = APIRouter()
|
||||
|
|
@ -38,3 +50,42 @@ def get_user(user_id: str, usecase: GetUser = Depends(get_get_user)) -> UserResp
|
|||
) from exc
|
||||
|
||||
return UserResponse(id=user.id, email=user.email, name=user.name)
|
||||
|
||||
|
||||
@router.post(
|
||||
'/create',
|
||||
response_model=SandboxSessionResponse,
|
||||
responses={
|
||||
status.HTTP_503_SERVICE_UNAVAILABLE: {'model': ErrorResponse},
|
||||
status.HTTP_500_INTERNAL_SERVER_ERROR: {'model': ErrorResponse},
|
||||
},
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
def create_sandbox(
|
||||
request: CreateSandboxRequest,
|
||||
usecase: CreateSandbox = Depends(get_create_sandbox),
|
||||
) -> SandboxSessionResponse:
|
||||
try:
|
||||
session = usecase.execute(CreateSandboxCommand(chat_id=request.chat_id))
|
||||
except SandboxStartError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail=str(exc),
|
||||
) from exc
|
||||
except SandboxError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=str(exc),
|
||||
) from exc
|
||||
|
||||
return _to_sandbox_session_response(session)
|
||||
|
||||
|
||||
def _to_sandbox_session_response(session: SandboxSession) -> SandboxSessionResponse:
|
||||
return SandboxSessionResponse(
|
||||
session_id=session.session_id,
|
||||
chat_id=session.chat_id,
|
||||
container_id=session.container_id,
|
||||
status=session.status.value,
|
||||
expires_at=session.expires_at,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue