From a8531f61d62168d0249a0a3bace7f6452f459719 Mon Sep 17 00:00:00 2001 From: Naymul Islam <68547750+ai-naymul@users.noreply.github.com> Date: Mon, 18 Sep 2023 22:06:35 +0600 Subject: [PATCH] improve the get_uri function with better error handling and code organization (#58) error handling in the get_uri function is improved and hardcoded constants is refactored --- vectordb/utils/push_to_hubble.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/vectordb/utils/push_to_hubble.py b/vectordb/utils/push_to_hubble.py index eb415bc..41addd0 100644 --- a/vectordb/utils/push_to_hubble.py +++ b/vectordb/utils/push_to_hubble.py @@ -11,7 +11,7 @@ __resources_path__ = os.path.join( Path(os.path.dirname(sys.modules['vectordb'].__file__)).parent.absolute(), 'resources' ) - +API_URL = "https://apihubble.jina.ai/v2/executor/getMeta?id={id}&tag={tag}" class EnvironmentVarCtxtManager: """a class to wrap env vars""" @@ -44,18 +44,20 @@ def get_uri(id: str, tag: str): import requests from hubble import Auth - r = requests.get( - f"https://apihubble.jina.ai/v2/executor/getMeta?id={id}&tag={tag}", - headers={"Authorization": f"token {Auth.get_auth_token()}"}, - ) - _json = r.json() - if _json is None: - print(f'Could not find image with id {id} and tag {tag}') - return - _image_name = _json['data']['name'] - _user_name = _json['meta']['owner']['name'] - return f'jinaai+docker://{_user_name}/{_image_name}:{tag}' + headers = {"Authorization": f"token {Auth.get_auth_token()}"} + response = requests.get(API_URL.format(id=id, tag=tag), headers=headers) + + if response.status_code != 200: + raise Exception(f"Request failed with status code {response.status_code}") + + response_json = response.json() + + if response_json is None: + raise Exception(f'Could not find image with id {id} and tag {tag}') + image_name = response_json['data']['name'] + user_name = response_json['meta']['owner']['name'] + return f'jinaai+docker://{user_name}/{image_name}:{tag}' def get_random_tag(): return 't-' + uuid.uuid4().hex[:5]