Spaces:
Runtime error
Runtime error
File size: 7,713 Bytes
105b369 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
from typing import List, Optional, Dict, Union
from httpx import Response
from phi.api.api import api, invalid_response
from phi.api.routes import ApiRoutes
from phi.api.schemas.user import UserSchema
from phi.api.schemas.workspace import (
WorkspaceSchema,
WorkspaceCreate,
WorkspaceUpdate,
WorkspaceDelete,
WorkspaceEvent,
UpdatePrimaryWorkspace,
)
from phi.cli.settings import phi_cli_settings
from phi.utils.log import logger
def get_primary_workspace(user: UserSchema) -> Optional[WorkspaceSchema]:
if not phi_cli_settings.api_enabled:
return None
logger.debug("--o-o-- Get primary workspace")
with api.AuthenticatedClient() as api_client:
try:
r: Response = api_client.post(
ApiRoutes.WORKSPACE_READ_PRIMARY, json=user.model_dump(include={"id_user", "email"})
)
if invalid_response(r):
return None
response_json: Union[Dict, List] = r.json()
if response_json is None:
return None
primary_workspace: WorkspaceSchema = WorkspaceSchema.model_validate(response_json)
if primary_workspace is not None:
return primary_workspace
except Exception as e:
logger.debug(f"Could not get primary workspace: {e}")
return None
def get_available_workspaces(user: UserSchema) -> Optional[List[WorkspaceSchema]]:
if not phi_cli_settings.api_enabled:
return None
logger.debug("--o-o-- Get available workspaces")
with api.AuthenticatedClient() as api_client:
try:
r: Response = api_client.post(
ApiRoutes.WORKSPACE_READ_AVAILABLE, json=user.model_dump(include={"id_user", "email"})
)
if invalid_response(r):
return None
response_json: Union[Dict, List] = r.json()
if response_json is None:
return None
available_workspaces: List[WorkspaceSchema] = []
for workspace in response_json:
if not isinstance(workspace, dict):
logger.debug(f"Not a dict: {workspace}")
continue
available_workspaces.append(WorkspaceSchema.model_validate(workspace))
return available_workspaces
except Exception as e:
logger.debug(f"Could not get available workspaces: {e}")
return None
def create_workspace_for_user(user: UserSchema, workspace: WorkspaceCreate) -> Optional[WorkspaceSchema]:
if not phi_cli_settings.api_enabled:
return None
logger.debug("--o-o-- Create workspace")
with api.AuthenticatedClient() as api_client:
try:
r: Response = api_client.post(
ApiRoutes.WORKSPACE_CREATE,
json={
"user": user.model_dump(include={"id_user", "email"}),
"workspace": workspace.model_dump(exclude_none=True),
},
)
if invalid_response(r):
return None
response_json: Union[Dict, List] = r.json()
if response_json is None:
return None
created_workspace: WorkspaceSchema = WorkspaceSchema.model_validate(response_json)
if created_workspace is not None:
return created_workspace
except Exception as e:
logger.debug(f"Could not create workspace: {e}")
return None
def update_workspace_for_user(user: UserSchema, workspace: WorkspaceUpdate) -> Optional[WorkspaceSchema]:
if not phi_cli_settings.api_enabled:
return None
logger.debug("--o-o-- Update workspace")
with api.AuthenticatedClient() as api_client:
try:
r: Response = api_client.post(
ApiRoutes.WORKSPACE_UPDATE,
json={
"user": user.model_dump(include={"id_user", "email"}),
"workspace": workspace.model_dump(exclude_none=True),
},
)
if invalid_response(r):
return None
response_json: Union[Dict, List] = r.json()
if response_json is None:
return None
updated_workspace: WorkspaceSchema = WorkspaceSchema.model_validate(response_json)
if updated_workspace is not None:
return updated_workspace
except Exception as e:
logger.debug(f"Could not update workspace: {e}")
return None
def update_primary_workspace_for_user(user: UserSchema, workspace: UpdatePrimaryWorkspace) -> Optional[WorkspaceSchema]:
if not phi_cli_settings.api_enabled:
return None
logger.debug(f"--o-o-- Update primary workspace to: {workspace.ws_name}")
with api.AuthenticatedClient() as api_client:
try:
r: Response = api_client.post(
ApiRoutes.WORKSPACE_UPDATE_PRIMARY,
json={
"user": user.model_dump(include={"id_user", "email"}),
"workspace": workspace.model_dump(exclude_none=True),
},
)
if invalid_response(r):
return None
response_json: Union[Dict, List] = r.json()
if response_json is None:
return None
updated_workspace: WorkspaceSchema = WorkspaceSchema.model_validate(response_json)
if updated_workspace is not None:
return updated_workspace
except Exception as e:
logger.debug(f"Could not update primary workspace: {e}")
return None
def delete_workspace_for_user(user: UserSchema, workspace: WorkspaceDelete) -> Optional[WorkspaceSchema]:
if not phi_cli_settings.api_enabled:
return None
logger.debug("--o-o-- Delete workspace")
with api.AuthenticatedClient() as api_client:
try:
r: Response = api_client.post(
ApiRoutes.WORKSPACE_DELETE,
json={
"user": user.model_dump(include={"id_user", "email"}),
"workspace": workspace.model_dump(exclude_none=True),
},
)
if invalid_response(r):
return None
response_json: Union[Dict, List] = r.json()
if response_json is None:
return None
updated_workspace: WorkspaceSchema = WorkspaceSchema.model_validate(response_json)
if updated_workspace is not None:
return updated_workspace
except Exception as e:
logger.debug(f"Could not delete workspace: {e}")
return None
def log_workspace_event(user: UserSchema, workspace_event: WorkspaceEvent) -> bool:
if not phi_cli_settings.api_enabled:
return False
logger.debug("--o-o-- Log workspace event")
with api.AuthenticatedClient() as api_client:
try:
r: Response = api_client.post(
ApiRoutes.WORKSPACE_EVENT_CREATE,
json={
"user": user.model_dump(include={"id_user", "email"}),
"event": workspace_event.model_dump(exclude_none=True),
},
)
if invalid_response(r):
return False
response_json: Union[Dict, List] = r.json()
if response_json is None:
return False
if isinstance(response_json, dict) and response_json.get("status") == "success":
return True
return False
except Exception as e:
logger.debug(f"Could not log workspace event: {e}")
return False
|