Skip to content

Commit fcefbdc

Browse files
authored
Merge pull request #702 from atlanhq/APP-8170
APP-8170 Add optional parameters to AtlanClient.from_token_guid method
2 parents 6875532 + 5e17bd0 commit fcefbdc

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

pyatlan/client/atlan.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ def dq_template_config_cache(self) -> DQTemplateConfigCache:
360360
return self._dq_template_config_cache
361361

362362
@classmethod
363-
def from_token_guid(cls, guid: str) -> AtlanClient:
363+
def from_token_guid(
364+
cls,
365+
guid: str,
366+
base_url: Optional[str] = None,
367+
client_id: Optional[str] = None,
368+
client_secret: Optional[str] = None,
369+
) -> AtlanClient:
364370
"""
365371
Create an AtlanClient instance using an API token GUID.
366372
@@ -371,15 +377,20 @@ def from_token_guid(cls, guid: str) -> AtlanClient:
371377
4. Returns a new AtlanClient authenticated with the resolved token
372378
373379
:param guid: API token GUID to resolve
380+
:param base_url: Optional base URL for the Atlan service(overrides ATLAN_BASE_URL environment variable)
381+
:param client_id: Optional client ID for authentication (overrides CLIENT_ID environment variable)
382+
:param client_secret: Optional client secret for authentication (overrides CLIENT_SECRET environment variable)
374383
:returns: a new client instance authenticated with the resolved token
375384
:raises: ErrorCode.UNABLE_TO_ESCALATE_WITH_PARAM: If any step in the token resolution fails
376385
"""
377-
base_url = os.environ.get("ATLAN_BASE_URL", "INTERNAL")
386+
final_base_url = base_url or os.environ.get("ATLAN_BASE_URL", "INTERNAL")
378387

379388
# Step 1: Initialize base client and get Atlan-Argo credentials
380389
# Note: Using empty api_key as we're bootstrapping authentication
381-
client = AtlanClient(base_url=base_url, api_key="")
382-
client_info = client.impersonate._get_client_info()
390+
client = AtlanClient(base_url=final_base_url, api_key="")
391+
client_info = client.impersonate._get_client_info(
392+
client_id=client_id, client_secret=client_secret
393+
)
383394

384395
# Prepare credentials for Atlan-Argo token request
385396
argo_credentials = {
@@ -393,7 +404,7 @@ def from_token_guid(cls, guid: str) -> AtlanClient:
393404
try:
394405
raw_json = client._call_api(GET_TOKEN, request_obj=argo_credentials)
395406
argo_token = AccessTokenResponse(**raw_json).access_token
396-
temp_argo_client = AtlanClient(base_url=base_url, api_key=argo_token)
407+
temp_argo_client = AtlanClient(base_url=final_base_url, api_key=argo_token)
397408
except AtlanError as atlan_err:
398409
raise ErrorCode.UNABLE_TO_ESCALATE_WITH_PARAM.exception_with_parameters(
399410
"Failed to obtain Atlan-Argo token"
@@ -419,7 +430,7 @@ def from_token_guid(cls, guid: str) -> AtlanClient:
419430
token_api_key = AccessTokenResponse(**raw_json).access_token
420431

421432
# Step 5: Create and return the authenticated client
422-
return AtlanClient(base_url=base_url, api_key=token_api_key)
433+
return AtlanClient(base_url=final_base_url, api_key=token_api_key)
423434
except AtlanError as atlan_err:
424435
raise ErrorCode.UNABLE_TO_ESCALATE_WITH_PARAM.exception_with_parameters(
425436
"Failed to obtain access token for API token"

pyatlan/client/impersonate.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,16 @@ def user(self, user_id: str) -> str:
6666
except AtlanError as atlan_err:
6767
raise ErrorCode.UNABLE_TO_IMPERSONATE.exception_with_parameters() from atlan_err
6868

69-
def _get_client_info(self) -> ClientInfo:
70-
client_id = os.getenv("CLIENT_ID")
71-
client_secret = os.getenv("CLIENT_SECRET")
72-
if not client_id or not client_secret:
69+
def _get_client_info(
70+
self, client_id: Optional[str] = None, client_secret: Optional[str] = None
71+
) -> ClientInfo:
72+
final_client_id = client_id or os.getenv("CLIENT_ID")
73+
final_client_secret = client_secret or os.getenv("CLIENT_SECRET")
74+
if not final_client_id or not final_client_secret:
7375
raise ErrorCode.MISSING_CREDENTIALS.exception_with_parameters()
74-
client_info = ClientInfo(client_id=client_id, client_secret=client_secret)
76+
client_info = ClientInfo(
77+
client_id=final_client_id, client_secret=final_client_secret
78+
)
7579
return client_info
7680

7781
def escalate(self) -> str:

0 commit comments

Comments
 (0)