13
13
from ._types import (
14
14
NOT_GIVEN ,
15
15
Omit ,
16
+ Headers ,
16
17
Timeout ,
17
18
NotGiven ,
18
19
Transport ,
25
26
)
26
27
from ._version import __version__
27
28
from ._streaming import Stream as Stream , AsyncStream as AsyncStream
28
- from ._exceptions import APIStatusError , CloudflareError
29
+ from ._exceptions import APIStatusError
29
30
from ._base_client import (
30
31
DEFAULT_MAX_RETRIES ,
31
32
SyncAPIClient ,
@@ -130,10 +131,10 @@ class Cloudflare(SyncAPIClient):
130
131
with_streaming_response : CloudflareWithStreamedResponse
131
132
132
133
# 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
137
138
138
139
def __init__ (
139
140
self ,
@@ -169,34 +170,18 @@ def __init__(
169
170
"""
170
171
if api_key is None :
171
172
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
- )
176
173
self .api_key = api_key
177
174
178
175
if api_email is None :
179
176
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
- )
184
177
self .api_email = api_email
185
178
186
179
if api_token is None :
187
180
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
- )
192
181
self .api_token = api_token
193
182
194
183
if user_service_key is None :
195
184
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
- )
200
185
self .user_service_key = user_service_key
201
186
202
187
if base_url is None :
@@ -319,21 +304,29 @@ def auth_headers(self) -> dict[str, str]:
319
304
@property
320
305
def _api_email (self ) -> dict [str , str ]:
321
306
api_email = self .api_email
307
+ if api_email is None :
308
+ return {}
322
309
return {"X-Auth-Email" : api_email }
323
310
324
311
@property
325
312
def _api_key (self ) -> dict [str , str ]:
326
313
api_key = self .api_key
314
+ if api_key is None :
315
+ return {}
327
316
return {"X-Auth-Key" : api_key }
328
317
329
318
@property
330
319
def _api_token (self ) -> dict [str , str ]:
331
320
api_token = self .api_token
321
+ if api_token is None :
322
+ return {}
332
323
return {"Authorization" : f"Bearer { api_token } " }
333
324
334
325
@property
335
326
def _user_service_key (self ) -> dict [str , str ]:
336
327
user_service_key = self .user_service_key
328
+ if user_service_key is None :
329
+ return {}
337
330
return {"X-Auth-User-Service-Key" : user_service_key }
338
331
339
332
@property
@@ -342,10 +335,36 @@ def default_headers(self) -> dict[str, str | Omit]:
342
335
return {
343
336
** super ().default_headers ,
344
337
"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 () ,
346
339
** self ._custom_headers ,
347
340
}
348
341
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
+
349
368
def copy (
350
369
self ,
351
370
* ,
@@ -522,10 +541,10 @@ class AsyncCloudflare(AsyncAPIClient):
522
541
with_streaming_response : AsyncCloudflareWithStreamedResponse
523
542
524
543
# 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
529
548
530
549
def __init__ (
531
550
self ,
@@ -561,34 +580,18 @@ def __init__(
561
580
"""
562
581
if api_key is None :
563
582
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
- )
568
583
self .api_key = api_key
569
584
570
585
if api_email is None :
571
586
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
- )
576
587
self .api_email = api_email
577
588
578
589
if api_token is None :
579
590
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
- )
584
591
self .api_token = api_token
585
592
586
593
if user_service_key is None :
587
594
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
- )
592
595
self .user_service_key = user_service_key
593
596
594
597
if base_url is None :
@@ -711,21 +714,29 @@ def auth_headers(self) -> dict[str, str]:
711
714
@property
712
715
def _api_email (self ) -> dict [str , str ]:
713
716
api_email = self .api_email
717
+ if api_email is None :
718
+ return {}
714
719
return {"X-Auth-Email" : api_email }
715
720
716
721
@property
717
722
def _api_key (self ) -> dict [str , str ]:
718
723
api_key = self .api_key
724
+ if api_key is None :
725
+ return {}
719
726
return {"X-Auth-Key" : api_key }
720
727
721
728
@property
722
729
def _api_token (self ) -> dict [str , str ]:
723
730
api_token = self .api_token
731
+ if api_token is None :
732
+ return {}
724
733
return {"Authorization" : f"Bearer { api_token } " }
725
734
726
735
@property
727
736
def _user_service_key (self ) -> dict [str , str ]:
728
737
user_service_key = self .user_service_key
738
+ if user_service_key is None :
739
+ return {}
729
740
return {"X-Auth-User-Service-Key" : user_service_key }
730
741
731
742
@property
@@ -734,10 +745,36 @@ def default_headers(self) -> dict[str, str | Omit]:
734
745
return {
735
746
** super ().default_headers ,
736
747
"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 () ,
738
749
** self ._custom_headers ,
739
750
}
740
751
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
+
741
778
def copy (
742
779
self ,
743
780
* ,
0 commit comments