Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Self, TypeVar, get_args, get_origin, overload
from urllib.parse import quote

import httpx
from nemo_platform_plugin.client.auth import (
Expand Down Expand Up @@ -177,8 +178,9 @@ def _resolve_path(self, request: PreparedRequest) -> str:
if self._workspace:
params["workspace"] = self._workspace
params.update(request.path_params)
encoded_params = {name: quote(str(value), safe="") for name, value in params.items()}
try:
path = request.path_template.format_map(params)
path = request.path_template.format_map(encoded_params)
except KeyError as exc:
raise ValueError(f"Missing path parameter {exc} for {request.method} {request.path_template}") from exc
return self._base_url + path
Expand Down
28 changes: 28 additions & 0 deletions packages/nemo_platform_plugin/tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,34 @@ def test_send_get_with_path_params() -> None:
)


def test_send_url_encodes_path_params() -> None:
mock_http = MagicMock(spec=httpx.Client)
mock_http.request.return_value = httpx.Response(
200,
request=httpx.Request("GET", f"{BASE}/apis/test/v2/items/name%20with%20%3F%23%2F"),
json={"id": 1, "name": "encoded"},
)

client = NemoClient(base_url=BASE, http_client=mock_http)
client.send(GET_ITEM(name="name with ?#/"))

assert mock_http.request.call_args.args[1] == f"{BASE}/apis/test/v2/items/name%20with%20%3F%23%2F"


def test_send_url_encodes_default_workspace() -> None:
mock_http = MagicMock(spec=httpx.Client)
mock_http.request.return_value = httpx.Response(
200,
request=httpx.Request("GET", f"{BASE}/apis/test/v2/workspaces/team%20one%2Fwest/items"),
json={"id": 1, "name": "encoded"},
)

client = NemoClient(base_url=BASE, workspace="team one/west", http_client=mock_http)
client.send(GET_WS_ITEM())

assert mock_http.request.call_args.args[1] == f"{BASE}/apis/test/v2/workspaces/team%20one%2Fwest/items"


def test_send_delete() -> None:
mock_http = MagicMock(spec=httpx.Client)
mock_http.request.return_value = httpx.Response(
Expand Down