33from urllib .parse import urlparse
44
55import httpx
6- from httpx import HTTPStatusError , Request
6+ from httpx import HTTPStatusError
77
88from ..errors import EnrichedException
9- from ._http_config import get_httpx_client_kwargs
9+ from .. identity import IdentityService
1010from .auth import TokenData
1111from .constants import ENV_BASE_URL
1212
@@ -22,16 +22,6 @@ def __init__(self, base_url: Optional[str]):
2222 self ._base_url = resolved_base_url
2323 self ._domain = self ._extract_environment_from_base_url (self ._base_url )
2424
25- def get_token_url (self ) -> str :
26- """Get the token URL for the specified domain."""
27- match self ._domain :
28- case "alpha" :
29- return "https://alpha.uipath.com/identity_/connect/token"
30- case "staging" :
31- return "https://staging.uipath.com/identity_/connect/token"
32- case _: # cloud (default)
33- return "https://cloud.uipath.com/identity_/connect/token"
34-
3525 def _is_valid_domain_or_subdomain (self , hostname : str , domain : str ) -> bool :
3626 """Check if hostname is either an exact match or a valid subdomain of the domain.
3727
@@ -87,53 +77,45 @@ def get_token_data(
8777 Returns:
8878 Token data if successful
8979 """
90- token_url = self .get_token_url ()
91-
92- data = {
93- "grant_type" : "client_credentials" ,
94- "client_id" : client_id ,
95- "client_secret" : client_secret ,
96- "scope" : scope ,
80+ domain_map = {
81+ "alpha" : "https://alpha.uipath.com" ,
82+ "staging" : "https://staging.uipath.com" ,
9783 }
84+ domain = domain_map .get (self ._domain , "https://cloud.uipath.com" )
9885
9986 try :
100- with httpx . Client ( ** get_httpx_client_kwargs ()) as client :
101- response = client . post ( token_url , data = data )
102- match response . status_code :
103- case 200 :
104- return TokenData . model_validate ( response . json ())
105- case 400 :
106- raise EnrichedException (
107- HTTPStatusError (
108- message = "Invalid client credentials or request parameters." ,
109- request = Request (
110- data = data , url = token_url , method = "post"
111- ) ,
112- response = response ,
113- )
87+ return IdentityService . get_client_credentials_token (
88+ domain = domain ,
89+ client_id = client_id ,
90+ client_secret = client_secret ,
91+ scope = scope ,
92+ )
93+ except HTTPStatusError as e :
94+ match e . response . status_code :
95+ case 400 :
96+ raise EnrichedException (
97+ HTTPStatusError (
98+ message = "Invalid client credentials or request parameters." ,
99+ request = e . request ,
100+ response = e . response ,
114101 )
115- case 401 :
116- raise EnrichedException (
117- HTTPStatusError (
118- message = "Unauthorized: Invalid client credentials." ,
119- request = Request (
120- data = data , url = token_url , method = "post"
121- ),
122- response = response ,
123- )
102+ ) from e
103+ case 401 :
104+ raise EnrichedException (
105+ HTTPStatusError (
106+ message = "Unauthorized: Invalid client credentials." ,
107+ request = e .request ,
108+ response = e .response ,
124109 )
125- case _:
126- raise EnrichedException (
127- HTTPStatusError (
128- message = f"Authentication failed with unexpected status: { response .status_code } " ,
129- request = Request (
130- data = data , url = token_url , method = "post"
131- ),
132- response = response ,
133- )
110+ ) from e
111+ case _:
112+ raise EnrichedException (
113+ HTTPStatusError (
114+ message = f"Authentication failed with unexpected status: { e .response .status_code } " ,
115+ request = e .request ,
116+ response = e .response ,
134117 )
135- except EnrichedException :
136- raise
118+ ) from e
137119 except httpx .RequestError as e :
138120 raise Exception (f"Network error during authentication: { e } " ) from e
139121 except Exception as e :
0 commit comments