diff --git a/ayon_api/exceptions.py b/ayon_api/exceptions.py index 6a44e9d9c..c72beb915 100644 --- a/ayon_api/exceptions.py +++ b/ayon_api/exceptions.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import copy try: @@ -21,13 +23,23 @@ class UrlError(Exception): UI if needed. """ - def __init__(self, message, title, hints=None): + + def __init__( + self, + message: str, + title: str, + hints: list[str] | None = None, + ) -> None: if hints is None: hints = [] self.title = title self.hints = hints - super(UrlError, self).__init__(message) + super().__init__(message) + + +class UrlNotReached(UrlError): + pass class ServerError(Exception): diff --git a/ayon_api/utils.py b/ayon_api/utils.py index 7294c110b..cd3a9a71a 100644 --- a/ayon_api/utils.py +++ b/ayon_api/utils.py @@ -27,6 +27,7 @@ ) from .exceptions import ( UrlError, + UrlNotReached, ServerError, UnauthorizedError, HTTPRequestError, @@ -570,11 +571,12 @@ def _try_connect_to_server( # TODO add validation if the url lead to AYON server # - this won't validate if the url lead to 'google.com' response = requests.get( - url, + f"{url}/api/info", timeout=timeout, verify=verify, cert=cert, ) + _ = response.json() if response.history: return response.history[-1].headers["location"].rstrip("/") return url @@ -759,24 +761,29 @@ def validate_url( # Not sure if this is good idea? modified_url = stripperd_url.rstrip("/") + + # Make sure url has http scheme + if not modified_url.lower().startswith("http"): + modified_url = f"http://{modified_url}" + parsed_url = _try_parse_url(modified_url) universal_hints = [ "does the url work in browser?" ] if parsed_url is None: raise UrlError( - "Invalid url format. Url cannot be parsed as url \"{}\".".format( - modified_url + ( + "Invalid url format. Url cannot be parsed" + f" as url \"{modified_url}\"." ), title="Invalid url format", hints=universal_hints ) - # Try add 'https://' scheme if is missing - # - this will trigger UrlError if both will crash - if not parsed_url.scheme: + pathless_url = f"{parsed_url.scheme}://{parsed_url.netloc}" + if parsed_url.path: new_url = _try_connect_to_server( - "http://" + modified_url, + pathless_url, timeout=timeout, verify=verify, cert=cert, @@ -794,17 +801,11 @@ def validate_url( return new_url hints = [] - if "/" in parsed_url.path or not parsed_url.scheme: - new_path = parsed_url.path.split("/")[0] - if not parsed_url.scheme: - new_path = "https://" + new_path - - hints.append( - "did you mean \"{}\"?".format(parsed_url.scheme + new_path) - ) + if parsed_url.path: + hints.append(f"did you mean \"{pathless_url}\"?") - raise UrlError( - "Couldn't connect to server on \"{}\"".format(url), + raise UrlNotReached( + f"Couldn't connect to server on \"{url}\"", title="Couldn't connect to server", hints=hints + universal_hints )