From b598c2170f9c6462bfdae2e053fa4c31f6d9b956 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Fri, 15 Sep 2023 15:00:38 +0200 Subject: [PATCH 1/3] moved 'get_default_timeout' to utils --- ayon_api/server_api.py | 23 ++--------------------- ayon_api/utils.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index fcdd6b95d..b87051ada 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -75,6 +75,7 @@ TransferProgress, create_dependency_package_basename, ThumbnailContent, + get_default_timeout, ) PatternType = type(re.compile("")) @@ -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__( @@ -496,25 +496,6 @@ def set_cert(self, cert): ssl_verify = property(get_ssl_verify, set_ssl_verify) cert = property(get_cert, set_cert) - @classmethod - 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'. - - Returns: - float: Timeout value in seconds. - """ - - try: - return float(os.environ.get(SERVER_TIMEOUT_ENV_KEY)) - except (ValueError, TypeError): - pass - - return cls._default_timeout - @classmethod def get_default_max_retries(cls): """Default value for requests max retries. @@ -551,7 +532,7 @@ def set_timeout(self, timeout): """ if timeout is None: - timeout = self.get_default_timeout() + timeout = get_default_timeout() self._timeout = float(timeout) def get_max_retries(self): diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 1ee6abf72..1206c9944 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -1,3 +1,4 @@ +import os import re import datetime import uuid @@ -15,6 +16,7 @@ import requests import unidecode +from .constants import SERVER_TIMEOUT_ENV_KEY from .exceptions import UrlError REMOVED_VALUE = object() @@ -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. From 58e2135adc333e0b8e6e706dd7fa3436ddefcdd5 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Fri, 15 Sep 2023 15:00:53 +0200 Subject: [PATCH 2/3] use timeout in 'validate_url' --- ayon_api/utils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 1206c9944..8c8699acf 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -250,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 @@ -332,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. @@ -353,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. @@ -388,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 = [] From 539f3de4d046c8887659eb760841b8d224a7e1fe Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Fri, 15 Sep 2023 15:04:20 +0200 Subject: [PATCH 3/3] keep option to override 'get_default_timeout' on 'ServerAPI' class --- ayon_api/server_api.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ayon_api/server_api.py b/ayon_api/server_api.py index b87051ada..41f578888 100644 --- a/ayon_api/server_api.py +++ b/ayon_api/server_api.py @@ -496,6 +496,18 @@ def set_cert(self, cert): ssl_verify = property(get_ssl_verify, set_ssl_verify) cert = property(get_cert, set_cert) + @classmethod + def get_default_timeout(cls): + """Default value for requests timeout. + + Utils function 'get_default_timeout' is used by default. + + Returns: + float: Timeout value in seconds. + """ + + return get_default_timeout() + @classmethod def get_default_max_retries(cls): """Default value for requests max retries. @@ -532,7 +544,7 @@ def set_timeout(self, timeout): """ if timeout is None: - timeout = get_default_timeout() + timeout = self.get_default_timeout() self._timeout = float(timeout) def get_max_retries(self):