Skip to content

Commit 6a9a740

Browse files
feat(api): OpenAPI spec update via Stainless API (#1059)
1 parent 77fe51a commit 6a9a740

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

.stats.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
configured_endpoints: 1348
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-ad39d8181627a820e39f80ef9591f6b22b652379be11d473f3840201d66eba38.yml
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-36a9d717773ebb507fd0744af578aa64b697030857c602c77458156a911fcab9.yml

src/cloudflare/_base_client.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
RequestOptions,
6161
ModelBuilderProtocol,
6262
)
63-
from ._utils import is_dict, is_list, is_given, lru_cache, is_mapping
63+
from ._utils import is_dict, is_list, asyncify, is_given, lru_cache, is_mapping
6464
from ._compat import model_copy, model_dump
6565
from ._models import GenericModel, FinalRequestOptions, validate_type, construct_type
6666
from ._response import (
@@ -358,6 +358,7 @@ def __init__(
358358
self._custom_query = custom_query or {}
359359
self._strict_response_validation = _strict_response_validation
360360
self._idempotency_header = None
361+
self._platform: Platform | None = None
361362

362363
if max_retries is None: # pyright: ignore[reportUnnecessaryComparison]
363364
raise TypeError(
@@ -622,7 +623,10 @@ def base_url(self, url: URL | str) -> None:
622623
self._base_url = self._enforce_trailing_slash(url if isinstance(url, URL) else URL(url))
623624

624625
def platform_headers(self) -> Dict[str, str]:
625-
return platform_headers(self._version)
626+
# the actual implementation is in a separate `lru_cache` decorated
627+
# function because adding `lru_cache` to methods will leak memory
628+
# https://github.com/python/cpython/issues/88476
629+
return platform_headers(self._version, platform=self._platform)
626630

627631
def _parse_retry_after_header(self, response_headers: Optional[httpx.Headers] = None) -> float | None:
628632
"""Returns a float of the number of seconds (not milliseconds) to wait after retrying, or None if unspecified.
@@ -1498,6 +1502,11 @@ async def _request(
14981502
stream_cls: type[_AsyncStreamT] | None,
14991503
remaining_retries: int | None,
15001504
) -> ResponseT | _AsyncStreamT:
1505+
if self._platform is None:
1506+
# `get_platform` can make blocking IO calls so we
1507+
# execute it earlier while we are in an async context
1508+
self._platform = await asyncify(get_platform)()
1509+
15011510
cast_to = self._maybe_override_cast_to(cast_to, options)
15021511
await self._prepare_options(options)
15031512

@@ -1921,11 +1930,11 @@ def get_platform() -> Platform:
19211930

19221931

19231932
@lru_cache(maxsize=None)
1924-
def platform_headers(version: str) -> Dict[str, str]:
1933+
def platform_headers(version: str, *, platform: Platform | None) -> Dict[str, str]:
19251934
return {
19261935
"X-Stainless-Lang": "python",
19271936
"X-Stainless-Package-Version": version,
1928-
"X-Stainless-OS": str(get_platform()),
1937+
"X-Stainless-OS": str(platform or get_platform()),
19291938
"X-Stainless-Arch": str(get_architecture()),
19301939
"X-Stainless-Runtime": get_python_runtime(),
19311940
"X-Stainless-Runtime-Version": get_python_version(),

src/cloudflare/_utils/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@
4949
maybe_transform as maybe_transform,
5050
async_maybe_transform as async_maybe_transform,
5151
)
52+
from ._reflection import function_has_argument as function_has_argument

src/cloudflare/_utils/_reflection.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import inspect
2+
from typing import Any, Callable
3+
4+
5+
def function_has_argument(func: Callable[..., Any], arg_name: str) -> bool:
6+
"""Returns whether or not the given function has a specific parameter"""
7+
sig = inspect.signature(func)
8+
return arg_name in sig.parameters

src/cloudflare/_utils/_sync.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import anyio
88
import anyio.to_thread
99

10+
from ._reflection import function_has_argument
11+
1012
T_Retval = TypeVar("T_Retval")
1113
T_ParamSpec = ParamSpec("T_ParamSpec")
1214

@@ -59,6 +61,21 @@ def do_work(arg1, arg2, kwarg1="", kwarg2="") -> str:
5961

6062
async def wrapper(*args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs) -> T_Retval:
6163
partial_f = functools.partial(function, *args, **kwargs)
62-
return await anyio.to_thread.run_sync(partial_f, cancellable=cancellable, limiter=limiter)
64+
65+
# In `v4.1.0` anyio added the `abandon_on_cancel` argument and deprecated the old
66+
# `cancellable` argument, so we need to use the new `abandon_on_cancel` to avoid
67+
# surfacing deprecation warnings.
68+
if function_has_argument(anyio.to_thread.run_sync, "abandon_on_cancel"):
69+
return await anyio.to_thread.run_sync(
70+
partial_f,
71+
abandon_on_cancel=cancellable,
72+
limiter=limiter,
73+
)
74+
75+
return await anyio.to_thread.run_sync(
76+
partial_f,
77+
cancellable=cancellable,
78+
limiter=limiter,
79+
)
6380

6481
return wrapper

0 commit comments

Comments
 (0)