Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow trailing slashes in SDK client url #5057

Merged
merged 4 commits into from
Oct 9, 2022
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ non-ascii paths while adding files from "Connected file share" (issue #4428)
- Fixed cvat-core ESlint problems (<https://github.com/opencv/cvat/pull/5027>)
- Fixed task creation with non-local files via the SDK/CLI
(<https://github.com/opencv/cvat/issues/4962>)
- A trailing slash in hostname does't allow SDK to send some requests
(<https://github.com/opencv/cvat/pull/5057>)

### Security
- TDB
Expand Down
6 changes: 4 additions & 2 deletions cvat-sdk/cvat_sdk/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def _validate_and_prepare_url(cls, url: str) -> str:
schema = ""
base_url = url

base_url = base_url.rstrip("/")

if schema and schema not in cls.ALLOWED_SCHEMAS:
raise InvalidHostException(
f"Invalid url schema '{schema}', expected "
Expand Down Expand Up @@ -279,7 +281,7 @@ class CVAT_API_V2:
"""Build parameterized API URLs"""

def __init__(self, host: str):
self.host = host
self.host = host.rstrip("/")
self.base = self.host + "/api/"
self.git = self.host + "/git/repository/"

Expand Down Expand Up @@ -308,7 +310,7 @@ def make_endpoint_url(
def make_client(
host: str, *, port: Optional[int] = None, credentials: Optional[Tuple[int, int]] = None
) -> Client:
url = host
url = host.rstrip("/")
if port:
url = f"{url}:{port}"

Expand Down
13 changes: 13 additions & 0 deletions tests/python/sdk/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ def test_can_get_server_version(self):
assert (version.major, version.minor) >= (2, 0)


def test_can_strip_trailing_slash_in_hostname_in_make_client(admin_user: str):
host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1)

with make_client(host=host + "/", port=port, credentials=(admin_user, USER_PASS)) as client:
assert client.api_map.host == BASE_URL


def test_can_strip_trailing_slash_in_hostname_in_client_ctor(admin_user: str):
with Client(url=BASE_URL + "/") as client:
client.login((admin_user, USER_PASS))
assert client.api_map.host == BASE_URL


def test_can_detect_server_schema_if_not_provided():
host, port = BASE_URL.split("://", maxsplit=1)[1].rsplit(":", maxsplit=1)
client = make_client(host=host, port=int(port))
Expand Down