From 48742015c793070fd88e701b00b1099b3884e1c1 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Fri, 2 May 2025 10:29:03 +0200 Subject: [PATCH 1/3] try connect handles change of schema --- ayon_api/utils.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 5529f121f..4758ce2fc 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -322,18 +322,38 @@ def _try_parse_url(url: str) -> Optional[str]: def _try_connect_to_server( - url: str, timeout: Optional[float] = None -) -> bool: + url: str, + timeout: Optional[float], + verify: Optional["Union[str, bool]"], + cert: Optional[str], +) -> Optional[str]: if timeout is None: timeout = get_default_timeout() + + if verify is None: + verify = os.environ.get("AYON_CA_FILE") or True + + if cert is None: + cert = os.environ.get("AYON_CERT_FILE") or None + try: # TODO add validation if the url lead to AYON server # - this won't validate if the url lead to 'google.com' - requests.get(url, timeout=timeout) + response = requests.get( + url, + timeout=timeout, + verify=verify, + cert=cert, + ) + if response.history: + return response.history[-1].headers["location"].rstrip("/") + return url - except BaseException: - return False - return True + except Exception: + print(f"Failed to connect to '{url}'") + traceback.print_exc() + + return None def login_to_server( From 16572503e446d47ed4eb28aa1a046ba1f6431c28 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Fri, 2 May 2025 10:29:30 +0200 Subject: [PATCH 2/3] validate url can expect verify and cert arguments --- ayon_api/utils.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 4758ce2fc..dedf88356 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -483,7 +483,12 @@ def is_token_valid( return False -def validate_url(url: str, timeout: Optional[int] = None) -> str: +def validate_url( + url: str, + timeout: Optional[int] = None, + verify: Optional["Union[str, bool]"] = None, + cert: Optional[str] = None, +) -> str: """Validate url if is valid and server is available. Validation checks if can be parsed as url and contains scheme. @@ -540,12 +545,23 @@ def validate_url(url: str, timeout: Optional[int] = None) -> str: # Try add 'https://' scheme if is missing # - 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, timeout=timeout): + new_url = _try_connect_to_server( + "http://" + modified_url, + timeout=timeout, + verify=verify, + cert=cert, + ) + if new_url: return new_url - if _try_connect_to_server(modified_url, timeout=timeout): - return modified_url + new_url = _try_connect_to_server( + modified_url, + timeout=timeout, + verify=verify, + cert=cert, + ) + if new_url: + return new_url hints = [] if "/" in parsed_url.path or not parsed_url.scheme: From a29ddfcc3cab3b526e59133142b5476e3c4924c7 Mon Sep 17 00:00:00 2001 From: Jakub Trllo <43494761+iLLiCiTiT@users.noreply.github.com> Date: Fri, 2 May 2025 10:30:15 +0200 Subject: [PATCH 3/3] added missing import --- ayon_api/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ayon_api/utils.py b/ayon_api/utils.py index dedf88356..5055ebc2e 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -4,6 +4,7 @@ import uuid import string import platform +import traceback import collections from urllib.parse import urlparse, urlencode import typing