@@ -56,20 +56,25 @@ def auth_header(self) -> str:
5656
5757
5858class LegacyOAuth2AuthManager (AuthManager ):
59+ _session : Session
60+ _auth_url : Optional [str ]
61+ _token : Optional [str ]
62+ _credential : Optional [str ]
63+ _optional_oauth_params : Optional [Dict [str , str ]]
64+
5965 def __init__ (
6066 self ,
6167 session : Session ,
62- auth_url : str ,
68+ auth_url : Optional [ str ] = None ,
6369 credential : Optional [str ] = None ,
6470 initial_token : Optional [str ] = None ,
6571 optional_oauth_params : Optional [Dict [str , str ]] = None ,
6672 ):
67- self ._token : Optional [str ] = None
68- self ._initial_token = initial_token
69- self ._credential = credential
73+ self ._session = session
7074 self ._auth_url = auth_url
75+ self ._token = initial_token
76+ self ._credential = credential
7177 self ._optional_oauth_params = optional_oauth_params
72- self ._session = session
7378 self ._refresh_token ()
7479
7580 def _fetch_access_token (self , credential : str ) -> str :
@@ -83,6 +88,9 @@ def _fetch_access_token(self, credential: str) -> str:
8388 if self ._optional_oauth_params :
8489 data .update (self ._optional_oauth_params )
8590
91+ if self ._auth_url is None :
92+ raise ValueError ("Cannot fetch access token from undefined auth_url" )
93+
8694 response = self ._session .post (
8795 url = self ._auth_url , data = data , headers = {** self ._session .headers , "Content-type" : "application/x-www-form-urlencoded" }
8896 )
@@ -94,9 +102,7 @@ def _fetch_access_token(self, credential: str) -> str:
94102 return TokenResponse .model_validate_json (response .text ).access_token
95103
96104 def _refresh_token (self ) -> None :
97- if self ._initial_token is not None :
98- self ._token = self ._initial_token
99- elif self ._credential :
105+ if self ._credential is not None :
100106 self ._token = self ._fetch_access_token (self ._credential )
101107
102108 def auth_header (self ) -> str :
@@ -140,14 +146,21 @@ class AuthManagerFactory:
140146 _registry : Dict [str , Type ["AuthManager" ]] = {}
141147
142148 @classmethod
143- def register (cls , name : str , manager_cls : Type ["AuthManager" ]) -> None :
149+ def register (cls , name : str , auth_manager_class : Type ["AuthManager" ]) -> None :
144150 """
145151 Register a string name to a known AuthManager class.
152+
153+ Args:
154+ name (str): unique name like 'oauth2' to register the AuthManager with
155+ auth_manager_class (Type["AuthManager"]): Implementation of AuthManager
156+
157+ Returns:
158+ None
146159 """
147- cls ._registry [name ] = manager_cls
160+ cls ._registry [name ] = auth_manager_class
148161
149162 @classmethod
150- def create (cls , class_or_name : str , config : Dict [str , Any ]) -> " AuthManager" :
163+ def create (cls , class_or_name : str , config : Dict [str , Any ]) -> AuthManager :
151164 """
152165 Create an AuthManager by name or fully-qualified class path.
153166
0 commit comments