|
| 1 | +from __future__ import annotations |
| 2 | +from pydantic import BaseModel, PrivateAttr |
| 3 | +from typing import Optional, List, TYPE_CHECKING |
| 4 | + |
| 5 | +if TYPE_CHECKING: |
| 6 | + from ...client import APIHttpClient |
| 7 | + |
| 8 | + |
| 9 | +class EnvVarPair(BaseModel): |
| 10 | + name: str |
| 11 | + value: str |
| 12 | + |
| 13 | + |
| 14 | +class WorkspaceCreate(BaseModel): |
| 15 | + teamId: int |
| 16 | + name: str |
| 17 | + planId: int |
| 18 | + baseImage: Optional[str] = None |
| 19 | + isPrivateRepo: bool = True |
| 20 | + replicas: int = 1 |
| 21 | + gitUrl: Optional[str] = None |
| 22 | + initialBranch: Optional[str] = None |
| 23 | + cloneDepth: Optional[int] = None |
| 24 | + sourceWorkspaceId: Optional[int] = None |
| 25 | + welcomeMessage: Optional[str] = None |
| 26 | + vpnConfig: Optional[str] = None |
| 27 | + restricted: Optional[bool] = None |
| 28 | + env: Optional[List[EnvVarPair]] = None |
| 29 | + |
| 30 | + |
| 31 | +# Defines the request body for PATCH /workspaces/{workspaceId} |
| 32 | +class WorkspaceUpdate(BaseModel): |
| 33 | + planId: Optional[int] = None |
| 34 | + baseImage: Optional[str] = None |
| 35 | + name: Optional[str] = None |
| 36 | + replicas: Optional[int] = None |
| 37 | + vpnConfig: Optional[str] = None |
| 38 | + restricted: Optional[bool] = None |
| 39 | + |
| 40 | + |
| 41 | +# Defines the response from GET /workspaces/{workspaceId}/status |
| 42 | +class WorkspaceStatus(BaseModel): |
| 43 | + isRunning: bool |
| 44 | + |
| 45 | + |
| 46 | +# This is the main model for a workspace, returned by GET, POST, and LIST |
| 47 | +class Workspace(BaseModel): |
| 48 | + _http_client: Optional[APIHttpClient] = PrivateAttr(default=None) |
| 49 | + |
| 50 | + id: int |
| 51 | + teamId: int |
| 52 | + name: str |
| 53 | + planId: int |
| 54 | + isPrivateRepo: bool |
| 55 | + replicas: int |
| 56 | + baseImage: Optional[str] = None |
| 57 | + dataCenterId: int |
| 58 | + userId: int |
| 59 | + gitUrl: Optional[str] = None |
| 60 | + initialBranch: Optional[str] = None |
| 61 | + sourceWorkspaceId: Optional[int] = None |
| 62 | + welcomeMessage: Optional[str] = None |
| 63 | + vpnConfig: Optional[str] = None |
| 64 | + restricted: bool |
| 65 | + |
| 66 | + async def update(self, data: WorkspaceUpdate) -> None: |
| 67 | + """Updates this workspace with new data.""" |
| 68 | + if not self._http_client: |
| 69 | + raise RuntimeError("Cannot make API calls on a detached model.") |
| 70 | + |
| 71 | + await self._http_client.patch( |
| 72 | + f"/workspaces/{self.id}", json=data.model_dump(exclude_unset=True) |
| 73 | + ) |
| 74 | + # Optionally, update the local object's state |
| 75 | + for key, value in data.model_dump(exclude_unset=True).items(): |
| 76 | + setattr(self, key, value) |
| 77 | + |
| 78 | + async def delete(self) -> None: |
| 79 | + """Deletes this workspace.""" |
| 80 | + if not self._http_client: |
| 81 | + raise RuntimeError("Cannot make API calls on a detached model.") |
| 82 | + await self._http_client.delete(f"/workspaces/{self.id}") |
| 83 | + |
| 84 | + async def get_status(self) -> WorkspaceStatus: |
| 85 | + """Gets the running status of this workspace.""" |
| 86 | + if not self._http_client: |
| 87 | + raise RuntimeError("Cannot make API calls on a detached model.") |
| 88 | + |
| 89 | + response = await self._http_client.get(f"/workspaces/{self.id}/status") |
| 90 | + return WorkspaceStatus.model_validate(response.json()) |
0 commit comments