Skip to content

Commit

Permalink
Fix: create api key when phi ws create is invoked
Browse files Browse the repository at this point in the history
  • Loading branch information
manthanguptaa committed Sep 27, 2024
1 parent 157d9e3 commit 5c4740a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions phi/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
PHI_RUNTIME_ENV_VAR: str = "PHI_RUNTIME"
PHI_API_KEY_ENV_VAR: str = "PHI_API_KEY"
PHI_WS_KEY_ENV_VAR: str = "PHI_WS_KEY"
PHI_API_ENDPOINT_ENV_VAR: str = "PHI_API_ENDPOINT"

SCRIPTS_DIR_ENV_VAR: str = "PHI_SCRIPTS_DIR"
STORAGE_DIR_ENV_VAR: str = "PHI_STORAGE_DIR"
Expand Down
21 changes: 21 additions & 0 deletions phi/workspace/operator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from os import getenv
from pathlib import Path
from typing import Optional, Dict, List

import requests

from phi.api.workspace import log_workspace_event
from phi.api.schemas.workspace import (
Expand All @@ -17,6 +19,8 @@
print_subheading,
log_config_not_available_msg,
)
from phi.cli.credentials import read_auth_token
from phi.constants import PHI_API_ENDPOINT_ENV_VAR
from phi.infra.type import InfraType
from phi.infra.resources import InfraResources
from phi.workspace.config import WorkspaceConfig
Expand Down Expand Up @@ -344,6 +348,23 @@ def setup_workspace(ws_root_path: Path, team: Optional[str] = None) -> bool:
else:
logger.debug("Failed to sync workspace with api. Please setup again")

######################################################
# 2.3 Create API Key for User Workspace
######################################################
if ws_schema:
url = getenv(PHI_API_ENDPOINT_ENV_VAR, "") + "v1/user-keys/refresh"
headers = {
"X-PHIDATA-AUTH-TOKEN": read_auth_token(),
}
params = {"workspace_url": ws_schema.ws_name}
data = {"user": {"id_user": phi_config.user.id_user, "email": phi_config.user.email}}
response = requests.post(url, params=params, json=data, headers=headers)
logger.info(response.json())
if response.status_code == 200:
print_info(f"New API Key created for workspace: {ws_schema.ws_name}")
else:
logger.error(f"Failed to create API Key for workspace: {ws_schema.ws_name}")

if ws_config is not None:
# logger.debug("Workspace Config: {}".format(ws_config.model_dump_json(indent=2)))
print_subheading("Setup complete! Next steps:")
Expand Down

0 comments on commit 5c4740a

Please sign in to comment.