1717import base64
1818import os
1919import socket
20+ from enum import Enum
2021from typing import Optional
2122from urllib import parse
2223
2627from selenium .webdriver .common .proxy import ProxyType
2728
2829
30+ class AuthType (Enum ):
31+ BASIC = "Basic"
32+ BEARER = "Bearer"
33+ X_API_KEY = "X-API-Key"
34+
35+
2936class ClientConfig :
3037 def __init__ (
3138 self ,
@@ -38,7 +45,7 @@ def __init__(
3845 ca_certs : Optional [str ] = None ,
3946 username : Optional [str ] = None ,
4047 password : Optional [str ] = None ,
41- auth_type : Optional [str ] = "Basic" ,
48+ auth_type : Optional [AuthType ] = AuthType . BASIC ,
4249 token : Optional [str ] = None ,
4350 user_agent : Optional [str ] = None ,
4451 extra_headers : Optional [dict ] = None ,
@@ -202,16 +209,16 @@ def password(self, value: str) -> None:
202209 self ._password = value
203210
204211 @property
205- def auth_type (self ) -> str :
212+ def auth_type (self ) -> AuthType :
206213 """Returns the type of authentication to the remote server."""
207214 return self ._auth_type
208215
209216 @auth_type .setter
210- def auth_type (self , value : str ) -> None :
217+ def auth_type (self , value : AuthType ) -> None :
211218 """Sets the type of authentication to the remote server if it is not
212219 using basic with username and password.
213220
214- Support values: Bearer, X-API-Key . For others, please use `extra_headers` instead
221+ :Args: value - AuthType enum value . For others, please use `extra_headers` instead
215222 """
216223 self ._auth_type = value
217224
@@ -273,13 +280,12 @@ def get_proxy_url(self) -> Optional[str]:
273280
274281 def get_auth_header (self ) -> Optional [dict ]:
275282 """Returns the authorization to add to the request headers."""
276- auth_type = self .auth_type .lower ()
277- if auth_type == "basic" and self .username and self .password :
283+ if self .auth_type is AuthType .BASIC and self .username and self .password :
278284 credentials = f"{ self .username } :{ self .password } "
279285 encoded_credentials = base64 .b64encode (credentials .encode ("utf-8" )).decode ("utf-8" )
280- return {"Authorization" : f"Basic { encoded_credentials } " }
281- if auth_type == "bearer" and self .token :
282- return {"Authorization" : f"Bearer { self .token } " }
283- if auth_type == "x-api-key" and self .token :
284- return {"X-API-Key " : f"{ self .token } " }
286+ return {"Authorization" : f"{ AuthType . BASIC . value } { encoded_credentials } " }
287+ if self . auth_type is AuthType . BEARER and self .token :
288+ return {"Authorization" : f"{ AuthType . BEARER . value } { self .token } " }
289+ if self . auth_type is AuthType . X_API_KEY and self .token :
290+ return {f" { AuthType . X_API_KEY . value } " : f"{ self .token } " }
285291 return None
0 commit comments