Skip to content

Commit 47c4e23

Browse files
feat: adding an initial validation to the base_url provided in order to try with or without the api part (#32)
1 parent 6421834 commit 47c4e23

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

modzy/client.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# -*- coding: utf-8 -*-
22
"""The API client implementation."""
3-
3+
from .error import NetworkError
44
from .http import HttpClient
55
from .jobs import Jobs
66
from .models import Models
77
from .results import Results
88
from .tags import Tags
9+
import logging
910

1011

1112
class ApiClient:
@@ -38,17 +39,37 @@ def __init__(self, base_url, api_key, cert=None):
3839
api_key (str): The API key to use for authentication.
3940
certs (str): A tuple to use custom cert and key, i.e.: (cert_file_path, key_file_path)
4041
"""
41-
if base_url is None or base_url == "":
42-
raise ValueError("Cannot initialize the modzy client: the base_url param should be a valid not empty string")
43-
if api_key is None or api_key == "":
44-
raise ValueError("Cannot initialize the modzy client: the api_key param should be a valid not empty string")
42+
self.logger = logging.getLogger(__name__)
4543
self.base_url = base_url
4644
self.api_key = api_key
4745
self.cert = cert
4846

4947
self.http = HttpClient(self)
50-
48+
self.check_client()
49+
5150
self.models = Models(self)
5251
self.jobs = Jobs(self)
5352
self.results = Results(self)
5453
self.tags = Tags(self)
54+
55+
def check_client(self):
56+
self.logger.debug("Checking base_url %s", self.base_url)
57+
if self.base_url is None or self.base_url == "":
58+
raise ValueError("Cannot initialize the modzy client: the base_url param should be a valid not empty string")
59+
if self.api_key is None or self.api_key == "":
60+
raise ValueError("Cannot initialize the modzy client: the api_key param should be a valid not empty string")
61+
req_check = False
62+
try:
63+
self.http.get('/models')
64+
req_check = True
65+
except Exception as e:
66+
if not self.base_url.endswith('api') and not self.base_url.endswith('api/'):
67+
self.base_url = self.base_url + ("" if self.base_url.endswith("/") else "/") + "api/"
68+
# Try again with the new URL
69+
self.check_client()
70+
req_check = True
71+
72+
if not req_check:
73+
raise ValueError("Cannot initialize the modzy client: the base_url param should point to a valid API "
74+
"endpoint and the api_key should be a valid key for the env")
75+

0 commit comments

Comments
 (0)