Skip to content

Commit

Permalink
Allow trailing slashes in SDK client url (#5057)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiltsov-max authored Oct 9, 2022
1 parent b029bc9 commit 511f970
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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

0 comments on commit 511f970

Please sign in to comment.