diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 69c80f54..69cc07fb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,12 +1,14 @@ name: CI on: push: - branches-ignore: - - 'generated' - - 'codegen/**' - - 'integrated/**' - - 'stl-preview-head/**' - - 'stl-preview-base/**' + branches: + - '**' + - '!integrated/**' + - '!stl-preview-head/**' + - '!stl-preview-base/**' + - '!generated' + - '!codegen/**' + - 'codegen/stl/**' pull_request: branches-ignore: - 'stl-preview-head/**' @@ -17,7 +19,7 @@ jobs: timeout-minutes: 10 name: lint runs-on: ${{ github.repository == 'stainless-sdks/openlayer-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }} - if: github.event_name == 'push' || github.event.pull_request.head.repo.fork + if: (github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata') steps: - uses: actions/checkout@v6 @@ -36,7 +38,7 @@ jobs: run: ./scripts/lint build: - if: github.event_name == 'push' || github.event.pull_request.head.repo.fork + if: (github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata') timeout-minutes: 10 name: build permissions: diff --git a/.gitignore b/.gitignore index 0dcb47e1..cd448815 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .prism.log +.stdy.log _dev __pycache__ diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 86b0e83d..cb9d2541 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.21.0" + ".": "0.22.0" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 762802f1..81f70284 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,39 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## 0.22.0 (2026-04-01) + +Full Changelog: [v0.21.0...v0.22.0](https://github.com/openlayer-ai/openlayer-python/compare/v0.21.0...v0.22.0) + +### Features + +* **internal:** implement indices array format for query and form serialization ([e942e18](https://github.com/openlayer-ai/openlayer-python/commit/e942e184c2aa996816f3ce31dc63540bba1b4a67)) + + +### Bug Fixes + +* **deps:** bump minimum typing-extensions version ([d359d17](https://github.com/openlayer-ai/openlayer-python/commit/d359d17848a178449417287829dba2e3aba8e8b2)) +* **pydantic:** do not pass `by_alias` unless set ([3679fc3](https://github.com/openlayer-ai/openlayer-python/commit/3679fc375ce6f06bfc1b73e810634de96435fa56)) +* sanitize endpoint path params ([dc9d512](https://github.com/openlayer-ai/openlayer-python/commit/dc9d512d70c3543562e241d1ad13c32700d9e79a)) + + +### Chores + +* **ci:** skip lint on metadata-only changes ([e23a0d8](https://github.com/openlayer-ai/openlayer-python/commit/e23a0d85258132f481f2780516bdd60f37ae60d5)) +* **internal:** tweak CI branches ([7e02151](https://github.com/openlayer-ai/openlayer-python/commit/7e021510e4a2abfe57e9634dcf561a55d52df23f)) +* **internal:** update gitignore ([e96f24b](https://github.com/openlayer-ai/openlayer-python/commit/e96f24b7a87c928e0e276b728e67e887ed2b4f1f)) +* **tests:** bump steady to v0.19.4 ([0ecf1e6](https://github.com/openlayer-ai/openlayer-python/commit/0ecf1e6b23fa9072b549325728ce7de83f0f8439)) +* **tests:** bump steady to v0.19.5 ([757aae0](https://github.com/openlayer-ai/openlayer-python/commit/757aae03228cbeb6daea0094a0744bb5148fb05f)) +* **tests:** bump steady to v0.19.6 ([95c72a9](https://github.com/openlayer-ai/openlayer-python/commit/95c72a92f27d3f8012a34567d134ee667663ee75)) +* **tests:** bump steady to v0.19.7 ([16d4263](https://github.com/openlayer-ai/openlayer-python/commit/16d426396e70c2a9e81e61a8ed7b323a17c90e95)) +* **tests:** bump steady to v0.20.1 ([6f06faf](https://github.com/openlayer-ai/openlayer-python/commit/6f06faf139d49d4ef42b1043f35ccb7c8653f067)) +* **tests:** bump steady to v0.20.2 ([5c4116d](https://github.com/openlayer-ai/openlayer-python/commit/5c4116d7f56608a438e91593be3ab78ebff7e2e2)) + + +### Refactors + +* **tests:** switch from prism to steady ([0f01051](https://github.com/openlayer-ai/openlayer-python/commit/0f01051033b82d888ea3d3966fb1f49b338e92c4)) + ## 0.21.0 (2026-03-26) Full Changelog: [v0.20.1...v0.21.0](https://github.com/openlayer-ai/openlayer-python/compare/v0.20.1...v0.21.0) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c835a6e3..c4992893 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -85,7 +85,7 @@ $ pip install ./path-to-wheel-file.whl ## Running tests -Most tests require you to [set up a mock server](https://github.com/stoplightio/prism) against the OpenAPI spec to run the tests. +Most tests require you to [set up a mock server](https://github.com/dgellow/steady) against the OpenAPI spec to run the tests. ```sh $ ./scripts/mock diff --git a/pyproject.toml b/pyproject.toml index 6b6867ff..e2557d0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "openlayer" -version = "0.21.0" +version = "0.22.0" description = "The official Python library for the openlayer API" dynamic = ["readme"] license = "Apache-2.0" @@ -11,7 +11,7 @@ authors = [ dependencies = [ "httpx>=0.23.0, <1", "pydantic>=1.9.0, <3", - "typing-extensions>=4.10, <5", + "typing-extensions>=4.14, <5", "anyio>=3.5.0, <5", "distro>=1.7.0, <2", "sniffio", diff --git a/scripts/mock b/scripts/mock index bcf3b392..5cd7c157 100755 --- a/scripts/mock +++ b/scripts/mock @@ -19,34 +19,34 @@ fi echo "==> Starting mock server with URL ${URL}" -# Run prism mock on the given spec +# Run steady mock on the given spec if [ "$1" == "--daemon" ]; then # Pre-install the package so the download doesn't eat into the startup timeout - npm exec --package=@stainless-api/prism-cli@5.15.0 -- prism --version + npm exec --package=@stdy/cli@0.20.2 -- steady --version - npm exec --package=@stainless-api/prism-cli@5.15.0 -- prism mock "$URL" &> .prism.log & + npm exec --package=@stdy/cli@0.20.2 -- steady --host 127.0.0.1 -p 4010 --validator-query-array-format=comma --validator-form-array-format=comma --validator-query-object-format=brackets --validator-form-object-format=brackets "$URL" &> .stdy.log & - # Wait for server to come online (max 30s) + # Wait for server to come online via health endpoint (max 30s) echo -n "Waiting for server" attempts=0 - while ! grep -q "✖ fatal\|Prism is listening" ".prism.log" ; do + while ! curl --silent --fail "http://127.0.0.1:4010/_x-steady/health" >/dev/null 2>&1; do + if ! kill -0 $! 2>/dev/null; then + echo + cat .stdy.log + exit 1 + fi attempts=$((attempts + 1)) if [ "$attempts" -ge 300 ]; then echo - echo "Timed out waiting for Prism server to start" - cat .prism.log + echo "Timed out waiting for Steady server to start" + cat .stdy.log exit 1 fi echo -n "." sleep 0.1 done - if grep -q "✖ fatal" ".prism.log"; then - cat .prism.log - exit 1 - fi - echo else - npm exec --package=@stainless-api/prism-cli@5.15.0 -- prism mock "$URL" + npm exec --package=@stdy/cli@0.20.2 -- steady --host 127.0.0.1 -p 4010 --validator-query-array-format=comma --validator-form-array-format=comma --validator-query-object-format=brackets --validator-form-object-format=brackets "$URL" fi diff --git a/scripts/test b/scripts/test index dbeda2d2..b8143aa3 100755 --- a/scripts/test +++ b/scripts/test @@ -9,8 +9,8 @@ GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m' # No Color -function prism_is_running() { - curl --silent "http://localhost:4010" >/dev/null 2>&1 +function steady_is_running() { + curl --silent "http://127.0.0.1:4010/_x-steady/health" >/dev/null 2>&1 } kill_server_on_port() { @@ -25,7 +25,7 @@ function is_overriding_api_base_url() { [ -n "$TEST_API_BASE_URL" ] } -if ! is_overriding_api_base_url && ! prism_is_running ; then +if ! is_overriding_api_base_url && ! steady_is_running ; then # When we exit this script, make sure to kill the background mock server process trap 'kill_server_on_port 4010' EXIT @@ -36,19 +36,19 @@ fi if is_overriding_api_base_url ; then echo -e "${GREEN}✔ Running tests against ${TEST_API_BASE_URL}${NC}" echo -elif ! prism_is_running ; then - echo -e "${RED}ERROR:${NC} The test suite will not run without a mock Prism server" +elif ! steady_is_running ; then + echo -e "${RED}ERROR:${NC} The test suite will not run without a mock Steady server" echo -e "running against your OpenAPI spec." echo echo -e "To run the server, pass in the path or url of your OpenAPI" - echo -e "spec to the prism command:" + echo -e "spec to the steady command:" echo - echo -e " \$ ${YELLOW}npm exec --package=@stainless-api/prism-cli@5.15.0 -- prism mock path/to/your.openapi.yml${NC}" + echo -e " \$ ${YELLOW}npm exec --package=@stdy/cli@0.20.2 -- steady path/to/your.openapi.yml --host 127.0.0.1 -p 4010 --validator-query-array-format=comma --validator-form-array-format=comma --validator-query-object-format=brackets --validator-form-object-format=brackets${NC}" echo exit 1 else - echo -e "${GREEN}✔ Mock prism server is running with your OpenAPI spec${NC}" + echo -e "${GREEN}✔ Mock steady server is running with your OpenAPI spec${NC}" echo fi diff --git a/src/openlayer/_compat.py b/src/openlayer/_compat.py index 6fb60851..c1e1b13a 100644 --- a/src/openlayer/_compat.py +++ b/src/openlayer/_compat.py @@ -2,7 +2,7 @@ from typing import TYPE_CHECKING, Any, Union, Generic, TypeVar, Callable, cast, overload from datetime import date, datetime -from typing_extensions import Self, Literal +from typing_extensions import Self, Literal, TypedDict import pydantic from pydantic.fields import FieldInfo @@ -131,6 +131,10 @@ def model_json(model: pydantic.BaseModel, *, indent: int | None = None) -> str: return model.model_dump_json(indent=indent) +class _ModelDumpKwargs(TypedDict, total=False): + by_alias: bool + + def model_dump( model: pydantic.BaseModel, *, @@ -142,6 +146,9 @@ def model_dump( by_alias: bool | None = None, ) -> dict[str, Any]: if (not PYDANTIC_V1) or hasattr(model, "model_dump"): + kwargs: _ModelDumpKwargs = {} + if by_alias is not None: + kwargs["by_alias"] = by_alias return model.model_dump( mode=mode, exclude=exclude, diff --git a/src/openlayer/_qs.py b/src/openlayer/_qs.py index ada6fd3f..de8c99bc 100644 --- a/src/openlayer/_qs.py +++ b/src/openlayer/_qs.py @@ -101,7 +101,10 @@ def _stringify_item( items.extend(self._stringify_item(key, item, opts)) return items elif array_format == "indices": - raise NotImplementedError("The array indices format is not supported yet") + items = [] + for i, item in enumerate(value): + items.extend(self._stringify_item(f"{key}[{i}]", item, opts)) + return items elif array_format == "brackets": items = [] key = key + "[]" diff --git a/src/openlayer/_utils/__init__.py b/src/openlayer/_utils/__init__.py index dc64e29a..10cb66d2 100644 --- a/src/openlayer/_utils/__init__.py +++ b/src/openlayer/_utils/__init__.py @@ -1,3 +1,4 @@ +from ._path import path_template as path_template from ._sync import asyncify as asyncify from ._proxy import LazyProxy as LazyProxy from ._utils import ( diff --git a/src/openlayer/_utils/_path.py b/src/openlayer/_utils/_path.py new file mode 100644 index 00000000..4d6e1e4c --- /dev/null +++ b/src/openlayer/_utils/_path.py @@ -0,0 +1,127 @@ +from __future__ import annotations + +import re +from typing import ( + Any, + Mapping, + Callable, +) +from urllib.parse import quote + +# Matches '.' or '..' where each dot is either literal or percent-encoded (%2e / %2E). +_DOT_SEGMENT_RE = re.compile(r"^(?:\.|%2[eE]){1,2}$") + +_PLACEHOLDER_RE = re.compile(r"\{(\w+)\}") + + +def _quote_path_segment_part(value: str) -> str: + """Percent-encode `value` for use in a URI path segment. + + Considers characters not in `pchar` set from RFC 3986 §3.3 to be unsafe. + https://datatracker.ietf.org/doc/html/rfc3986#section-3.3 + """ + # quote() already treats unreserved characters (letters, digits, and -._~) + # as safe, so we only need to add sub-delims, ':', and '@'. + # Notably, unlike the default `safe` for quote(), / is unsafe and must be quoted. + return quote(value, safe="!$&'()*+,;=:@") + + +def _quote_query_part(value: str) -> str: + """Percent-encode `value` for use in a URI query string. + + Considers &, = and characters not in `query` set from RFC 3986 §3.4 to be unsafe. + https://datatracker.ietf.org/doc/html/rfc3986#section-3.4 + """ + return quote(value, safe="!$'()*+,;:@/?") + + +def _quote_fragment_part(value: str) -> str: + """Percent-encode `value` for use in a URI fragment. + + Considers characters not in `fragment` set from RFC 3986 §3.5 to be unsafe. + https://datatracker.ietf.org/doc/html/rfc3986#section-3.5 + """ + return quote(value, safe="!$&'()*+,;=:@/?") + + +def _interpolate( + template: str, + values: Mapping[str, Any], + quoter: Callable[[str], str], +) -> str: + """Replace {name} placeholders in `template`, quoting each value with `quoter`. + + Placeholder names are looked up in `values`. + + Raises: + KeyError: If a placeholder is not found in `values`. + """ + # re.split with a capturing group returns alternating + # [text, name, text, name, ..., text] elements. + parts = _PLACEHOLDER_RE.split(template) + + for i in range(1, len(parts), 2): + name = parts[i] + if name not in values: + raise KeyError(f"a value for placeholder {{{name}}} was not provided") + val = values[name] + if val is None: + parts[i] = "null" + elif isinstance(val, bool): + parts[i] = "true" if val else "false" + else: + parts[i] = quoter(str(values[name])) + + return "".join(parts) + + +def path_template(template: str, /, **kwargs: Any) -> str: + """Interpolate {name} placeholders in `template` from keyword arguments. + + Args: + template: The template string containing {name} placeholders. + **kwargs: Keyword arguments to interpolate into the template. + + Returns: + The template with placeholders interpolated and percent-encoded. + + Safe characters for percent-encoding are dependent on the URI component. + Placeholders in path and fragment portions are percent-encoded where the `segment` + and `fragment` sets from RFC 3986 respectively are considered safe. + Placeholders in the query portion are percent-encoded where the `query` set from + RFC 3986 §3.3 is considered safe except for = and & characters. + + Raises: + KeyError: If a placeholder is not found in `kwargs`. + ValueError: If resulting path contains /./ or /../ segments (including percent-encoded dot-segments). + """ + # Split the template into path, query, and fragment portions. + fragment_template: str | None = None + query_template: str | None = None + + rest = template + if "#" in rest: + rest, fragment_template = rest.split("#", 1) + if "?" in rest: + rest, query_template = rest.split("?", 1) + path_template = rest + + # Interpolate each portion with the appropriate quoting rules. + path_result = _interpolate(path_template, kwargs, _quote_path_segment_part) + + # Reject dot-segments (. and ..) in the final assembled path. The check + # runs after interpolation so that adjacent placeholders or a mix of static + # text and placeholders that together form a dot-segment are caught. + # Also reject percent-encoded dot-segments to protect against incorrectly + # implemented normalization in servers/proxies. + for segment in path_result.split("/"): + if _DOT_SEGMENT_RE.match(segment): + raise ValueError(f"Constructed path {path_result!r} contains dot-segment {segment!r} which is not allowed") + + result = path_result + if query_template is not None: + result += "?" + _interpolate(query_template, kwargs, _quote_query_part) + if fragment_template is not None: + result += "#" + _interpolate(fragment_template, kwargs, _quote_fragment_part) + + return result diff --git a/src/openlayer/_version.py b/src/openlayer/_version.py index 5cf8d337..92451487 100644 --- a/src/openlayer/_version.py +++ b/src/openlayer/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. __title__ = "openlayer" -__version__ = "0.21.0" # x-release-please-version +__version__ = "0.22.0" # x-release-please-version diff --git a/src/openlayer/resources/commits/commits.py b/src/openlayer/resources/commits/commits.py index df43c6e2..5b9ac98d 100644 --- a/src/openlayer/resources/commits/commits.py +++ b/src/openlayer/resources/commits/commits.py @@ -5,6 +5,7 @@ import httpx from ..._types import Body, Query, Headers, NotGiven, not_given +from ..._utils import path_template from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -77,7 +78,7 @@ def retrieve( if not project_version_id: raise ValueError(f"Expected a non-empty value for `project_version_id` but received {project_version_id!r}") return self._get( - f"/versions/{project_version_id}", + path_template("/versions/{project_version_id}", project_version_id=project_version_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -135,7 +136,7 @@ async def retrieve( if not project_version_id: raise ValueError(f"Expected a non-empty value for `project_version_id` but received {project_version_id!r}") return await self._get( - f"/versions/{project_version_id}", + path_template("/versions/{project_version_id}", project_version_id=project_version_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), diff --git a/src/openlayer/resources/commits/test_results.py b/src/openlayer/resources/commits/test_results.py index 4c848588..f8e34dce 100644 --- a/src/openlayer/resources/commits/test_results.py +++ b/src/openlayer/resources/commits/test_results.py @@ -7,7 +7,7 @@ import httpx from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -88,7 +88,7 @@ def list( if not project_version_id: raise ValueError(f"Expected a non-empty value for `project_version_id` but received {project_version_id!r}") return self._get( - f"/versions/{project_version_id}/results", + path_template("/versions/{project_version_id}/results", project_version_id=project_version_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -172,7 +172,7 @@ async def list( if not project_version_id: raise ValueError(f"Expected a non-empty value for `project_version_id` but received {project_version_id!r}") return await self._get( - f"/versions/{project_version_id}/results", + path_template("/versions/{project_version_id}/results", project_version_id=project_version_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, diff --git a/src/openlayer/resources/inference_pipelines/data.py b/src/openlayer/resources/inference_pipelines/data.py index 9d2b6370..784414d0 100644 --- a/src/openlayer/resources/inference_pipelines/data.py +++ b/src/openlayer/resources/inference_pipelines/data.py @@ -7,7 +7,7 @@ import httpx from ..._types import Body, Query, Headers, NotGiven, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -78,7 +78,9 @@ def stream( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return self._post( - f"/inference-pipelines/{inference_pipeline_id}/data-stream", + path_template( + "/inference-pipelines/{inference_pipeline_id}/data-stream", inference_pipeline_id=inference_pipeline_id + ), body=maybe_transform( { "config": config, @@ -148,7 +150,9 @@ async def stream( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return await self._post( - f"/inference-pipelines/{inference_pipeline_id}/data-stream", + path_template( + "/inference-pipelines/{inference_pipeline_id}/data-stream", inference_pipeline_id=inference_pipeline_id + ), body=await async_maybe_transform( { "config": config, diff --git a/src/openlayer/resources/inference_pipelines/inference_pipelines.py b/src/openlayer/resources/inference_pipelines/inference_pipelines.py index 4fc649c0..9615453f 100644 --- a/src/openlayer/resources/inference_pipelines/inference_pipelines.py +++ b/src/openlayer/resources/inference_pipelines/inference_pipelines.py @@ -29,7 +29,7 @@ inference_pipeline_retrieve_users_params, ) from ..._types import Body, Omit, Query, Headers, NoneType, NotGiven, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -117,7 +117,7 @@ def retrieve( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return self._get( - f"/inference-pipelines/{inference_pipeline_id}", + path_template("/inference-pipelines/{inference_pipeline_id}", inference_pipeline_id=inference_pipeline_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -168,7 +168,7 @@ def update( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return self._put( - f"/inference-pipelines/{inference_pipeline_id}", + path_template("/inference-pipelines/{inference_pipeline_id}", inference_pipeline_id=inference_pipeline_id), body=maybe_transform( { "description": description, @@ -212,7 +212,7 @@ def delete( ) extra_headers = {"Accept": "*/*", **(extra_headers or {})} return self._delete( - f"/inference-pipelines/{inference_pipeline_id}", + path_template("/inference-pipelines/{inference_pipeline_id}", inference_pipeline_id=inference_pipeline_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -257,7 +257,9 @@ def retrieve_users( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return self._get( - f"/inference-pipelines/{inference_pipeline_id}/users", + path_template( + "/inference-pipelines/{inference_pipeline_id}/users", inference_pipeline_id=inference_pipeline_id + ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -338,7 +340,7 @@ async def retrieve( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return await self._get( - f"/inference-pipelines/{inference_pipeline_id}", + path_template("/inference-pipelines/{inference_pipeline_id}", inference_pipeline_id=inference_pipeline_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -389,7 +391,7 @@ async def update( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return await self._put( - f"/inference-pipelines/{inference_pipeline_id}", + path_template("/inference-pipelines/{inference_pipeline_id}", inference_pipeline_id=inference_pipeline_id), body=await async_maybe_transform( { "description": description, @@ -433,7 +435,7 @@ async def delete( ) extra_headers = {"Accept": "*/*", **(extra_headers or {})} return await self._delete( - f"/inference-pipelines/{inference_pipeline_id}", + path_template("/inference-pipelines/{inference_pipeline_id}", inference_pipeline_id=inference_pipeline_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -478,7 +480,9 @@ async def retrieve_users( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return await self._get( - f"/inference-pipelines/{inference_pipeline_id}/users", + path_template( + "/inference-pipelines/{inference_pipeline_id}/users", inference_pipeline_id=inference_pipeline_id + ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, diff --git a/src/openlayer/resources/inference_pipelines/rows.py b/src/openlayer/resources/inference_pipelines/rows.py index 4fa059f6..16426073 100644 --- a/src/openlayer/resources/inference_pipelines/rows.py +++ b/src/openlayer/resources/inference_pipelines/rows.py @@ -7,7 +7,7 @@ import httpx from ..._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -77,7 +77,9 @@ def update( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return self._put( - f"/inference-pipelines/{inference_pipeline_id}/rows", + path_template( + "/inference-pipelines/{inference_pipeline_id}/rows", inference_pipeline_id=inference_pipeline_id + ), body=maybe_transform( { "row": row, @@ -142,7 +144,9 @@ def list( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return self._post( - f"/inference-pipelines/{inference_pipeline_id}/rows", + path_template( + "/inference-pipelines/{inference_pipeline_id}/rows", inference_pipeline_id=inference_pipeline_id + ), body=maybe_transform( { "column_filters": column_filters, @@ -227,7 +231,9 @@ async def update( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return await self._put( - f"/inference-pipelines/{inference_pipeline_id}/rows", + path_template( + "/inference-pipelines/{inference_pipeline_id}/rows", inference_pipeline_id=inference_pipeline_id + ), body=await async_maybe_transform( { "row": row, @@ -292,7 +298,9 @@ async def list( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return await self._post( - f"/inference-pipelines/{inference_pipeline_id}/rows", + path_template( + "/inference-pipelines/{inference_pipeline_id}/rows", inference_pipeline_id=inference_pipeline_id + ), body=await async_maybe_transform( { "column_filters": column_filters, diff --git a/src/openlayer/resources/inference_pipelines/test_results.py b/src/openlayer/resources/inference_pipelines/test_results.py index 5344d554..9adaebe0 100644 --- a/src/openlayer/resources/inference_pipelines/test_results.py +++ b/src/openlayer/resources/inference_pipelines/test_results.py @@ -7,7 +7,7 @@ import httpx from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -87,7 +87,9 @@ def list( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return self._get( - f"/inference-pipelines/{inference_pipeline_id}/results", + path_template( + "/inference-pipelines/{inference_pipeline_id}/results", inference_pipeline_id=inference_pipeline_id + ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -169,7 +171,9 @@ async def list( f"Expected a non-empty value for `inference_pipeline_id` but received {inference_pipeline_id!r}" ) return await self._get( - f"/inference-pipelines/{inference_pipeline_id}/results", + path_template( + "/inference-pipelines/{inference_pipeline_id}/results", inference_pipeline_id=inference_pipeline_id + ), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, diff --git a/src/openlayer/resources/projects/commits.py b/src/openlayer/resources/projects/commits.py index 381d8b2d..09514938 100644 --- a/src/openlayer/resources/projects/commits.py +++ b/src/openlayer/resources/projects/commits.py @@ -7,7 +7,7 @@ import httpx from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -82,7 +82,7 @@ def create( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return self._post( - f"/projects/{project_id}/versions", + path_template("/projects/{project_id}/versions", project_id=project_id), body=maybe_transform( { "commit": commit, @@ -130,7 +130,7 @@ def list( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return self._get( - f"/projects/{project_id}/versions", + path_template("/projects/{project_id}/versions", project_id=project_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -206,7 +206,7 @@ async def create( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return await self._post( - f"/projects/{project_id}/versions", + path_template("/projects/{project_id}/versions", project_id=project_id), body=await async_maybe_transform( { "commit": commit, @@ -254,7 +254,7 @@ async def list( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return await self._get( - f"/projects/{project_id}/versions", + path_template("/projects/{project_id}/versions", project_id=project_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, diff --git a/src/openlayer/resources/projects/inference_pipelines.py b/src/openlayer/resources/projects/inference_pipelines.py index fbc1b9c5..0158cbc9 100644 --- a/src/openlayer/resources/projects/inference_pipelines.py +++ b/src/openlayer/resources/projects/inference_pipelines.py @@ -7,7 +7,7 @@ import httpx from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -79,7 +79,7 @@ def create( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return self._post( - f"/projects/{project_id}/inference-pipelines", + path_template("/projects/{project_id}/inference-pipelines", project_id=project_id), body=maybe_transform( { "description": description, @@ -131,7 +131,7 @@ def list( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return self._get( - f"/projects/{project_id}/inference-pipelines", + path_template("/projects/{project_id}/inference-pipelines", project_id=project_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -205,7 +205,7 @@ async def create( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return await self._post( - f"/projects/{project_id}/inference-pipelines", + path_template("/projects/{project_id}/inference-pipelines", project_id=project_id), body=await async_maybe_transform( { "description": description, @@ -257,7 +257,7 @@ async def list( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return await self._get( - f"/projects/{project_id}/inference-pipelines", + path_template("/projects/{project_id}/inference-pipelines", project_id=project_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, diff --git a/src/openlayer/resources/projects/projects.py b/src/openlayer/resources/projects/projects.py index 38e4379e..83ab2d8f 100644 --- a/src/openlayer/resources/projects/projects.py +++ b/src/openlayer/resources/projects/projects.py @@ -25,7 +25,7 @@ AsyncCommitsResourceWithStreamingResponse, ) from ..._types import Body, Omit, Query, Headers, NoneType, NotGiven, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -210,7 +210,7 @@ def delete( raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return self._delete( - f"/projects/{project_id}", + path_template("/projects/{project_id}", project_id=project_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -379,7 +379,7 @@ async def delete( raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return await self._delete( - f"/projects/{project_id}", + path_template("/projects/{project_id}", project_id=project_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), diff --git a/src/openlayer/resources/projects/tests.py b/src/openlayer/resources/projects/tests.py index ed102a19..ad54607d 100644 --- a/src/openlayer/resources/projects/tests.py +++ b/src/openlayer/resources/projects/tests.py @@ -8,7 +8,7 @@ import httpx from ..._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -170,7 +170,7 @@ def create( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return self._post( - f"/projects/{project_id}/tests", + path_template("/projects/{project_id}/tests", project_id=project_id), body=maybe_transform( { "description": description, @@ -226,7 +226,7 @@ def update( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return self._put( - f"/projects/{project_id}/tests", + path_template("/projects/{project_id}/tests", project_id=project_id), body=maybe_transform({"payloads": payloads}, test_update_params.TestUpdateParams), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout @@ -282,7 +282,7 @@ def list( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return self._get( - f"/projects/{project_id}/tests", + path_template("/projects/{project_id}/tests", project_id=project_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -447,7 +447,7 @@ async def create( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return await self._post( - f"/projects/{project_id}/tests", + path_template("/projects/{project_id}/tests", project_id=project_id), body=await async_maybe_transform( { "description": description, @@ -503,7 +503,7 @@ async def update( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return await self._put( - f"/projects/{project_id}/tests", + path_template("/projects/{project_id}/tests", project_id=project_id), body=await async_maybe_transform({"payloads": payloads}, test_update_params.TestUpdateParams), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout @@ -559,7 +559,7 @@ async def list( if not project_id: raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") return await self._get( - f"/projects/{project_id}/tests", + path_template("/projects/{project_id}/tests", project_id=project_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, diff --git a/src/openlayer/resources/tests.py b/src/openlayer/resources/tests.py index 6c5711fc..5f71a7c1 100644 --- a/src/openlayer/resources/tests.py +++ b/src/openlayer/resources/tests.py @@ -8,7 +8,7 @@ from ..types import test_evaluate_params, test_list_results_params from .._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given -from .._utils import maybe_transform, async_maybe_transform +from .._utils import path_template, maybe_transform, async_maybe_transform from .._compat import cached_property from .._resource import SyncAPIResource, AsyncAPIResource from .._response import ( @@ -88,7 +88,7 @@ def evaluate( if not test_id: raise ValueError(f"Expected a non-empty value for `test_id` but received {test_id!r}") return self._post( - f"/tests/{test_id}/evaluate", + path_template("/tests/{test_id}/evaluate", test_id=test_id), body=maybe_transform( { "end_timestamp": end_timestamp, @@ -154,7 +154,7 @@ def list_results( if not test_id: raise ValueError(f"Expected a non-empty value for `test_id` but received {test_id!r}") return self._get( - f"/tests/{test_id}/results", + path_template("/tests/{test_id}/results", test_id=test_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -240,7 +240,7 @@ async def evaluate( if not test_id: raise ValueError(f"Expected a non-empty value for `test_id` but received {test_id!r}") return await self._post( - f"/tests/{test_id}/evaluate", + path_template("/tests/{test_id}/evaluate", test_id=test_id), body=await async_maybe_transform( { "end_timestamp": end_timestamp, @@ -306,7 +306,7 @@ async def list_results( if not test_id: raise ValueError(f"Expected a non-empty value for `test_id` but received {test_id!r}") return await self._get( - f"/tests/{test_id}/results", + path_template("/tests/{test_id}/results", test_id=test_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, diff --git a/src/openlayer/resources/workspaces/api_keys.py b/src/openlayer/resources/workspaces/api_keys.py index 30c4bc3a..99a1d237 100644 --- a/src/openlayer/resources/workspaces/api_keys.py +++ b/src/openlayer/resources/workspaces/api_keys.py @@ -7,7 +7,7 @@ import httpx from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -72,7 +72,7 @@ def create( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return self._post( - f"/workspaces/{workspace_id}/api-keys", + path_template("/workspaces/{workspace_id}/api-keys", workspace_id=workspace_id), body=maybe_transform({"name": name}, api_key_create_params.APIKeyCreateParams), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout @@ -130,7 +130,7 @@ async def create( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return await self._post( - f"/workspaces/{workspace_id}/api-keys", + path_template("/workspaces/{workspace_id}/api-keys", workspace_id=workspace_id), body=await async_maybe_transform({"name": name}, api_key_create_params.APIKeyCreateParams), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout diff --git a/src/openlayer/resources/workspaces/invites.py b/src/openlayer/resources/workspaces/invites.py index 3e138f90..c7033989 100644 --- a/src/openlayer/resources/workspaces/invites.py +++ b/src/openlayer/resources/workspaces/invites.py @@ -7,7 +7,7 @@ import httpx from ..._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -74,7 +74,7 @@ def create( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return self._post( - f"/workspaces/{workspace_id}/invites", + path_template("/workspaces/{workspace_id}/invites", workspace_id=workspace_id), body=maybe_transform( { "emails": emails, @@ -120,7 +120,7 @@ def list( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return self._get( - f"/workspaces/{workspace_id}/invites", + path_template("/workspaces/{workspace_id}/invites", workspace_id=workspace_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -188,7 +188,7 @@ async def create( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return await self._post( - f"/workspaces/{workspace_id}/invites", + path_template("/workspaces/{workspace_id}/invites", workspace_id=workspace_id), body=await async_maybe_transform( { "emails": emails, @@ -234,7 +234,7 @@ async def list( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return await self._get( - f"/workspaces/{workspace_id}/invites", + path_template("/workspaces/{workspace_id}/invites", workspace_id=workspace_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, diff --git a/src/openlayer/resources/workspaces/workspaces.py b/src/openlayer/resources/workspaces/workspaces.py index cc529de0..2f5f8665 100644 --- a/src/openlayer/resources/workspaces/workspaces.py +++ b/src/openlayer/resources/workspaces/workspaces.py @@ -14,7 +14,7 @@ AsyncInvitesResourceWithStreamingResponse, ) from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given -from ..._utils import maybe_transform, async_maybe_transform +from ..._utils import path_template, maybe_transform, async_maybe_transform from .api_keys import ( APIKeysResource, AsyncAPIKeysResource, @@ -92,7 +92,7 @@ def retrieve( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return self._get( - f"/workspaces/{workspace_id}", + path_template("/workspaces/{workspace_id}", workspace_id=workspace_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -134,7 +134,7 @@ def update( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return self._put( - f"/workspaces/{workspace_id}", + path_template("/workspaces/{workspace_id}", workspace_id=workspace_id), body=maybe_transform( { "invite_code": invite_code, @@ -204,7 +204,7 @@ async def retrieve( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return await self._get( - f"/workspaces/{workspace_id}", + path_template("/workspaces/{workspace_id}", workspace_id=workspace_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -246,7 +246,7 @@ async def update( if not workspace_id: raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}") return await self._put( - f"/workspaces/{workspace_id}", + path_template("/workspaces/{workspace_id}", workspace_id=workspace_id), body=await async_maybe_transform( { "invite_code": invite_code, diff --git a/tests/test_utils/test_path.py b/tests/test_utils/test_path.py new file mode 100644 index 00000000..e3eedae6 --- /dev/null +++ b/tests/test_utils/test_path.py @@ -0,0 +1,89 @@ +from __future__ import annotations + +from typing import Any + +import pytest + +from openlayer._utils._path import path_template + + +@pytest.mark.parametrize( + "template, kwargs, expected", + [ + ("/v1/{id}", dict(id="abc"), "/v1/abc"), + ("/v1/{a}/{b}", dict(a="x", b="y"), "/v1/x/y"), + ("/v1/{a}{b}/path/{c}?val={d}#{e}", dict(a="x", b="y", c="z", d="u", e="v"), "/v1/xy/path/z?val=u#v"), + ("/{w}/{w}", dict(w="echo"), "/echo/echo"), + ("/v1/static", {}, "/v1/static"), + ("", {}, ""), + ("/v1/?q={n}&count=10", dict(n=42), "/v1/?q=42&count=10"), + ("/v1/{v}", dict(v=None), "/v1/null"), + ("/v1/{v}", dict(v=True), "/v1/true"), + ("/v1/{v}", dict(v=False), "/v1/false"), + ("/v1/{v}", dict(v=".hidden"), "/v1/.hidden"), # dot prefix ok + ("/v1/{v}", dict(v="file.txt"), "/v1/file.txt"), # dot in middle ok + ("/v1/{v}", dict(v="..."), "/v1/..."), # triple dot ok + ("/v1/{a}{b}", dict(a=".", b="txt"), "/v1/.txt"), # dot var combining with adjacent to be ok + ("/items?q={v}#{f}", dict(v=".", f=".."), "/items?q=.#.."), # dots in query/fragment are fine + ( + "/v1/{a}?query={b}", + dict(a="../../other/endpoint", b="a&bad=true"), + "/v1/..%2F..%2Fother%2Fendpoint?query=a%26bad%3Dtrue", + ), + ("/v1/{val}", dict(val="a/b/c"), "/v1/a%2Fb%2Fc"), + ("/v1/{val}", dict(val="a/b/c?query=value"), "/v1/a%2Fb%2Fc%3Fquery=value"), + ("/v1/{val}", dict(val="a/b/c?query=value&bad=true"), "/v1/a%2Fb%2Fc%3Fquery=value&bad=true"), + ("/v1/{val}", dict(val="%20"), "/v1/%2520"), # escapes escape sequences in input + # Query: slash and ? are safe, # is not + ("/items?q={v}", dict(v="a/b"), "/items?q=a/b"), + ("/items?q={v}", dict(v="a?b"), "/items?q=a?b"), + ("/items?q={v}", dict(v="a#b"), "/items?q=a%23b"), + ("/items?q={v}", dict(v="a b"), "/items?q=a%20b"), + # Fragment: slash and ? are safe + ("/docs#{v}", dict(v="a/b"), "/docs#a/b"), + ("/docs#{v}", dict(v="a?b"), "/docs#a?b"), + # Path: slash, ? and # are all encoded + ("/v1/{v}", dict(v="a/b"), "/v1/a%2Fb"), + ("/v1/{v}", dict(v="a?b"), "/v1/a%3Fb"), + ("/v1/{v}", dict(v="a#b"), "/v1/a%23b"), + # same var encoded differently by component + ( + "/v1/{v}?q={v}#{v}", + dict(v="a/b?c#d"), + "/v1/a%2Fb%3Fc%23d?q=a/b?c%23d#a/b?c%23d", + ), + ("/v1/{val}", dict(val="x?admin=true"), "/v1/x%3Fadmin=true"), # query injection + ("/v1/{val}", dict(val="x#admin"), "/v1/x%23admin"), # fragment injection + ], +) +def test_interpolation(template: str, kwargs: dict[str, Any], expected: str) -> None: + assert path_template(template, **kwargs) == expected + + +def test_missing_kwarg_raises_key_error() -> None: + with pytest.raises(KeyError, match="org_id"): + path_template("/v1/{org_id}") + + +@pytest.mark.parametrize( + "template, kwargs", + [ + ("{a}/path", dict(a=".")), + ("{a}/path", dict(a="..")), + ("/v1/{a}", dict(a=".")), + ("/v1/{a}", dict(a="..")), + ("/v1/{a}/path", dict(a=".")), + ("/v1/{a}/path", dict(a="..")), + ("/v1/{a}{b}", dict(a=".", b=".")), # adjacent vars → ".." + ("/v1/{a}.", dict(a=".")), # var + static → ".." + ("/v1/{a}{b}", dict(a="", b=".")), # empty + dot → "." + ("/v1/%2e/{x}", dict(x="ok")), # encoded dot in static text + ("/v1/%2e./{x}", dict(x="ok")), # mixed encoded ".." in static + ("/v1/.%2E/{x}", dict(x="ok")), # mixed encoded ".." in static + ("/v1/{v}?q=1", dict(v="..")), + ("/v1/{v}#frag", dict(v="..")), + ], +) +def test_dot_segment_rejected(template: str, kwargs: dict[str, Any]) -> None: + with pytest.raises(ValueError, match="dot-segment"): + path_template(template, **kwargs)