1- from time import time
2- from typing import Generator , Optional
1+ from typing import Optional
32
4- from httpx import Request , Response
5-
6- from firebolt .client .auth .base import Auth
7- from firebolt .client .constants import _REQUEST_ERRORS
8- from firebolt .utils .exception import AuthenticationError
3+ from firebolt .client .auth .base import AuthRequest
4+ from firebolt .client .auth .request_auth_base import _RequestBasedAuth
95from firebolt .utils .token_storage import TokenSecureStorage
106from firebolt .utils .urls import AUTH_URL
117from firebolt .utils .util import cached_property
128
139
14- class UsernamePassword (Auth ):
10+ class UsernamePassword (_RequestBasedAuth ):
1511 """Username/Password authentication class for Firebolt Database.
1612
1713 Gets authentication token using
@@ -34,6 +30,7 @@ class UsernamePassword(Auth):
3430 "_token" ,
3531 "_expires" ,
3632 "_use_token_cache" ,
33+ "_user_agent" ,
3734 )
3835
3936 requires_response_body = True
@@ -67,7 +64,7 @@ def _token_storage(self) -> Optional[TokenSecureStorage]:
6764 """
6865 return TokenSecureStorage (username = self .username , password = self .password )
6966
70- def get_new_token_generator (self ) -> Generator [ Request , Response , None ] :
67+ def _make_auth_request (self ) -> AuthRequest :
7168 """Get new token using username and password.
7269
7370 Yields:
@@ -76,39 +73,13 @@ def get_new_token_generator(self) -> Generator[Request, Response, None]:
7673 Raises:
7774 AuthenticationError: Error while authenticating with provided credentials
7875 """
79- try :
80- response = yield self .request_class (
81- "POST" ,
82- # The full url is generated on client side by attaching
83- # it to api_endpoint
84- AUTH_URL ,
85- headers = {
86- "Content-Type" : "application/json;charset=UTF-8" ,
87- "User-Agent" : "firebolt-sdk" ,
88- },
89- json = {"username" : self .username , "password" : self .password },
90- )
91- response .raise_for_status ()
92-
93- parsed = response .json ()
94- self ._check_response_error (parsed )
95-
96- self ._token = parsed ["access_token" ]
97- self ._expires = int (time ()) + int (parsed ["expires_in" ])
98-
99- except _REQUEST_ERRORS as e :
100- raise AuthenticationError (repr (e )) from e
101-
102- def _check_response_error (self , response : dict ) -> None :
103- """Check if response data contains errors.
104-
105- Args:
106- response (dict): Response data
107-
108- Raises:
109- AuthenticationError: Were unable to authenticate
110- """
111- if "error" in response :
112- raise AuthenticationError (
113- response .get ("message" , "unknown server error" ),
114- )
76+ response = self .request_class (
77+ "POST" ,
78+ AUTH_URL ,
79+ headers = {
80+ "Content-Type" : "application/json;charset=UTF-8" ,
81+ "User-Agent" : self ._user_agent ,
82+ },
83+ json = {"username" : self .username , "password" : self .password },
84+ )
85+ return response
0 commit comments