Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 74
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/togetherai%2Ftogetherai-31893d157d3c85caa1d8615b73a5fa431ea2cc126bd2410e0f84f3defd5c7dec.yml
openapi_spec_hash: b652a4d504b4a3dbf585ab803b0f59fc
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/togetherai%2Ftogetherai-607d55fc5cb3aef533d26c8d23cad27948a494cc847df66650004f72905dcdc1.yml
openapi_spec_hash: 77793253c4dfc99f5bf1db9103213740
config_hash: 52d213100a0ca1a4b2cdcd2718936b51
1 change: 1 addition & 0 deletions src/together/_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
127 changes: 127 additions & 0 deletions src/together/_utils/_path.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions src/together/lib/cli/api/beta/jig/jig.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def validate(value: Any, value_type: type, path: str = "") -> str | None:
return err
return None

if origin is Union or origin is getattr(types, "UnionType", None):
if origin is Union or origin is getattr(types, "UnionType", Union):
errs = [validate(value, a, path) for a in args if a is not type(None)]
if not all(errs):
return None
Expand Down Expand Up @@ -505,7 +505,7 @@ def state(self) -> State:
def registry(self) -> str:
"""Get registry and namespace for current user"""
if not self.state.registry_base_path:
response = self.together.get("/image-repositories/base-path", cast_to=dict[str, str])
response = self.together.get("/image-repositories/base-path", cast_to=typing.Dict[str, str])
# strip protocol for docker image format
self.state.registry_base_path = response["base-path"].split("://", 1)[-1]
self.state.save()
Expand Down Expand Up @@ -890,6 +890,9 @@ def _command(f: Callable[..., Any]) -> Callable[..., Any]:
@click.option("-c", "--config", "config_path", default=None, help="Configuration file path")
@wraps(f)
def wrapper(ctx: Context, config_path: str | None, *args: Any, **kwargs: Any) -> None:
if sys.version_info < (3, 10):
click.secho("Jig requires Python 3.10+", fg="red")
raise Exit(1)
try:
result = f(Jig(ctx.obj, config_path), *args, **kwargs)
except (Exit, click.Abort, click.ClickException):
Expand Down
10 changes: 5 additions & 5 deletions src/together/resources/batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ..types import batch_create_params
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 (
Expand Down Expand Up @@ -126,7 +126,7 @@ def retrieve(
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
return self._get(
f"/batches/{id}",
path_template("/batches/{id}", id=id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -180,7 +180,7 @@ def cancel(
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
return self._post(
f"/batches/{id}/cancel",
path_template("/batches/{id}/cancel", id=id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -291,7 +291,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"/batches/{id}",
path_template("/batches/{id}", id=id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -345,7 +345,7 @@ async def cancel(
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
return await self._post(
f"/batches/{id}/cancel",
path_template("/batches/{id}/cancel", id=id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down
14 changes: 7 additions & 7 deletions src/together/resources/beta/clusters/clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
AsyncStorageResourceWithStreamingResponse,
)
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 (
Expand Down Expand Up @@ -170,7 +170,7 @@ def retrieve(
if not cluster_id:
raise ValueError(f"Expected a non-empty value for `cluster_id` but received {cluster_id!r}")
return self._get(
f"/compute/clusters/{cluster_id}",
path_template("/compute/clusters/{cluster_id}", cluster_id=cluster_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -212,7 +212,7 @@ def update(
if not cluster_id:
raise ValueError(f"Expected a non-empty value for `cluster_id` but received {cluster_id!r}")
return self._put(
f"/compute/clusters/{cluster_id}",
path_template("/compute/clusters/{cluster_id}", cluster_id=cluster_id),
body=maybe_transform(
{
"cluster_type": cluster_type,
Expand Down Expand Up @@ -273,7 +273,7 @@ def delete(
if not cluster_id:
raise ValueError(f"Expected a non-empty value for `cluster_id` but received {cluster_id!r}")
return self._delete(
f"/compute/clusters/{cluster_id}",
path_template("/compute/clusters/{cluster_id}", cluster_id=cluster_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -436,7 +436,7 @@ async def retrieve(
if not cluster_id:
raise ValueError(f"Expected a non-empty value for `cluster_id` but received {cluster_id!r}")
return await self._get(
f"/compute/clusters/{cluster_id}",
path_template("/compute/clusters/{cluster_id}", cluster_id=cluster_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -478,7 +478,7 @@ async def update(
if not cluster_id:
raise ValueError(f"Expected a non-empty value for `cluster_id` but received {cluster_id!r}")
return await self._put(
f"/compute/clusters/{cluster_id}",
path_template("/compute/clusters/{cluster_id}", cluster_id=cluster_id),
body=await async_maybe_transform(
{
"cluster_type": cluster_type,
Expand Down Expand Up @@ -539,7 +539,7 @@ async def delete(
if not cluster_id:
raise ValueError(f"Expected a non-empty value for `cluster_id` but received {cluster_id!r}")
return await self._delete(
f"/compute/clusters/{cluster_id}",
path_template("/compute/clusters/{cluster_id}", cluster_id=cluster_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down
10 changes: 5 additions & 5 deletions src/together/resources/beta/clusters/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -122,7 +122,7 @@ def retrieve(
if not volume_id:
raise ValueError(f"Expected a non-empty value for `volume_id` but received {volume_id!r}")
return self._get(
f"/compute/clusters/storage/volumes/{volume_id}",
path_template("/compute/clusters/storage/volumes/{volume_id}", volume_id=volume_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -221,7 +221,7 @@ def delete(
if not volume_id:
raise ValueError(f"Expected a non-empty value for `volume_id` but received {volume_id!r}")
return self._delete(
f"/compute/clusters/storage/volumes/{volume_id}",
path_template("/compute/clusters/storage/volumes/{volume_id}", volume_id=volume_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -328,7 +328,7 @@ async def retrieve(
if not volume_id:
raise ValueError(f"Expected a non-empty value for `volume_id` but received {volume_id!r}")
return await self._get(
f"/compute/clusters/storage/volumes/{volume_id}",
path_template("/compute/clusters/storage/volumes/{volume_id}", volume_id=volume_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down Expand Up @@ -427,7 +427,7 @@ async def delete(
if not volume_id:
raise ValueError(f"Expected a non-empty value for `volume_id` but received {volume_id!r}")
return await self._delete(
f"/compute/clusters/storage/volumes/{volume_id}",
path_template("/compute/clusters/storage/volumes/{volume_id}", volume_id=volume_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
Expand Down
Loading
Loading