Skip to content

Commit 68a9ed1

Browse files
stainless-app[bot]stainless-bot
authored andcommitted
feat: update via SDK Studio (#84)
1 parent 074cbd4 commit 68a9ed1

File tree

3 files changed

+120
-464
lines changed

3 files changed

+120
-464
lines changed

src/cloudflare/_client.py

+80-43
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ._types import (
1414
NOT_GIVEN,
1515
Omit,
16+
Headers,
1617
Timeout,
1718
NotGiven,
1819
Transport,
@@ -25,7 +26,7 @@
2526
)
2627
from ._version import __version__
2728
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
28-
from ._exceptions import APIStatusError, CloudflareError
29+
from ._exceptions import APIStatusError
2930
from ._base_client import (
3031
DEFAULT_MAX_RETRIES,
3132
SyncAPIClient,
@@ -130,10 +131,10 @@ class Cloudflare(SyncAPIClient):
130131
with_streaming_response: CloudflareWithStreamedResponse
131132

132133
# client options
133-
api_key: str
134-
api_email: str
135-
api_token: str
136-
user_service_key: str
134+
api_key: str | None
135+
api_email: str | None
136+
api_token: str | None
137+
user_service_key: str | None
137138

138139
def __init__(
139140
self,
@@ -169,34 +170,18 @@ def __init__(
169170
"""
170171
if api_key is None:
171172
api_key = os.environ.get("CLOUDFLARE_API_KEY")
172-
if api_key is None:
173-
raise CloudflareError(
174-
"The api_key client option must be set either by passing api_key to the client or by setting the CLOUDFLARE_API_KEY environment variable"
175-
)
176173
self.api_key = api_key
177174

178175
if api_email is None:
179176
api_email = os.environ.get("CLOUDFLARE_EMAIL")
180-
if api_email is None:
181-
raise CloudflareError(
182-
"The api_email client option must be set either by passing api_email to the client or by setting the CLOUDFLARE_EMAIL environment variable"
183-
)
184177
self.api_email = api_email
185178

186179
if api_token is None:
187180
api_token = os.environ.get("CLOUDFLARE_API_TOKEN")
188-
if api_token is None:
189-
raise CloudflareError(
190-
"The api_token client option must be set either by passing api_token to the client or by setting the CLOUDFLARE_API_TOKEN environment variable"
191-
)
192181
self.api_token = api_token
193182

194183
if user_service_key is None:
195184
user_service_key = os.environ.get("CLOUDFLARE_API_USER_SERVICE_KEY")
196-
if user_service_key is None:
197-
raise CloudflareError(
198-
"The user_service_key client option must be set either by passing user_service_key to the client or by setting the CLOUDFLARE_API_USER_SERVICE_KEY environment variable"
199-
)
200185
self.user_service_key = user_service_key
201186

202187
if base_url is None:
@@ -319,21 +304,29 @@ def auth_headers(self) -> dict[str, str]:
319304
@property
320305
def _api_email(self) -> dict[str, str]:
321306
api_email = self.api_email
307+
if api_email is None:
308+
return {}
322309
return {"X-Auth-Email": api_email}
323310

324311
@property
325312
def _api_key(self) -> dict[str, str]:
326313
api_key = self.api_key
314+
if api_key is None:
315+
return {}
327316
return {"X-Auth-Key": api_key}
328317

329318
@property
330319
def _api_token(self) -> dict[str, str]:
331320
api_token = self.api_token
321+
if api_token is None:
322+
return {}
332323
return {"Authorization": f"Bearer {api_token}"}
333324

334325
@property
335326
def _user_service_key(self) -> dict[str, str]:
336327
user_service_key = self.user_service_key
328+
if user_service_key is None:
329+
return {}
337330
return {"X-Auth-User-Service-Key": user_service_key}
338331

339332
@property
@@ -342,10 +335,36 @@ def default_headers(self) -> dict[str, str | Omit]:
342335
return {
343336
**super().default_headers,
344337
"X-Stainless-Async": "false",
345-
"x-auth-email": self.api_email,
338+
"x-auth-email": self.api_email if self.api_email is not None else Omit(),
346339
**self._custom_headers,
347340
}
348341

342+
@override
343+
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
344+
if self.api_email and headers.get("X-Auth-Email"):
345+
return
346+
if isinstance(custom_headers.get("X-Auth-Email"), Omit):
347+
return
348+
349+
if self.api_key and headers.get("X-Auth-Key"):
350+
return
351+
if isinstance(custom_headers.get("X-Auth-Key"), Omit):
352+
return
353+
354+
if self.api_token and headers.get("Authorization"):
355+
return
356+
if isinstance(custom_headers.get("Authorization"), Omit):
357+
return
358+
359+
if self.user_service_key and headers.get("X-Auth-User-Service-Key"):
360+
return
361+
if isinstance(custom_headers.get("X-Auth-User-Service-Key"), Omit):
362+
return
363+
364+
raise TypeError(
365+
'"Could not resolve authentication method. Expected one of api_email, api_key, api_token or user_service_key to be set. Or for one of the `X-Auth-Email`, `X-Auth-Key`, `Authorization` or `X-Auth-User-Service-Key` headers to be explicitly omitted"'
366+
)
367+
349368
def copy(
350369
self,
351370
*,
@@ -522,10 +541,10 @@ class AsyncCloudflare(AsyncAPIClient):
522541
with_streaming_response: AsyncCloudflareWithStreamedResponse
523542

524543
# client options
525-
api_key: str
526-
api_email: str
527-
api_token: str
528-
user_service_key: str
544+
api_key: str | None
545+
api_email: str | None
546+
api_token: str | None
547+
user_service_key: str | None
529548

530549
def __init__(
531550
self,
@@ -561,34 +580,18 @@ def __init__(
561580
"""
562581
if api_key is None:
563582
api_key = os.environ.get("CLOUDFLARE_API_KEY")
564-
if api_key is None:
565-
raise CloudflareError(
566-
"The api_key client option must be set either by passing api_key to the client or by setting the CLOUDFLARE_API_KEY environment variable"
567-
)
568583
self.api_key = api_key
569584

570585
if api_email is None:
571586
api_email = os.environ.get("CLOUDFLARE_EMAIL")
572-
if api_email is None:
573-
raise CloudflareError(
574-
"The api_email client option must be set either by passing api_email to the client or by setting the CLOUDFLARE_EMAIL environment variable"
575-
)
576587
self.api_email = api_email
577588

578589
if api_token is None:
579590
api_token = os.environ.get("CLOUDFLARE_API_TOKEN")
580-
if api_token is None:
581-
raise CloudflareError(
582-
"The api_token client option must be set either by passing api_token to the client or by setting the CLOUDFLARE_API_TOKEN environment variable"
583-
)
584591
self.api_token = api_token
585592

586593
if user_service_key is None:
587594
user_service_key = os.environ.get("CLOUDFLARE_API_USER_SERVICE_KEY")
588-
if user_service_key is None:
589-
raise CloudflareError(
590-
"The user_service_key client option must be set either by passing user_service_key to the client or by setting the CLOUDFLARE_API_USER_SERVICE_KEY environment variable"
591-
)
592595
self.user_service_key = user_service_key
593596

594597
if base_url is None:
@@ -711,21 +714,29 @@ def auth_headers(self) -> dict[str, str]:
711714
@property
712715
def _api_email(self) -> dict[str, str]:
713716
api_email = self.api_email
717+
if api_email is None:
718+
return {}
714719
return {"X-Auth-Email": api_email}
715720

716721
@property
717722
def _api_key(self) -> dict[str, str]:
718723
api_key = self.api_key
724+
if api_key is None:
725+
return {}
719726
return {"X-Auth-Key": api_key}
720727

721728
@property
722729
def _api_token(self) -> dict[str, str]:
723730
api_token = self.api_token
731+
if api_token is None:
732+
return {}
724733
return {"Authorization": f"Bearer {api_token}"}
725734

726735
@property
727736
def _user_service_key(self) -> dict[str, str]:
728737
user_service_key = self.user_service_key
738+
if user_service_key is None:
739+
return {}
729740
return {"X-Auth-User-Service-Key": user_service_key}
730741

731742
@property
@@ -734,10 +745,36 @@ def default_headers(self) -> dict[str, str | Omit]:
734745
return {
735746
**super().default_headers,
736747
"X-Stainless-Async": f"async:{get_async_library()}",
737-
"x-auth-email": self.api_email,
748+
"x-auth-email": self.api_email if self.api_email is not None else Omit(),
738749
**self._custom_headers,
739750
}
740751

752+
@override
753+
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
754+
if self.api_email and headers.get("X-Auth-Email"):
755+
return
756+
if isinstance(custom_headers.get("X-Auth-Email"), Omit):
757+
return
758+
759+
if self.api_key and headers.get("X-Auth-Key"):
760+
return
761+
if isinstance(custom_headers.get("X-Auth-Key"), Omit):
762+
return
763+
764+
if self.api_token and headers.get("Authorization"):
765+
return
766+
if isinstance(custom_headers.get("Authorization"), Omit):
767+
return
768+
769+
if self.user_service_key and headers.get("X-Auth-User-Service-Key"):
770+
return
771+
if isinstance(custom_headers.get("X-Auth-User-Service-Key"), Omit):
772+
return
773+
774+
raise TypeError(
775+
'"Could not resolve authentication method. Expected one of api_email, api_key, api_token or user_service_key to be set. Or for one of the `X-Auth-Email`, `X-Auth-Key`, `Authorization` or `X-Auth-User-Service-Key` headers to be explicitly omitted"'
776+
)
777+
741778
def copy(
742779
self,
743780
*,

tests/conftest.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,14 @@ def event_loop() -> Iterator[asyncio.AbstractEventLoop]:
2626

2727
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
2828

29-
api_key = "144c9defac04969c7bfad8efaa8ea194"
30-
api_email = "[email protected]"
31-
api_token = "Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"
32-
user_service_key = "v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719"
33-
3429

3530
@pytest.fixture(scope="session")
3631
def client(request: FixtureRequest) -> Iterator[Cloudflare]:
3732
strict = getattr(request, "param", True)
3833
if not isinstance(strict, bool):
3934
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
4035

41-
with Cloudflare(
42-
base_url=base_url,
43-
api_key=api_key,
44-
api_email=api_email,
45-
api_token=api_token,
46-
user_service_key=user_service_key,
47-
_strict_response_validation=strict,
48-
) as client:
36+
with Cloudflare(base_url=base_url, _strict_response_validation=strict) as client:
4937
yield client
5038

5139

@@ -55,12 +43,5 @@ async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncCloudflare
5543
if not isinstance(strict, bool):
5644
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
5745

58-
async with AsyncCloudflare(
59-
base_url=base_url,
60-
api_key=api_key,
61-
api_email=api_email,
62-
api_token=api_token,
63-
user_service_key=user_service_key,
64-
_strict_response_validation=strict,
65-
) as client:
46+
async with AsyncCloudflare(base_url=base_url, _strict_response_validation=strict) as client:
6647
yield client

0 commit comments

Comments
 (0)