Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions ayon_api/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import copy

try:
Expand All @@ -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):
Expand Down
35 changes: 18 additions & 17 deletions ayon_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from .exceptions import (
UrlError,
UrlNotReached,
ServerError,
UnauthorizedError,
HTTPRequestError,
Expand Down Expand Up @@ -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()
Comment on lines 573 to +579
if response.history:
return response.history[-1].headers["location"].rstrip("/")
return url
Expand Down Expand Up @@ -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,
Expand All @@ -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
)
Expand Down
Loading