Skip to content

feat(manager): add api versioning to manager #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion singlestoredb/management/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion singlestoredb/management/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 31 additions & 5 deletions singlestoredb/management/manager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion singlestoredb/management/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion singlestoredb/management/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down