1+ import os
12import re
23import datetime
34import uuid
1516import requests
1617import unidecode
1718
19+ from .constants import SERVER_TIMEOUT_ENV_KEY
1820from .exceptions import UrlError
1921
2022REMOVED_VALUE = object ()
2729)
2830
2931
32+ def get_default_timeout ():
33+ """Default value for requests timeout.
34+
35+ First looks for environment variable SERVER_TIMEOUT_ENV_KEY which
36+ can affect timeout value. If not available then use 10.0 s.
37+
38+ Returns:
39+ float: Timeout value in seconds.
40+ """
41+
42+ try :
43+ return float (os .environ .get (SERVER_TIMEOUT_ENV_KEY ))
44+ except (ValueError , TypeError ):
45+ pass
46+ return 10.0
47+
48+
3049class ThumbnailContent :
3150 """Wrapper for thumbnail content.
3251
@@ -231,11 +250,13 @@ def _try_parse_url(url):
231250 return None
232251
233252
234- def _try_connect_to_server (url ):
253+ def _try_connect_to_server (url , timeout = None ):
254+ if timeout is None :
255+ timeout = get_default_timeout ()
235256 try :
236257 # TODO add validation if the url lead to Ayon server
237- # - thiw won't validate if the url lead to 'google.com'
238- requests .get (url )
258+ # - this won't validate if the url lead to 'google.com'
259+ requests .get (url , timeout = timeout )
239260
240261 except BaseException :
241262 return False
@@ -313,7 +334,7 @@ def is_token_valid(url, token):
313334 return response .status_code == 200
314335
315336
316- def validate_url (url ):
337+ def validate_url (url , timeout = None ):
317338 """Validate url if is valid and server is available.
318339
319340 Validation checks if can be parsed as url and contains scheme.
@@ -334,6 +355,7 @@ def validate_url(url):
334355
335356 Args:
336357 url (str): Server url.
358+ timeout (Optional[int]): Timeout in seconds for connection to server.
337359
338360 Returns:
339361 Url which was used to connect to server.
@@ -369,10 +391,10 @@ def validate_url(url):
369391 # - this will trigger UrlError if both will crash
370392 if not parsed_url .scheme :
371393 new_url = "https://" + modified_url
372- if _try_connect_to_server (new_url ):
394+ if _try_connect_to_server (new_url , timeout = timeout ):
373395 return new_url
374396
375- if _try_connect_to_server (modified_url ):
397+ if _try_connect_to_server (modified_url , timeout = timeout ):
376398 return modified_url
377399
378400 hints = []
0 commit comments