66
77from .exceptions import HyperbrowserError
88from .header_utils import normalize_headers , parse_headers_env_json
9+ from .type_utils import is_plain_int , is_plain_string
910
1011_ENCODED_HOST_DELIMITER_PATTERN = re .compile (r"%(?:2f|3f|23|40|3a)" , re .IGNORECASE )
1112
@@ -32,11 +33,11 @@ def normalize_api_key(
3233 * ,
3334 empty_error_message : str = "api_key must not be empty" ,
3435 ) -> str :
35- if type (api_key ) is not str :
36+ if not is_plain_string (api_key ):
3637 raise HyperbrowserError ("api_key must be a string" )
3738 try :
3839 normalized_api_key = api_key .strip ()
39- if type (normalized_api_key ) is not str :
40+ if not is_plain_string (normalized_api_key ):
4041 raise TypeError ("normalized api_key must be a string" )
4142 except HyperbrowserError :
4243 raise
@@ -99,7 +100,7 @@ def _decode_url_component_with_limit(value: str, *, component_label: str) -> str
99100 def _safe_unquote (value : str , * , component_label : str ) -> str :
100101 try :
101102 decoded_value = unquote (value )
102- if type (decoded_value ) is not str :
103+ if not is_plain_string (decoded_value ):
103104 raise TypeError ("decoded URL component must be a string" )
104105 return decoded_value
105106 except HyperbrowserError :
@@ -112,14 +113,14 @@ def _safe_unquote(value: str, *, component_label: str) -> str:
112113
113114 @staticmethod
114115 def normalize_base_url (base_url : str ) -> str :
115- if type (base_url ) is not str :
116+ if not is_plain_string (base_url ):
116117 raise HyperbrowserError ("base_url must be a string" )
117118 try :
118119 stripped_base_url = base_url .strip ()
119- if type (stripped_base_url ) is not str :
120+ if not is_plain_string (stripped_base_url ):
120121 raise TypeError ("normalized base_url must be a string" )
121122 normalized_base_url = stripped_base_url .rstrip ("/" )
122- if type (normalized_base_url ) is not str :
123+ if not is_plain_string (normalized_base_url ):
123124 raise TypeError ("normalized base_url must be a string" )
124125 except HyperbrowserError :
125126 raise
@@ -165,11 +166,11 @@ def normalize_base_url(base_url: str) -> str:
165166 original_error = exc ,
166167 ) from exc
167168 if (
168- type (parsed_base_url_scheme ) is not str
169- or type (parsed_base_url_netloc ) is not str
170- or type (parsed_base_url_path ) is not str
171- or type (parsed_base_url_query ) is not str
172- or type (parsed_base_url_fragment ) is not str
169+ not is_plain_string (parsed_base_url_scheme )
170+ or not is_plain_string (parsed_base_url_netloc )
171+ or not is_plain_string (parsed_base_url_path )
172+ or not is_plain_string (parsed_base_url_query )
173+ or not is_plain_string (parsed_base_url_fragment )
173174 ):
174175 raise HyperbrowserError ("base_url parser returned invalid URL components" )
175176 try :
@@ -181,9 +182,8 @@ def normalize_base_url(base_url: str) -> str:
181182 "Failed to parse base_url host" ,
182183 original_error = exc ,
183184 ) from exc
184- if (
185- parsed_base_url_hostname is not None
186- and type (parsed_base_url_hostname ) is not str
185+ if parsed_base_url_hostname is not None and not is_plain_string (
186+ parsed_base_url_hostname
187187 ):
188188 raise HyperbrowserError ("base_url parser returned invalid URL components" )
189189 if (
@@ -211,14 +211,12 @@ def normalize_base_url(base_url: str) -> str:
211211 "Failed to parse base_url credentials" ,
212212 original_error = exc ,
213213 ) from exc
214- if (
215- parsed_base_url_username is not None
216- and type (parsed_base_url_username ) is not str
214+ if parsed_base_url_username is not None and not is_plain_string (
215+ parsed_base_url_username
217216 ):
218217 raise HyperbrowserError ("base_url parser returned invalid URL components" )
219- if (
220- parsed_base_url_password is not None
221- and type (parsed_base_url_password ) is not str
218+ if parsed_base_url_password is not None and not is_plain_string (
219+ parsed_base_url_password
222220 ):
223221 raise HyperbrowserError ("base_url parser returned invalid URL components" )
224222 if parsed_base_url_username is not None or parsed_base_url_password is not None :
@@ -237,7 +235,7 @@ def normalize_base_url(base_url: str) -> str:
237235 "base_url must contain a valid port number" ,
238236 original_error = exc ,
239237 ) from exc
240- if parsed_base_url_port is not None and type (parsed_base_url_port ) is not int :
238+ if parsed_base_url_port is not None and not is_plain_int (parsed_base_url_port ):
241239 raise HyperbrowserError ("base_url parser returned invalid URL components" )
242240 if parsed_base_url_port is not None and not (
243241 0 <= parsed_base_url_port <= 65535
@@ -351,11 +349,11 @@ def parse_headers_from_env(raw_headers: Optional[str]) -> Optional[Dict[str, str
351349 def resolve_base_url_from_env (raw_base_url : Optional [str ]) -> str :
352350 if raw_base_url is None :
353351 return "https://api.hyperbrowser.ai"
354- if type (raw_base_url ) is not str :
352+ if not is_plain_string (raw_base_url ):
355353 raise HyperbrowserError ("HYPERBROWSER_BASE_URL must be a string" )
356354 try :
357355 normalized_env_base_url = raw_base_url .strip ()
358- if type (normalized_env_base_url ) is not str :
356+ if not is_plain_string (normalized_env_base_url ):
359357 raise TypeError ("normalized environment base_url must be a string" )
360358 is_empty_env_base_url = len (normalized_env_base_url ) == 0
361359 except HyperbrowserError :
0 commit comments