diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 97f05b2a..ebcf4606 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/steel-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 95ceb189..3824f4c4 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 6db19b95..4ad3fef3 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.17.0" + ".": "0.18.0" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index ab379643..2c7ba907 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,47 @@ # Changelog +## 0.18.0 (2026-04-23) + +Full Changelog: [v0.17.0...v0.18.0](https://github.com/steel-dev/steel-python/compare/v0.17.0...v0.18.0) + +### Features + +* **internal:** implement indices array format for query and form serialization ([5e7b0a2](https://github.com/steel-dev/steel-python/commit/5e7b0a243b6d3fd6299b60695e57ef709096e265)) + + +### Bug Fixes + +* **client:** preserve hardcoded query params when merging with user params ([a2ee006](https://github.com/steel-dev/steel-python/commit/a2ee00611edec4afc18dbe55707afcff37738b18)) +* **deps:** bump minimum typing-extensions version ([3815a72](https://github.com/steel-dev/steel-python/commit/3815a72732d737d7403f91fbec741c95dd5f89d5)) +* ensure file data are only sent as 1 parameter ([3d0485c](https://github.com/steel-dev/steel-python/commit/3d0485c4ab6faf9bf4cdf46b80f8bd5267a8b033)) +* **pydantic:** do not pass `by_alias` unless set ([f9686b4](https://github.com/steel-dev/steel-python/commit/f9686b47001a452f8591d9d65c7dd8c70dad5204)) +* sanitize endpoint path params ([abb806c](https://github.com/steel-dev/steel-python/commit/abb806c6ada0cb749bf734ada489eeff02738673)) + + +### Performance Improvements + +* **client:** optimize file structure copying in multipart requests ([fe678f2](https://github.com/steel-dev/steel-python/commit/fe678f2ed6cd4eac844a0c260c62e4fba9a84cbb)) + + +### Chores + +* **ci:** skip lint on metadata-only changes ([a90bcd7](https://github.com/steel-dev/steel-python/commit/a90bcd7ec396cded3006818beffe1c1f8780f6d1)) +* **internal:** more robust bootstrap script ([c6d65ae](https://github.com/steel-dev/steel-python/commit/c6d65ae060526ed85fc7e27040a921cad3c00196)) +* **internal:** tweak CI branches ([35a8acc](https://github.com/steel-dev/steel-python/commit/35a8acc7fd278d27398979892a10832291f74e9d)) +* **internal:** update gitignore ([4f0a06c](https://github.com/steel-dev/steel-python/commit/4f0a06c5eb8b1b88597e4d562de24284d4c9e2d3)) +* **tests:** bump steady to v0.19.4 ([fc582c4](https://github.com/steel-dev/steel-python/commit/fc582c4da1253d3851af25e753398ed6139cdac6)) +* **tests:** bump steady to v0.19.5 ([9927574](https://github.com/steel-dev/steel-python/commit/9927574b27a5d281732032176c9da052e7da5ea0)) +* **tests:** bump steady to v0.19.6 ([81717f3](https://github.com/steel-dev/steel-python/commit/81717f34046cb49e43a25db03d8b5bb153429284)) +* **tests:** bump steady to v0.19.7 ([4d06b71](https://github.com/steel-dev/steel-python/commit/4d06b71429d4341474d75d915d17bd597ebd42ec)) +* **tests:** bump steady to v0.20.1 ([793aca8](https://github.com/steel-dev/steel-python/commit/793aca80bd008a31fe47a9fff8380f2a9ab513f4)) +* **tests:** bump steady to v0.20.2 ([0be42f6](https://github.com/steel-dev/steel-python/commit/0be42f6789832bc33a87b8f5ce0a374cce1a4508)) +* **tests:** bump steady to v0.22.1 ([1fe72c6](https://github.com/steel-dev/steel-python/commit/1fe72c6a2ef34388e815315acbf93026c5727d5d)) + + +### Refactors + +* **tests:** switch from prism to steady ([791de4d](https://github.com/steel-dev/steel-python/commit/791de4d8baf003a3600e7e7a9422a36fc6729c12)) + ## 0.17.0 (2026-03-16) Full Changelog: [v0.16.0...v0.17.0](https://github.com/steel-dev/steel-python/compare/v0.16.0...v0.17.0) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fbd86d24..c0e917b1 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 8219a00b..c2b157d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "steel-sdk" -version = "0.17.0" +version = "0.18.0" description = "The official Python library for the steel 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/bootstrap b/scripts/bootstrap index b430fee3..fe8451e4 100755 --- a/scripts/bootstrap +++ b/scripts/bootstrap @@ -4,7 +4,7 @@ set -e cd "$(dirname "$0")/.." -if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "$SKIP_BREW" != "1" ] && [ -t 0 ]; then +if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "${SKIP_BREW:-}" != "1" ] && [ -t 0 ]; then brew bundle check >/dev/null 2>&1 || { echo -n "==> Install Homebrew dependencies? (y/N): " read -r response diff --git a/scripts/mock b/scripts/mock index bcf3b392..feebe5ed 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.22.1 -- steady --version - npm exec --package=@stainless-api/prism-cli@5.15.0 -- prism mock "$URL" &> .prism.log & + npm exec --package=@stdy/cli@0.22.1 -- 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.22.1 -- 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..19acc916 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.22.1 -- 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/steel/_base_client.py b/src/steel/_base_client.py index 278ca503..6db34d23 100644 --- a/src/steel/_base_client.py +++ b/src/steel/_base_client.py @@ -540,6 +540,10 @@ def _build_request( files = cast(HttpxRequestFiles, ForceMultipartDict()) prepared_url = self._prepare_url(options.url) + # preserve hard-coded query params from the url + if params and prepared_url.query: + params = {**dict(prepared_url.params.items()), **params} + prepared_url = prepared_url.copy_with(raw_path=prepared_url.raw_path.split(b"?", 1)[0]) if "_" in prepared_url.host: # work around https://github.com/encode/httpx/discussions/2880 kwargs["extensions"] = {"sni_hostname": prepared_url.host.replace("_", "-")} diff --git a/src/steel/_compat.py b/src/steel/_compat.py index 786ff42a..e6690a4f 100644 --- a/src/steel/_compat.py +++ b/src/steel/_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, @@ -149,7 +156,7 @@ def model_dump( exclude_defaults=exclude_defaults, # warnings are not supported in Pydantic v1 warnings=True if PYDANTIC_V1 else warnings, - by_alias=by_alias, + **kwargs, ) return cast( "dict[str, Any]", diff --git a/src/steel/_files.py b/src/steel/_files.py index b3386203..77e66753 100644 --- a/src/steel/_files.py +++ b/src/steel/_files.py @@ -3,8 +3,8 @@ import io import os import pathlib -from typing import overload -from typing_extensions import TypeGuard +from typing import Sequence, cast, overload +from typing_extensions import TypeVar, TypeGuard import anyio @@ -17,7 +17,9 @@ HttpxFileContent, HttpxRequestFiles, ) -from ._utils import is_tuple_t, is_mapping_t, is_sequence_t +from ._utils import is_list, is_mapping, is_tuple_t, is_mapping_t, is_sequence_t + +_T = TypeVar("_T") def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]: @@ -121,3 +123,51 @@ async def async_read_file_content(file: FileContent) -> HttpxFileContent: return await anyio.Path(file).read_bytes() return file + + +def deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]]) -> _T: + """Copy only the containers along the given paths. + + Used to guard against mutation by extract_files without copying the entire structure. + Only dicts and lists that lie on a path are copied; everything else + is returned by reference. + + For example, given paths=[["foo", "files", "file"]] and the structure: + { + "foo": { + "bar": {"baz": {}}, + "files": {"file": } + } + } + The root dict, "foo", and "files" are copied (they lie on the path). + "bar" and "baz" are returned by reference (off the path). + """ + return _deepcopy_with_paths(item, paths, 0) + + +def _deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]], index: int) -> _T: + if not paths: + return item + if is_mapping(item): + key_to_paths: dict[str, list[Sequence[str]]] = {} + for path in paths: + if index < len(path): + key_to_paths.setdefault(path[index], []).append(path) + + # if no path continues through this mapping, it won't be mutated and copying it is redundant + if not key_to_paths: + return item + + result = dict(item) + for key, subpaths in key_to_paths.items(): + if key in result: + result[key] = _deepcopy_with_paths(result[key], subpaths, index + 1) + return cast(_T, result) + if is_list(item): + array_paths = [path for path in paths if index < len(path) and path[index] == ""] + + # if no path expects a list here, nothing will be mutated inside it - return by reference + if not array_paths: + return cast(_T, item) + return cast(_T, [_deepcopy_with_paths(entry, array_paths, index + 1) for entry in item]) + return item diff --git a/src/steel/_qs.py b/src/steel/_qs.py index ada6fd3f..de8c99bc 100644 --- a/src/steel/_qs.py +++ b/src/steel/_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/steel/_utils/__init__.py b/src/steel/_utils/__init__.py index dc64e29a..1c090e51 100644 --- a/src/steel/_utils/__init__.py +++ b/src/steel/_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 ( @@ -23,7 +24,6 @@ coerce_integer as coerce_integer, file_from_path as file_from_path, strip_not_given as strip_not_given, - deepcopy_minimal as deepcopy_minimal, get_async_library as get_async_library, maybe_coerce_float as maybe_coerce_float, get_required_header as get_required_header, diff --git a/src/steel/_utils/_path.py b/src/steel/_utils/_path.py new file mode 100644 index 00000000..4d6e1e4c --- /dev/null +++ b/src/steel/_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/steel/_utils/_utils.py b/src/steel/_utils/_utils.py index eec7f4a1..771859f5 100644 --- a/src/steel/_utils/_utils.py +++ b/src/steel/_utils/_utils.py @@ -86,8 +86,9 @@ def _extract_items( index += 1 if is_dict(obj): try: - # We are at the last entry in the path so we must remove the field - if (len(path)) == index: + # Remove the field if there are no more dict keys in the path, + # only "" traversal markers or end. + if all(p == "" for p in path[index:]): item = obj.pop(key) else: item = obj[key] @@ -176,21 +177,6 @@ def is_iterable(obj: object) -> TypeGuard[Iterable[object]]: return isinstance(obj, Iterable) -def deepcopy_minimal(item: _T) -> _T: - """Minimal reimplementation of copy.deepcopy() that will only copy certain object types: - - - mappings, e.g. `dict` - - list - - This is done for performance reasons. - """ - if is_mapping(item): - return cast(_T, {k: deepcopy_minimal(v) for k, v in item.items()}) - if is_list(item): - return cast(_T, [deepcopy_minimal(entry) for entry in item]) - return item - - # copied from https://github.com/Rapptz/RoboDanny def human_join(seq: Sequence[str], *, delim: str = ", ", final: str = "or") -> str: size = len(seq) diff --git a/src/steel/_version.py b/src/steel/_version.py index 06fdcfc6..a9604d8d 100644 --- a/src/steel/_version.py +++ b/src/steel/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. __title__ = "steel" -__version__ = "0.17.0" # x-release-please-version +__version__ = "0.18.0" # x-release-please-version diff --git a/src/steel/resources/extensions.py b/src/steel/resources/extensions.py index 6f20af7a..5a3dc284 100644 --- a/src/steel/resources/extensions.py +++ b/src/steel/resources/extensions.py @@ -7,8 +7,9 @@ import httpx from ..types import extension_update_params, extension_upload_params +from .._files import deepcopy_with_paths from .._types import Body, Omit, Query, Headers, NotGiven, FileTypes, omit, not_given -from .._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform +from .._utils import extract_files, path_template, maybe_transform, async_maybe_transform from .._compat import cached_property from .._resource import SyncAPIResource, AsyncAPIResource from .._response import ( @@ -79,11 +80,12 @@ def update( """ if not extension_id: raise ValueError(f"Expected a non-empty value for `extension_id` but received {extension_id!r}") - body = deepcopy_minimal( + body = deepcopy_with_paths( { "file": file, "url": url, - } + }, + [["file"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be @@ -91,7 +93,7 @@ def update( # multipart/form-data; boundary=---abc-- extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} return self._put( - f"/v1/extensions/{extension_id}", + path_template("/v1/extensions/{extension_id}", extension_id=extension_id), body=maybe_transform(body, extension_update_params.ExtensionUpdateParams), files=files, options=make_request_options( @@ -145,7 +147,7 @@ def delete( if not extension_id: raise ValueError(f"Expected a non-empty value for `extension_id` but received {extension_id!r}") return self._delete( - f"/v1/extensions/{extension_id}", + path_template("/v1/extensions/{extension_id}", extension_id=extension_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -197,7 +199,7 @@ def download( if not extension_id: raise ValueError(f"Expected a non-empty value for `extension_id` but received {extension_id!r}") return self._get( - f"/v1/extensions/{extension_id}", + path_template("/v1/extensions/{extension_id}", extension_id=extension_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -233,11 +235,12 @@ def upload( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "file": file, "url": url, - } + }, + [["file"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be @@ -307,11 +310,12 @@ async def update( """ if not extension_id: raise ValueError(f"Expected a non-empty value for `extension_id` but received {extension_id!r}") - body = deepcopy_minimal( + body = deepcopy_with_paths( { "file": file, "url": url, - } + }, + [["file"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be @@ -319,7 +323,7 @@ async def update( # multipart/form-data; boundary=---abc-- extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} return await self._put( - f"/v1/extensions/{extension_id}", + path_template("/v1/extensions/{extension_id}", extension_id=extension_id), body=await async_maybe_transform(body, extension_update_params.ExtensionUpdateParams), files=files, options=make_request_options( @@ -373,7 +377,7 @@ async def delete( if not extension_id: raise ValueError(f"Expected a non-empty value for `extension_id` but received {extension_id!r}") return await self._delete( - f"/v1/extensions/{extension_id}", + path_template("/v1/extensions/{extension_id}", extension_id=extension_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -425,7 +429,7 @@ async def download( if not extension_id: raise ValueError(f"Expected a non-empty value for `extension_id` but received {extension_id!r}") return await self._get( - f"/v1/extensions/{extension_id}", + path_template("/v1/extensions/{extension_id}", extension_id=extension_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -461,11 +465,12 @@ async def upload( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "file": file, "url": url, - } + }, + [["file"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be diff --git a/src/steel/resources/files.py b/src/steel/resources/files.py index a2e72050..a5b2fc7f 100644 --- a/src/steel/resources/files.py +++ b/src/steel/resources/files.py @@ -7,8 +7,9 @@ import httpx from ..types import file_upload_params +from .._files import deepcopy_with_paths from .._types import Body, Omit, Query, Headers, NoneType, NotGiven, FileTypes, omit, not_given -from .._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform +from .._utils import extract_files, path_template, maybe_transform, async_maybe_transform from .._compat import cached_property from .._resource import SyncAPIResource, AsyncAPIResource from .._response import ( @@ -98,7 +99,7 @@ def delete( raise ValueError(f"Expected a non-empty value for `path` but received {path!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return self._delete( - f"/v1/files/{path}", + path_template("/v1/files/{path}", path=path), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -132,7 +133,7 @@ def download( raise ValueError(f"Expected a non-empty value for `path` but received {path!r}") extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})} return self._get( - f"/v1/files/{path}", + path_template("/v1/files/{path}", path=path), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -169,11 +170,12 @@ def upload( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "file": file, "path": path, - } + }, + [["file"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be @@ -257,7 +259,7 @@ async def delete( raise ValueError(f"Expected a non-empty value for `path` but received {path!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return await self._delete( - f"/v1/files/{path}", + path_template("/v1/files/{path}", path=path), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -291,7 +293,7 @@ async def download( raise ValueError(f"Expected a non-empty value for `path` but received {path!r}") extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})} return await self._get( - f"/v1/files/{path}", + path_template("/v1/files/{path}", path=path), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -328,11 +330,12 @@ async def upload( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "file": file, "path": path, - } + }, + [["file"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be diff --git a/src/steel/resources/profiles.py b/src/steel/resources/profiles.py index 3e149507..1a7f838e 100644 --- a/src/steel/resources/profiles.py +++ b/src/steel/resources/profiles.py @@ -7,8 +7,9 @@ import httpx from ..types import profile_create_params, profile_update_params +from .._files import deepcopy_with_paths from .._types import Body, Omit, Query, Headers, NotGiven, FileTypes, omit, not_given -from .._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform +from .._utils import extract_files, path_template, maybe_transform, async_maybe_transform from .._compat import cached_property from .._resource import SyncAPIResource, AsyncAPIResource from .._response import ( @@ -80,13 +81,14 @@ def create( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "user_data_dir": user_data_dir, "dimensions": dimensions, "proxy_url": proxy_url, "user_agent": user_agent, - } + }, + [["userDataDir"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["userDataDir"]]) # It should be noted that the actual Content-Type header that will be @@ -140,13 +142,14 @@ def update( """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") - body = deepcopy_minimal( + body = deepcopy_with_paths( { "user_data_dir": user_data_dir, "dimensions": dimensions, "proxy_url": proxy_url, "user_agent": user_agent, - } + }, + [["userDataDir"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["userDataDir"]]) # It should be noted that the actual Content-Type header that will be @@ -154,7 +157,7 @@ def update( # multipart/form-data; boundary=---abc-- extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} return self._patch( - f"/v1/profiles/{id}", + path_template("/v1/profiles/{id}", id=id), body=maybe_transform(body, profile_update_params.ProfileUpdateParams), files=files, options=make_request_options( @@ -208,7 +211,7 @@ def get( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._get( - f"/v1/profiles/{id}", + path_template("/v1/profiles/{id}", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -270,13 +273,14 @@ async def create( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "user_data_dir": user_data_dir, "dimensions": dimensions, "proxy_url": proxy_url, "user_agent": user_agent, - } + }, + [["userDataDir"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["userDataDir"]]) # It should be noted that the actual Content-Type header that will be @@ -330,13 +334,14 @@ async def update( """ if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") - body = deepcopy_minimal( + body = deepcopy_with_paths( { "user_data_dir": user_data_dir, "dimensions": dimensions, "proxy_url": proxy_url, "user_agent": user_agent, - } + }, + [["userDataDir"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["userDataDir"]]) # It should be noted that the actual Content-Type header that will be @@ -344,7 +349,7 @@ async def update( # multipart/form-data; boundary=---abc-- extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} return await self._patch( - f"/v1/profiles/{id}", + path_template("/v1/profiles/{id}", id=id), body=await async_maybe_transform(body, profile_update_params.ProfileUpdateParams), files=files, options=make_request_options( @@ -398,7 +403,7 @@ async def get( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._get( - f"/v1/profiles/{id}", + path_template("/v1/profiles/{id}", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), diff --git a/src/steel/resources/sessions/captchas.py b/src/steel/resources/sessions/captchas.py index 638dc058..abca5e2d 100644 --- a/src/steel/resources/sessions/captchas.py +++ b/src/steel/resources/sessions/captchas.py @@ -5,7 +5,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 ( @@ -81,7 +81,7 @@ def solve( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return self._post( - f"/v1/sessions/{session_id}/captchas/solve", + path_template("/v1/sessions/{session_id}/captchas/solve", session_id=session_id), body=maybe_transform( { "page_id": page_id, @@ -131,7 +131,7 @@ def solve_image( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return self._post( - f"/v1/sessions/{session_id}/captchas/solve-image", + path_template("/v1/sessions/{session_id}/captchas/solve-image", session_id=session_id), body=maybe_transform( { "image_x_path": image_x_path, @@ -172,7 +172,7 @@ def status( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return self._get( - f"/v1/sessions/{session_id}/captchas/status", + path_template("/v1/sessions/{session_id}/captchas/status", session_id=session_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -238,7 +238,7 @@ async def solve( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return await self._post( - f"/v1/sessions/{session_id}/captchas/solve", + path_template("/v1/sessions/{session_id}/captchas/solve", session_id=session_id), body=await async_maybe_transform( { "page_id": page_id, @@ -288,7 +288,7 @@ async def solve_image( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return await self._post( - f"/v1/sessions/{session_id}/captchas/solve-image", + path_template("/v1/sessions/{session_id}/captchas/solve-image", session_id=session_id), body=await async_maybe_transform( { "image_x_path": image_x_path, @@ -329,7 +329,7 @@ async def status( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return await self._get( - f"/v1/sessions/{session_id}/captchas/status", + path_template("/v1/sessions/{session_id}/captchas/status", session_id=session_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), diff --git a/src/steel/resources/sessions/files.py b/src/steel/resources/sessions/files.py index ea7b2822..d8424abe 100644 --- a/src/steel/resources/sessions/files.py +++ b/src/steel/resources/sessions/files.py @@ -6,8 +6,9 @@ import httpx +from ..._files import deepcopy_with_paths from ..._types import Body, Omit, Query, Headers, NoneType, NotGiven, FileTypes, omit, not_given -from ..._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform +from ..._utils import extract_files, path_template, maybe_transform, async_maybe_transform from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource from ..._response import ( @@ -78,7 +79,7 @@ def list( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return self._get( - f"/v1/sessions/{session_id}/files", + path_template("/v1/sessions/{session_id}/files", session_id=session_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -115,7 +116,7 @@ def delete( raise ValueError(f"Expected a non-empty value for `path` but received {path!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return self._delete( - f"/v1/sessions/{session_id}/files/{path}", + path_template("/v1/sessions/{session_id}/files/{path}", session_id=session_id, path=path), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -149,7 +150,7 @@ def delete_all( raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return self._delete( - f"/v1/sessions/{session_id}/files", + path_template("/v1/sessions/{session_id}/files", session_id=session_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -186,7 +187,7 @@ def download( raise ValueError(f"Expected a non-empty value for `path` but received {path!r}") extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})} return self._get( - f"/v1/sessions/{session_id}/files/{path}", + path_template("/v1/sessions/{session_id}/files/{path}", session_id=session_id, path=path), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -220,7 +221,7 @@ def download_archive( raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") extra_headers = {"Accept": "application/zip", **(extra_headers or {})} return self._get( - f"/v1/sessions/{session_id}/files.zip", + path_template("/v1/sessions/{session_id}/files.zip", session_id=session_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -260,11 +261,12 @@ def upload( """ if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") - body = deepcopy_minimal( + body = deepcopy_with_paths( { "file": file, "path": path, - } + }, + [["file"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be @@ -272,7 +274,7 @@ def upload( # multipart/form-data; boundary=---abc-- extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} return self._post( - f"/v1/sessions/{session_id}/files", + path_template("/v1/sessions/{session_id}/files", session_id=session_id), body=maybe_transform(body, file_upload_params.FileUploadParams), files=files, options=make_request_options( @@ -328,7 +330,7 @@ async def list( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return await self._get( - f"/v1/sessions/{session_id}/files", + path_template("/v1/sessions/{session_id}/files", session_id=session_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -365,7 +367,7 @@ async def delete( raise ValueError(f"Expected a non-empty value for `path` but received {path!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return await self._delete( - f"/v1/sessions/{session_id}/files/{path}", + path_template("/v1/sessions/{session_id}/files/{path}", session_id=session_id, path=path), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -399,7 +401,7 @@ async def delete_all( raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") extra_headers = {"Accept": "*/*", **(extra_headers or {})} return await self._delete( - f"/v1/sessions/{session_id}/files", + path_template("/v1/sessions/{session_id}/files", session_id=session_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -436,7 +438,7 @@ async def download( raise ValueError(f"Expected a non-empty value for `path` but received {path!r}") extra_headers = {"Accept": "application/octet-stream", **(extra_headers or {})} return await self._get( - f"/v1/sessions/{session_id}/files/{path}", + path_template("/v1/sessions/{session_id}/files/{path}", session_id=session_id, path=path), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -470,7 +472,7 @@ async def download_archive( raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") extra_headers = {"Accept": "application/zip", **(extra_headers or {})} return await self._get( - f"/v1/sessions/{session_id}/files.zip", + path_template("/v1/sessions/{session_id}/files.zip", session_id=session_id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -510,11 +512,12 @@ async def upload( """ if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") - body = deepcopy_minimal( + body = deepcopy_with_paths( { "file": file, "path": path, - } + }, + [["file"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["file"]]) # It should be noted that the actual Content-Type header that will be @@ -522,7 +525,7 @@ async def upload( # multipart/form-data; boundary=---abc-- extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})} return await self._post( - f"/v1/sessions/{session_id}/files", + path_template("/v1/sessions/{session_id}/files", session_id=session_id), body=await async_maybe_transform(body, file_upload_params.FileUploadParams), files=files, options=make_request_options( diff --git a/src/steel/resources/sessions/sessions.py b/src/steel/resources/sessions/sessions.py index d54ca834..e66c8b84 100644 --- a/src/steel/resources/sessions/sessions.py +++ b/src/steel/resources/sessions/sessions.py @@ -17,7 +17,7 @@ ) from ...types import session_list_params, session_create_params, session_events_params, session_computer_params from ..._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given -from ..._utils import required_args, maybe_transform, async_maybe_transform +from ..._utils import path_template, required_args, maybe_transform, async_maybe_transform from .captchas import ( CaptchasResource, AsyncCaptchasResource, @@ -238,7 +238,7 @@ def retrieve( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._get( - f"/v1/sessions/{id}", + path_template("/v1/sessions/{id}", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -658,7 +658,7 @@ def computer( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return self._post( - f"/v1/sessions/{session_id}/computer", + path_template("/v1/sessions/{session_id}/computer", session_id=session_id), body=maybe_transform( { "action": action, @@ -709,7 +709,7 @@ def context( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._get( - f"/v1/sessions/{id}/context", + path_template("/v1/sessions/{id}/context", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -751,7 +751,7 @@ def events( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._get( - f"/v1/sessions/{id}/events", + path_template("/v1/sessions/{id}/events", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -795,7 +795,7 @@ def live_details( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._get( - f"/v1/sessions/{id}/live-details", + path_template("/v1/sessions/{id}/live-details", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -828,7 +828,7 @@ def release( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return self._post( - f"/v1/sessions/{id}/release", + path_template("/v1/sessions/{id}/release", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -1045,7 +1045,7 @@ async def retrieve( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._get( - f"/v1/sessions/{id}", + path_template("/v1/sessions/{id}", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -1465,7 +1465,7 @@ async def computer( if not session_id: raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}") return await self._post( - f"/v1/sessions/{session_id}/computer", + path_template("/v1/sessions/{session_id}/computer", session_id=session_id), body=await async_maybe_transform( { "action": action, @@ -1516,7 +1516,7 @@ async def context( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._get( - f"/v1/sessions/{id}/context", + path_template("/v1/sessions/{id}/context", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -1558,7 +1558,7 @@ async def events( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._get( - f"/v1/sessions/{id}/events", + path_template("/v1/sessions/{id}/events", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, @@ -1602,7 +1602,7 @@ async def live_details( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._get( - f"/v1/sessions/{id}/live-details", + path_template("/v1/sessions/{id}/live-details", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), @@ -1635,7 +1635,7 @@ async def release( if not id: raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") return await self._post( - f"/v1/sessions/{id}/release", + path_template("/v1/sessions/{id}/release", id=id), options=make_request_options( extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout ), diff --git a/tests/test_client.py b/tests/test_client.py index 410bcac6..22baf136 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -392,6 +392,30 @@ def test_default_query_option(self) -> None: client.close() + def test_hardcoded_query_params_in_url(self, client: Steel) -> None: + request = client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true")) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true"} + + request = client._build_request( + FinalRequestOptions( + method="get", + url="/foo?beta=true", + params={"limit": "10", "page": "abc"}, + ) + ) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true", "limit": "10", "page": "abc"} + + request = client._build_request( + FinalRequestOptions( + method="get", + url="/files/a%2Fb?beta=true", + params={"limit": "10"}, + ) + ) + assert request.url.raw_path == b"/files/a%2Fb?beta=true&limit=10" + def test_request_extra_json(self, client: Steel) -> None: request = client._build_request( FinalRequestOptions( @@ -1241,6 +1265,30 @@ async def test_default_query_option(self) -> None: await client.close() + async def test_hardcoded_query_params_in_url(self, async_client: AsyncSteel) -> None: + request = async_client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true")) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true"} + + request = async_client._build_request( + FinalRequestOptions( + method="get", + url="/foo?beta=true", + params={"limit": "10", "page": "abc"}, + ) + ) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true", "limit": "10", "page": "abc"} + + request = async_client._build_request( + FinalRequestOptions( + method="get", + url="/files/a%2Fb?beta=true", + params={"limit": "10"}, + ) + ) + assert request.url.raw_path == b"/files/a%2Fb?beta=true&limit=10" + def test_request_extra_json(self, client: Steel) -> None: request = client._build_request( FinalRequestOptions( diff --git a/tests/test_deepcopy.py b/tests/test_deepcopy.py deleted file mode 100644 index 8b0c8442..00000000 --- a/tests/test_deepcopy.py +++ /dev/null @@ -1,58 +0,0 @@ -from steel._utils import deepcopy_minimal - - -def assert_different_identities(obj1: object, obj2: object) -> None: - assert obj1 == obj2 - assert id(obj1) != id(obj2) - - -def test_simple_dict() -> None: - obj1 = {"foo": "bar"} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - - -def test_nested_dict() -> None: - obj1 = {"foo": {"bar": True}} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1["foo"], obj2["foo"]) - - -def test_complex_nested_dict() -> None: - obj1 = {"foo": {"bar": [{"hello": "world"}]}} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1["foo"], obj2["foo"]) - assert_different_identities(obj1["foo"]["bar"], obj2["foo"]["bar"]) - assert_different_identities(obj1["foo"]["bar"][0], obj2["foo"]["bar"][0]) - - -def test_simple_list() -> None: - obj1 = ["a", "b", "c"] - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - - -def test_nested_list() -> None: - obj1 = ["a", [1, 2, 3]] - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1[1], obj2[1]) - - -class MyObject: ... - - -def test_ignores_other_types() -> None: - # custom classes - my_obj = MyObject() - obj1 = {"foo": my_obj} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert obj1["foo"] is my_obj - - # tuples - obj3 = ("a", "b") - obj4 = deepcopy_minimal(obj3) - assert obj3 is obj4 diff --git a/tests/test_extract_files.py b/tests/test_extract_files.py index 6197514b..97ae9499 100644 --- a/tests/test_extract_files.py +++ b/tests/test_extract_files.py @@ -35,6 +35,15 @@ def test_multiple_files() -> None: assert query == {"documents": [{}, {}]} +def test_top_level_file_array() -> None: + query = {"files": [b"file one", b"file two"], "title": "hello"} + assert extract_files(query, paths=[["files", ""]]) == [ + ("files[]", b"file one"), + ("files[]", b"file two"), + ] + assert query == {"title": "hello"} + + @pytest.mark.parametrize( "query,paths,expected", [ diff --git a/tests/test_files.py b/tests/test_files.py index ae0e733c..722fb751 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -4,7 +4,8 @@ import pytest from dirty_equals import IsDict, IsList, IsBytes, IsTuple -from steel._files import to_httpx_files, async_to_httpx_files +from steel._files import to_httpx_files, deepcopy_with_paths, async_to_httpx_files +from steel._utils import extract_files readme_path = Path(__file__).parent.parent.joinpath("README.md") @@ -49,3 +50,99 @@ def test_string_not_allowed() -> None: "file": "foo", # type: ignore } ) + + +def assert_different_identities(obj1: object, obj2: object) -> None: + assert obj1 == obj2 + assert obj1 is not obj2 + + +class TestDeepcopyWithPaths: + def test_copies_top_level_dict(self) -> None: + original = {"file": b"data", "other": "value"} + result = deepcopy_with_paths(original, [["file"]]) + assert_different_identities(result, original) + + def test_file_value_is_same_reference(self) -> None: + file_bytes = b"contents" + original = {"file": file_bytes} + result = deepcopy_with_paths(original, [["file"]]) + assert_different_identities(result, original) + assert result["file"] is file_bytes + + def test_list_popped_wholesale(self) -> None: + files = [b"f1", b"f2"] + original = {"files": files, "title": "t"} + result = deepcopy_with_paths(original, [["files", ""]]) + assert_different_identities(result, original) + result_files = result["files"] + assert isinstance(result_files, list) + assert_different_identities(result_files, files) + + def test_nested_array_path_copies_list_and_elements(self) -> None: + elem1 = {"file": b"f1", "extra": 1} + elem2 = {"file": b"f2", "extra": 2} + original = {"items": [elem1, elem2]} + result = deepcopy_with_paths(original, [["items", "", "file"]]) + assert_different_identities(result, original) + result_items = result["items"] + assert isinstance(result_items, list) + assert_different_identities(result_items, original["items"]) + assert_different_identities(result_items[0], elem1) + assert_different_identities(result_items[1], elem2) + + def test_empty_paths_returns_same_object(self) -> None: + original = {"foo": "bar"} + result = deepcopy_with_paths(original, []) + assert result is original + + def test_multiple_paths(self) -> None: + f1 = b"file1" + f2 = b"file2" + original = {"a": f1, "b": f2, "c": "unchanged"} + result = deepcopy_with_paths(original, [["a"], ["b"]]) + assert_different_identities(result, original) + assert result["a"] is f1 + assert result["b"] is f2 + assert result["c"] is original["c"] + + def test_extract_files_does_not_mutate_original_top_level(self) -> None: + file_bytes = b"contents" + original = {"file": file_bytes, "other": "value"} + + copied = deepcopy_with_paths(original, [["file"]]) + extracted = extract_files(copied, paths=[["file"]]) + + assert extracted == [("file", file_bytes)] + assert original == {"file": file_bytes, "other": "value"} + assert copied == {"other": "value"} + + def test_extract_files_does_not_mutate_original_nested_array_path(self) -> None: + file1 = b"f1" + file2 = b"f2" + original = { + "items": [ + {"file": file1, "extra": 1}, + {"file": file2, "extra": 2}, + ], + "title": "example", + } + + copied = deepcopy_with_paths(original, [["items", "", "file"]]) + extracted = extract_files(copied, paths=[["items", "", "file"]]) + + assert extracted == [("items[][file]", file1), ("items[][file]", file2)] + assert original == { + "items": [ + {"file": file1, "extra": 1}, + {"file": file2, "extra": 2}, + ], + "title": "example", + } + assert copied == { + "items": [ + {"extra": 1}, + {"extra": 2}, + ], + "title": "example", + } diff --git a/tests/test_utils/test_path.py b/tests/test_utils/test_path.py new file mode 100644 index 00000000..55e06b12 --- /dev/null +++ b/tests/test_utils/test_path.py @@ -0,0 +1,89 @@ +from __future__ import annotations + +from typing import Any + +import pytest + +from steel._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)