Skip to content

Commit

Permalink
Merge pull request #288 from roboflow/api_key_precedence
Browse files Browse the repository at this point in the history
api key precedence
  • Loading branch information
ryanjball authored Jul 16, 2024
2 parents 3750654 + 098d96f commit e135c40
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
16 changes: 10 additions & 6 deletions roboflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from roboflow.models import CLIPModel, GazeModel # noqa: F401
from roboflow.util.general import write_line

__version__ = "1.1.34"
__version__ = "1.1.35"


def check_key(api_key, model, notebook, num_retries=0):
Expand Down Expand Up @@ -238,13 +238,17 @@ def workspace(self, the_workspace=None):
if the_workspace is None:
the_workspace = self.current_workspace

if self.api_key in DEMO_KEYS:
if self.api_key: # Check if api_key was passed during __init__
workspace_api_key = load_roboflow_api_key(the_workspace)
api_key = workspace_api_key or self.api_key
list_projects = rfapi.get_workspace(api_key, the_workspace)
return Workspace(list_projects, api_key, the_workspace, self.model_format)

elif self.api_key in DEMO_KEYS:
return Workspace({}, self.api_key, the_workspace, self.model_format)
workspace_api_key = load_roboflow_api_key(the_workspace)
api_key = workspace_api_key or self.api_key

list_projects = rfapi.get_workspace(api_key, the_workspace)
return Workspace(list_projects, api_key, the_workspace, self.model_format)
else:
raise ValueError("A valid API key must be provided.")

def project(self, project_name, the_workspace=None):
"""Function that takes in the name of the project and returns the project object
Expand Down
13 changes: 9 additions & 4 deletions roboflow/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ def __init__(self, api_key: str, a_project: dict, model_format: Optional[str] =
>>> project = rf.workspace().project("PROJECT_ID")
"""
if api_key in DEMO_KEYS:
self.__api_key = api_key
self.model_format = model_format
else:

if api_key:
self.__api_key = api_key
self.annotation = a_project["annotation"]
self.classes = a_project["classes"]
Expand All @@ -76,6 +74,13 @@ def __init__(self, api_key: str, a_project: dict, model_format: Optional[str] =
self.__workspace = temp[0]
self.__project_name = temp[1]

elif DEMO_KEYS:
self.__api_key = DEMO_KEYS[0]
self.model_format = model_format

else:
raise ValueError("A valid API key must be provided.")

def get_version_information(self):
"""
Retrieve all versions of a project.
Expand Down
28 changes: 15 additions & 13 deletions roboflow/core/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,7 @@ def __init__(
"""
Initialize a Version object.
"""
if api_key in DEMO_KEYS:
if api_key == "coco-128-sample":
self.__api_key = api_key
self.model_format = model_format
self.name = "coco-128"
self.version = "1"
else:
self.__api_key = api_key
self.model_format = model_format
self.name = "chess-pieces-new"
self.version = "23"
self.id = "joseph-nelson/chess-pieces-new"
else:
if api_key:
self.__api_key = api_key
self.name = name
self.version = unwrap_version_id(version_id=version)
Expand Down Expand Up @@ -145,6 +133,20 @@ def __init__(
else:
self.model = None

elif DEMO_KEYS:
api_key = DEMO_KEYS[0]
if api_key == "coco-128-sample":
self.__api_key = api_key
self.model_format = model_format
self.name = "coco-128"
self.version = "1"
else:
self.__api_key = api_key
self.model_format = model_format
self.name = "chess-pieces-new"
self.version = "23"
self.id = "joseph-nelson/chess-pieces-new"

def __check_if_generating(self):
# check Roboflow API to see if this version is still generating

Expand Down
14 changes: 9 additions & 5 deletions roboflow/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ class Workspace:
"""

def __init__(self, info, api_key, default_workspace, model_format):
if api_key in DEMO_KEYS:
if api_key:
self.__api_key = api_key
self.model_format = model_format
self.project_list = []
else:

workspace_info = info["workspace"]
self.name = workspace_info["name"]
self.project_list = workspace_info["projects"]
Expand All @@ -38,7 +36,13 @@ def __init__(self, info, api_key, default_workspace, model_format):
self.url = workspace_info["url"]
self.model_format = model_format

self.__api_key = api_key
elif DEMO_KEYS:
self.__api_key = DEMO_KEYS[0]
self.model_format = model_format
self.project_list = []

else:
raise ValueError("A valid API key must be provided.")

def list_projects(self):
"""
Expand Down
42 changes: 42 additions & 0 deletions roboflow/roboflowpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ def get_workspace(args):
print(json.dumps(workspace_json, indent=2))


def get_workspace_project_version(args):
# api_key = load_roboflow_api_key(args.workspaceId)
rf = roboflow.Roboflow(args.api_key)
workspace = rf.workspace()
print("workspace", workspace)
project = workspace.project(args.project)
print("project", project)
version = project.version(args.version_number)
print("version", version)


def get_project(args):
workspace_url = args.workspace or get_conditional_configuration_variable("RF_WORKSPACE", default=None)
api_key = load_roboflow_api_key(workspace_url)
Expand Down Expand Up @@ -161,6 +172,8 @@ def _argparser():
_add_projects_parser(subparsers)
_add_workspaces_parser(subparsers)
_add_upload_model_parser(subparsers)
_add_get_workspace_project_version_parser(subparsers)

return parser


Expand Down Expand Up @@ -411,6 +424,35 @@ def _add_upload_model_parser(subparsers):
upload_model_parser.set_defaults(func=upload_model)


def _add_get_workspace_project_version_parser(subparsers):
workspace_project_version_parser = subparsers.add_parser(
"get_workspace_info",
help="get workspace project version info",
)
workspace_project_version_parser.add_argument(
"-a",
dest="api_key",
help="api_key",
)
workspace_project_version_parser.add_argument(
"-w",
dest="workspace",
help="specify a workspace url or id (will use default workspace if not specified)",
)
workspace_project_version_parser.add_argument(
"-p",
dest="project",
help="project_id to upload the model into",
)
workspace_project_version_parser.add_argument(
"-v",
dest="version_number",
type=int,
help="version number to upload the model to",
)
workspace_project_version_parser.set_defaults(func=get_workspace_project_version)


def _add_login_parser(subparsers):
login_parser = subparsers.add_parser("login", help="Log in to Roboflow")
login_parser.set_defaults(func=login)
Expand Down

0 comments on commit e135c40

Please sign in to comment.