diff --git a/singlestoredb/management/cluster.py b/singlestoredb/management/cluster.py index 35428abd9..89324c4af 100644 --- a/singlestoredb/management/cluster.py +++ b/singlestoredb/management/cluster.py @@ -440,7 +440,7 @@ def manage_cluster( access_token : str, optional The API key or other access token for the cluster management API version : str, optional - Version of the API to use + Version of the API to use (default: 'v1') base_url : str, optional Base URL of the cluster management API organization_id: str, optional diff --git a/singlestoredb/management/files.py b/singlestoredb/management/files.py index ac3561ad7..330c077ca 100644 --- a/singlestoredb/management/files.py +++ b/singlestoredb/management/files.py @@ -534,7 +534,7 @@ def manage_files( access_token : str, optional The API key or other access token for the files management API version : str, optional - Version of the API to use + Version of the API to use (default: 'v1') base_url : str, optional Base URL of the files management API organization_id : str, optional diff --git a/singlestoredb/management/manager.py b/singlestoredb/management/manager.py index 9474360a8..4f8bb85c2 100644 --- a/singlestoredb/management/manager.py +++ b/singlestoredb/management/manager.py @@ -1,8 +1,10 @@ #!/usr/bin/env python """SingleStoreDB Base Manager.""" import os +import re import sys import time +from copy import deepcopy from typing import Any from typing import Dict from typing import List @@ -63,6 +65,8 @@ def __init__( if not new_access_token: raise ManagementError(msg='No management token was configured.') + self.version = version or self.default_version + self._is_jwt = not access_token and new_access_token and is_jwt(new_access_token) self._sess = requests.Session() self._sess.headers.update({ @@ -72,17 +76,35 @@ def __init__( 'User-Agent': f'SingleStoreDB-Python/{client_version}', }) - self._base_url = urljoin( + self._base_url = ( base_url or config.get_option('management.base_url') - or type(self).default_base_url, - version or type(self).default_version, + or type(self).default_base_url ) + '/' - + self._access_token = new_access_token self._params: Dict[str, str] = {} if organization_id: self._params['organizationID'] = organization_id + def copy(self) -> 'Manager': + """Create a new instance with the same settings.""" + new_manager = type(self).__new__(type(self)) + new_manager._is_jwt = self._is_jwt + new_manager._sess = deepcopy(self._sess) + new_manager._base_url = self._base_url + new_manager.version = self.version + new_manager._access_token = self._access_token + new_manager._params = deepcopy(self._params) + return new_manager + + def __getattr__(self, name: str) -> Any: + """Handle dynamic version attributes (v2, v3, etc.).""" + if re.match(r'^v\d+[0-9a-z]*$', name): + new_mgr = self.copy() + new_mgr.version = name + return new_mgr + return super().__getattribute__(name) + def _check( self, res: requests.Response, url: str, params: Dict[str, Any], ) -> requests.Response: @@ -125,8 +147,12 @@ def _doit( # Refresh the JWT as needed if self._is_jwt: self._sess.headers.update({'Authorization': f'Bearer {get_token()}'}) + + # Combine version and path + versioned_path = f'{self.version}/{path}' + return getattr(self._sess, method.lower())( - urljoin(self._base_url, path), *args, **kwargs, + urljoin(self._base_url, versioned_path), *args, **kwargs, ) def _get(self, path: str, *args: Any, **kwargs: Any) -> requests.Response: diff --git a/singlestoredb/management/region.py b/singlestoredb/management/region.py index 546070e4b..84b206a1e 100644 --- a/singlestoredb/management/region.py +++ b/singlestoredb/management/region.py @@ -143,7 +143,7 @@ def manage_regions( access_token : str, optional The API key or other access token for the workspace management API version : str, optional - Version of the API to use + Version of the API to use (default: 'v1') base_url : str, optional Base URL of the workspace management API diff --git a/singlestoredb/management/workspace.py b/singlestoredb/management/workspace.py index 7b3c08cc2..e16d6ea12 100644 --- a/singlestoredb/management/workspace.py +++ b/singlestoredb/management/workspace.py @@ -1978,7 +1978,7 @@ def manage_workspaces( access_token : str, optional The API key or other access token for the workspace management API version : str, optional - Version of the API to use + Version of the API to use (default is 'v1') base_url : str, optional Base URL of the workspace management API organization_id : str, optional