|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | """The API client implementation."""
|
3 |
| - |
| 3 | +from .error import NetworkError |
4 | 4 | from .http import HttpClient
|
5 | 5 | from .jobs import Jobs
|
6 | 6 | from .models import Models
|
7 | 7 | from .results import Results
|
8 | 8 | from .tags import Tags
|
| 9 | +import logging |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class ApiClient:
|
@@ -38,17 +39,37 @@ def __init__(self, base_url, api_key, cert=None):
|
38 | 39 | api_key (str): The API key to use for authentication.
|
39 | 40 | certs (str): A tuple to use custom cert and key, i.e.: (cert_file_path, key_file_path)
|
40 | 41 | """
|
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__) |
45 | 43 | self.base_url = base_url
|
46 | 44 | self.api_key = api_key
|
47 | 45 | self.cert = cert
|
48 | 46 |
|
49 | 47 | self.http = HttpClient(self)
|
50 |
| - |
| 48 | + self.check_client() |
| 49 | + |
51 | 50 | self.models = Models(self)
|
52 | 51 | self.jobs = Jobs(self)
|
53 | 52 | self.results = Results(self)
|
54 | 53 | 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