Skip to content

Commit b743906

Browse files
kesmit13claude
andcommitted
refactor: extract auth token logic into private method
Extract repeated auth token parsing code from manager property methods into a reusable private method `_get_auth_token()`. This eliminates code duplication across all 6 manager properties (teams, private_connections, audit_logs, users, metrics, storage_dr). Benefits: - Reduces code duplication by ~30 lines - Improves maintainability - Consistent token handling across all managers - Easier to modify auth logic in the future 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a7881a4 commit b743906

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

singlestoredb/management/workspace.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,17 +2055,20 @@ def delete_starter_workspace_user(
20552055
f'sharedtier/virtualWorkspaces/{virtual_workspace_id}/users/{user_id}',
20562056
)
20572057

2058+
def _get_auth_token(self) -> Optional[str]:
2059+
"""Extract and decode the authorization token from session headers."""
2060+
auth_header = self._sess.headers.get('Authorization', '')
2061+
if isinstance(auth_header, bytes):
2062+
auth_header = auth_header.decode('utf-8')
2063+
return auth_header.replace('Bearer ', '') if auth_header else None
2064+
20582065
# Add properties for new managers
20592066
@property
20602067
def teams(self) -> 'TeamsManager':
20612068
"""Return the teams manager."""
20622069
from .teams import TeamsManager
2063-
auth_header = self._sess.headers.get('Authorization', '')
2064-
if isinstance(auth_header, bytes):
2065-
auth_header = auth_header.decode('utf-8')
2066-
token = auth_header.replace('Bearer ', '') if auth_header else None
20672070
return TeamsManager(
2068-
access_token=token,
2071+
access_token=self._get_auth_token(),
20692072
base_url=self._base_url.rstrip('/v1/'),
20702073
version='v1',
20712074
organization_id=self._params.get('organizationID'),
@@ -2075,12 +2078,8 @@ def teams(self) -> 'TeamsManager':
20752078
def private_connections(self) -> 'PrivateConnectionsManager':
20762079
"""Return the private connections manager."""
20772080
from .private_connections import PrivateConnectionsManager
2078-
auth_header = self._sess.headers.get('Authorization', '')
2079-
if isinstance(auth_header, bytes):
2080-
auth_header = auth_header.decode('utf-8')
2081-
token = auth_header.replace('Bearer ', '') if auth_header else None
20822081
return PrivateConnectionsManager(
2083-
access_token=token,
2082+
access_token=self._get_auth_token(),
20842083
base_url=self._base_url.rstrip('/v1/'),
20852084
version='v1',
20862085
organization_id=self._params.get('organizationID'),
@@ -2090,12 +2089,8 @@ def private_connections(self) -> 'PrivateConnectionsManager':
20902089
def audit_logs(self) -> 'AuditLogsManager':
20912090
"""Return the audit logs manager."""
20922091
from .audit_logs import AuditLogsManager
2093-
auth_header = self._sess.headers.get('Authorization', '')
2094-
if isinstance(auth_header, bytes):
2095-
auth_header = auth_header.decode('utf-8')
2096-
token = auth_header.replace('Bearer ', '') if auth_header else None
20972092
return AuditLogsManager(
2098-
access_token=token,
2093+
access_token=self._get_auth_token(),
20992094
base_url=self._base_url.rstrip('/v1/'),
21002095
version='v1',
21012096
organization_id=self._params.get('organizationID'),
@@ -2105,12 +2100,8 @@ def audit_logs(self) -> 'AuditLogsManager':
21052100
def users(self) -> 'UsersManager':
21062101
"""Return the users manager."""
21072102
from .users import UsersManager
2108-
auth_header = self._sess.headers.get('Authorization', '')
2109-
if isinstance(auth_header, bytes):
2110-
auth_header = auth_header.decode('utf-8')
2111-
token = auth_header.replace('Bearer ', '') if auth_header else None
21122103
return UsersManager(
2113-
access_token=token,
2104+
access_token=self._get_auth_token(),
21142105
base_url=self._base_url.rstrip('/v1/'),
21152106
version='v1',
21162107
organization_id=self._params.get('organizationID'),
@@ -2120,12 +2111,8 @@ def users(self) -> 'UsersManager':
21202111
def metrics(self) -> 'MetricsManager':
21212112
"""Return the metrics manager."""
21222113
from .metrics import MetricsManager
2123-
auth_header = self._sess.headers.get('Authorization', '')
2124-
if isinstance(auth_header, bytes):
2125-
auth_header = auth_header.decode('utf-8')
2126-
token = auth_header.replace('Bearer ', '') if auth_header else None
21272114
return MetricsManager(
2128-
access_token=token,
2115+
access_token=self._get_auth_token(),
21292116
base_url=self._base_url.rstrip('/v1/'),
21302117
version='v2', # Metrics use v2 API
21312118
organization_id=self._params.get('organizationID'),
@@ -2135,12 +2122,8 @@ def metrics(self) -> 'MetricsManager':
21352122
def storage_dr(self) -> 'StorageDRManager':
21362123
"""Return the storage DR manager."""
21372124
from .storage_dr import StorageDRManager
2138-
auth_header = self._sess.headers.get('Authorization', '')
2139-
if isinstance(auth_header, bytes):
2140-
auth_header = auth_header.decode('utf-8')
2141-
token = auth_header.replace('Bearer ', '') if auth_header else None
21422125
return StorageDRManager(
2143-
access_token=token,
2126+
access_token=self._get_auth_token(),
21442127
base_url=self._base_url.rstrip('/v1/'),
21452128
version='v1',
21462129
organization_id=self._params.get('organizationID'),

0 commit comments

Comments
 (0)