Skip to content

Commit

Permalink
Merge pull request #93 from ynput/enhancement/timeout-in-server-valid…
Browse files Browse the repository at this point in the history
…ation

Timeout: Use timeout for URL validation
  • Loading branch information
iLLiCiTiT authored Sep 15, 2023
2 parents 37e3a43 + 539f3de commit 36abb70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
13 changes: 3 additions & 10 deletions ayon_api/server_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
TransferProgress,
create_dependency_package_basename,
ThumbnailContent,
get_default_timeout,
)

PatternType = type(re.compile(""))
Expand Down Expand Up @@ -351,7 +352,6 @@ class ServerAPI(object):
timeout (Optional[float]): Timeout for requests.
max_retries (Optional[int]): Number of retries for requests.
"""
_default_timeout = 10.0
_default_max_retries = 3

def __init__(
Expand Down Expand Up @@ -500,20 +500,13 @@ def set_cert(self, cert):
def get_default_timeout(cls):
"""Default value for requests timeout.
First looks for environment variable SERVER_TIMEOUT_ENV_KEY which
can affect timeout value. If not available then use class
attribute '_default_timeout'.
Utils function 'get_default_timeout' is used by default.
Returns:
float: Timeout value in seconds.
"""

try:
return float(os.environ.get(SERVER_TIMEOUT_ENV_KEY))
except (ValueError, TypeError):
pass

return cls._default_timeout
return get_default_timeout()

@classmethod
def get_default_max_retries(cls):
Expand Down
34 changes: 28 additions & 6 deletions ayon_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
import datetime
import uuid
Expand All @@ -15,6 +16,7 @@
import requests
import unidecode

from .constants import SERVER_TIMEOUT_ENV_KEY
from .exceptions import UrlError

REMOVED_VALUE = object()
Expand All @@ -27,6 +29,23 @@
)


def get_default_timeout():
"""Default value for requests timeout.
First looks for environment variable SERVER_TIMEOUT_ENV_KEY which
can affect timeout value. If not available then use 10.0 s.
Returns:
float: Timeout value in seconds.
"""

try:
return float(os.environ.get(SERVER_TIMEOUT_ENV_KEY))
except (ValueError, TypeError):
pass
return 10.0


class ThumbnailContent:
"""Wrapper for thumbnail content.
Expand Down Expand Up @@ -231,11 +250,13 @@ def _try_parse_url(url):
return None


def _try_connect_to_server(url):
def _try_connect_to_server(url, timeout=None):
if timeout is None:
timeout = get_default_timeout()
try:
# TODO add validation if the url lead to Ayon server
# - thiw won't validate if the url lead to 'google.com'
requests.get(url)
# - this won't validate if the url lead to 'google.com'
requests.get(url, timeout=timeout)

except BaseException:
return False
Expand Down Expand Up @@ -313,7 +334,7 @@ def is_token_valid(url, token):
return response.status_code == 200


def validate_url(url):
def validate_url(url, timeout=None):
"""Validate url if is valid and server is available.
Validation checks if can be parsed as url and contains scheme.
Expand All @@ -334,6 +355,7 @@ def validate_url(url):
Args:
url (str): Server url.
timeout (Optional[int]): Timeout in seconds for connection to server.
Returns:
Url which was used to connect to server.
Expand Down Expand Up @@ -369,10 +391,10 @@ def validate_url(url):
# - this will trigger UrlError if both will crash
if not parsed_url.scheme:
new_url = "https://" + modified_url
if _try_connect_to_server(new_url):
if _try_connect_to_server(new_url, timeout=timeout):
return new_url

if _try_connect_to_server(modified_url):
if _try_connect_to_server(modified_url, timeout=timeout):
return modified_url

hints = []
Expand Down

0 comments on commit 36abb70

Please sign in to comment.