Skip to content

Commit 36abb70

Browse files
authored
Merge pull request #93 from ynput/enhancement/timeout-in-server-validation
Timeout: Use timeout for URL validation
2 parents 37e3a43 + 539f3de commit 36abb70

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

ayon_api/server_api.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
TransferProgress,
7676
create_dependency_package_basename,
7777
ThumbnailContent,
78+
get_default_timeout,
7879
)
7980

8081
PatternType = type(re.compile(""))
@@ -351,7 +352,6 @@ class ServerAPI(object):
351352
timeout (Optional[float]): Timeout for requests.
352353
max_retries (Optional[int]): Number of retries for requests.
353354
"""
354-
_default_timeout = 10.0
355355
_default_max_retries = 3
356356

357357
def __init__(
@@ -500,20 +500,13 @@ def set_cert(self, cert):
500500
def get_default_timeout(cls):
501501
"""Default value for requests timeout.
502502
503-
First looks for environment variable SERVER_TIMEOUT_ENV_KEY which
504-
can affect timeout value. If not available then use class
505-
attribute '_default_timeout'.
503+
Utils function 'get_default_timeout' is used by default.
506504
507505
Returns:
508506
float: Timeout value in seconds.
509507
"""
510508

511-
try:
512-
return float(os.environ.get(SERVER_TIMEOUT_ENV_KEY))
513-
except (ValueError, TypeError):
514-
pass
515-
516-
return cls._default_timeout
509+
return get_default_timeout()
517510

518511
@classmethod
519512
def get_default_max_retries(cls):

ayon_api/utils.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import re
23
import datetime
34
import uuid
@@ -15,6 +16,7 @@
1516
import requests
1617
import unidecode
1718

19+
from .constants import SERVER_TIMEOUT_ENV_KEY
1820
from .exceptions import UrlError
1921

2022
REMOVED_VALUE = object()
@@ -27,6 +29,23 @@
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+
3049
class 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

Comments
 (0)