Skip to content

Commit 8bc71bd

Browse files
committed
feat: support API versioning
1 parent f61be23 commit 8bc71bd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/cloudflare/_base_client.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ class BaseClient(Generic[_HttpxClientT, _DefaultStreamT]):
333333
_client: _HttpxClientT
334334
_version: str
335335
_base_url: URL
336+
_api_version: str
336337
max_retries: int
337338
timeout: Union[float, Timeout, None]
338339
_strict_response_validation: bool
@@ -344,6 +345,7 @@ def __init__(
344345
*,
345346
version: str,
346347
base_url: str | URL,
348+
api_version: str,
347349
_strict_response_validation: bool,
348350
max_retries: int = DEFAULT_MAX_RETRIES,
349351
timeout: float | Timeout | None = DEFAULT_TIMEOUT,
@@ -352,6 +354,7 @@ def __init__(
352354
) -> None:
353355
self._version = version
354356
self._base_url = self._enforce_trailing_slash(URL(base_url))
357+
self.api_version = api_version
355358
self.max_retries = max_retries
356359
self.timeout = timeout
357360
self._custom_headers = custom_headers or {}
@@ -425,6 +428,8 @@ def _build_headers(self, options: FinalRequestOptions, *, retries_taken: int = 0
425428
if timeout is not None:
426429
headers["x-stainless-read-timeout"] = str(timeout)
427430

431+
headers["api-version"] = self.api_version
432+
428433
return headers
429434

430435
def _prepare_url(self, url: str) -> URL:
@@ -486,7 +491,7 @@ def _build_request(
486491
raise TypeError(
487492
f"Expected query input to be a dictionary for multipart requests but got {type(json_data)} instead."
488493
)
489-
494+
490495
if options.multipart_syntax == 'json':
491496
json_data = cast("Mapping[str, object]", json_data)
492497
if is_mapping_t(files):
@@ -820,6 +825,7 @@ def __init__(
820825
*,
821826
version: str,
822827
base_url: str | URL,
828+
api_version: str,
823829
max_retries: int = DEFAULT_MAX_RETRIES,
824830
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
825831
http_client: httpx.Client | None = None,
@@ -850,6 +856,7 @@ def __init__(
850856
# cast to a valid type because mypy doesn't understand our type narrowing
851857
timeout=cast(Timeout, timeout),
852858
base_url=base_url,
859+
api_version=api_version,
853860
max_retries=max_retries,
854861
custom_query=custom_query,
855862
custom_headers=custom_headers,
@@ -1350,6 +1357,7 @@ def __init__(
13501357
*,
13511358
version: str,
13521359
base_url: str | URL,
1360+
api_version: str,
13531361
_strict_response_validation: bool,
13541362
max_retries: int = DEFAULT_MAX_RETRIES,
13551363
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
@@ -1378,6 +1386,7 @@ def __init__(
13781386
super().__init__(
13791387
version=version,
13801388
base_url=base_url,
1389+
api_version=api_version,
13811390
# cast to a valid type because mypy doesn't understand our type narrowing
13821391
timeout=cast(Timeout, timeout),
13831392
max_retries=max_retries,

src/cloudflare/_client.py

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66
from typing import TYPE_CHECKING, Any, Union, Mapping
7+
from datetime import datetime
78
from typing_extensions import Self, override
89

910
import httpx
@@ -280,6 +281,7 @@ def __init__(
280281
api_email: str | None = None,
281282
user_service_key: str | None = None,
282283
base_url: str | httpx.URL | None = None,
284+
api_version: str | None = None,
283285
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
284286
max_retries: int = DEFAULT_MAX_RETRIES,
285287
default_headers: Mapping[str, str] | None = None,
@@ -327,9 +329,13 @@ def __init__(
327329
if base_url is None:
328330
base_url = f"https://api.cloudflare.com/client/v4"
329331

332+
if api_version is None:
333+
api_version = datetime.today().strftime('%Y-%m-%d')
334+
330335
super().__init__(
331336
version=__version__,
332337
base_url=base_url,
338+
api_version=api_version,
333339
max_retries=max_retries,
334340
timeout=timeout,
335341
http_client=http_client,
@@ -1001,6 +1007,7 @@ def copy(
10011007
api_email: str | None = None,
10021008
user_service_key: str | None = None,
10031009
base_url: str | httpx.URL | None = None,
1010+
api_version: str | None = None,
10041011
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
10051012
http_client: httpx.Client | None = None,
10061013
max_retries: int | NotGiven = NOT_GIVEN,
@@ -1038,6 +1045,7 @@ def copy(
10381045
api_email=api_email or self.api_email,
10391046
user_service_key=user_service_key or self.user_service_key,
10401047
base_url=base_url or self.base_url,
1048+
api_version=api_version or self.api_version,
10411049
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
10421050
http_client=http_client,
10431051
max_retries=max_retries if is_given(max_retries) else self.max_retries,
@@ -1099,6 +1107,7 @@ def __init__(
10991107
api_email: str | None = None,
11001108
user_service_key: str | None = None,
11011109
base_url: str | httpx.URL | None = None,
1110+
api_version: str | None = None,
11021111
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
11031112
max_retries: int = DEFAULT_MAX_RETRIES,
11041113
default_headers: Mapping[str, str] | None = None,
@@ -1146,9 +1155,13 @@ def __init__(
11461155
if base_url is None:
11471156
base_url = f"https://api.cloudflare.com/client/v4"
11481157

1158+
if api_version is None:
1159+
api_version = datetime.today().strftime('%Y-%m-%d')
1160+
11491161
super().__init__(
11501162
version=__version__,
11511163
base_url=base_url,
1164+
api_version=api_version,
11521165
max_retries=max_retries,
11531166
timeout=timeout,
11541167
http_client=http_client,

0 commit comments

Comments
 (0)