From 13b9cfd39534dc25bf11abc2531ef976693eb244 Mon Sep 17 00:00:00 2001 From: stainless-bot Date: Mon, 4 Mar 2024 23:51:02 +0000 Subject: [PATCH] feat: update via SDK Studio --- src/cloudflare/_client.py | 123 +++++++---- tests/conftest.py | 23 +- tests/test_client.py | 438 ++++---------------------------------- 3 files changed, 120 insertions(+), 464 deletions(-) diff --git a/src/cloudflare/_client.py b/src/cloudflare/_client.py index 2af7e742b44..762aa9fb637 100644 --- a/src/cloudflare/_client.py +++ b/src/cloudflare/_client.py @@ -13,6 +13,7 @@ from ._types import ( NOT_GIVEN, Omit, + Headers, Timeout, NotGiven, Transport, @@ -25,7 +26,7 @@ ) from ._version import __version__ from ._streaming import Stream as Stream, AsyncStream as AsyncStream -from ._exceptions import APIStatusError, CloudflareError +from ._exceptions import APIStatusError from ._base_client import ( DEFAULT_MAX_RETRIES, SyncAPIClient, @@ -130,10 +131,10 @@ class Cloudflare(SyncAPIClient): with_streaming_response: CloudflareWithStreamedResponse # client options - api_key: str - api_email: str - api_token: str - user_service_key: str + api_key: str | None + api_email: str | None + api_token: str | None + user_service_key: str | None def __init__( self, @@ -169,34 +170,18 @@ def __init__( """ if api_key is None: api_key = os.environ.get("CLOUDFLARE_API_KEY") - if api_key is None: - raise CloudflareError( - "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" - ) self.api_key = api_key if api_email is None: api_email = os.environ.get("CLOUDFLARE_EMAIL") - if api_email is None: - raise CloudflareError( - "The api_email client option must be set either by passing api_email to the client or by setting the CLOUDFLARE_EMAIL environment variable" - ) self.api_email = api_email if api_token is None: api_token = os.environ.get("CLOUDFLARE_API_TOKEN") - if api_token is None: - raise CloudflareError( - "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" - ) self.api_token = api_token if user_service_key is None: user_service_key = os.environ.get("CLOUDFLARE_API_USER_SERVICE_KEY") - if user_service_key is None: - raise CloudflareError( - "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" - ) self.user_service_key = user_service_key if base_url is None: @@ -319,21 +304,29 @@ def auth_headers(self) -> dict[str, str]: @property def _api_email(self) -> dict[str, str]: api_email = self.api_email + if api_email is None: + return {} return {"X-Auth-Email": api_email} @property def _api_key(self) -> dict[str, str]: api_key = self.api_key + if api_key is None: + return {} return {"X-Auth-Key": api_key} @property def _api_token(self) -> dict[str, str]: api_token = self.api_token + if api_token is None: + return {} return {"Authorization": f"Bearer {api_token}"} @property def _user_service_key(self) -> dict[str, str]: user_service_key = self.user_service_key + if user_service_key is None: + return {} return {"X-Auth-User-Service-Key": user_service_key} @property @@ -342,10 +335,36 @@ def default_headers(self) -> dict[str, str | Omit]: return { **super().default_headers, "X-Stainless-Async": "false", - "x-auth-email": self.api_email, + "x-auth-email": self.api_email if self.api_email is not None else Omit(), **self._custom_headers, } + @override + def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None: + if self.api_email and headers.get("X-Auth-Email"): + return + if isinstance(custom_headers.get("X-Auth-Email"), Omit): + return + + if self.api_key and headers.get("X-Auth-Key"): + return + if isinstance(custom_headers.get("X-Auth-Key"), Omit): + return + + if self.api_token and headers.get("Authorization"): + return + if isinstance(custom_headers.get("Authorization"), Omit): + return + + if self.user_service_key and headers.get("X-Auth-User-Service-Key"): + return + if isinstance(custom_headers.get("X-Auth-User-Service-Key"), Omit): + return + + raise TypeError( + '"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"' + ) + def copy( self, *, @@ -522,10 +541,10 @@ class AsyncCloudflare(AsyncAPIClient): with_streaming_response: AsyncCloudflareWithStreamedResponse # client options - api_key: str - api_email: str - api_token: str - user_service_key: str + api_key: str | None + api_email: str | None + api_token: str | None + user_service_key: str | None def __init__( self, @@ -561,34 +580,18 @@ def __init__( """ if api_key is None: api_key = os.environ.get("CLOUDFLARE_API_KEY") - if api_key is None: - raise CloudflareError( - "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" - ) self.api_key = api_key if api_email is None: api_email = os.environ.get("CLOUDFLARE_EMAIL") - if api_email is None: - raise CloudflareError( - "The api_email client option must be set either by passing api_email to the client or by setting the CLOUDFLARE_EMAIL environment variable" - ) self.api_email = api_email if api_token is None: api_token = os.environ.get("CLOUDFLARE_API_TOKEN") - if api_token is None: - raise CloudflareError( - "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" - ) self.api_token = api_token if user_service_key is None: user_service_key = os.environ.get("CLOUDFLARE_API_USER_SERVICE_KEY") - if user_service_key is None: - raise CloudflareError( - "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" - ) self.user_service_key = user_service_key if base_url is None: @@ -711,21 +714,29 @@ def auth_headers(self) -> dict[str, str]: @property def _api_email(self) -> dict[str, str]: api_email = self.api_email + if api_email is None: + return {} return {"X-Auth-Email": api_email} @property def _api_key(self) -> dict[str, str]: api_key = self.api_key + if api_key is None: + return {} return {"X-Auth-Key": api_key} @property def _api_token(self) -> dict[str, str]: api_token = self.api_token + if api_token is None: + return {} return {"Authorization": f"Bearer {api_token}"} @property def _user_service_key(self) -> dict[str, str]: user_service_key = self.user_service_key + if user_service_key is None: + return {} return {"X-Auth-User-Service-Key": user_service_key} @property @@ -734,10 +745,36 @@ def default_headers(self) -> dict[str, str | Omit]: return { **super().default_headers, "X-Stainless-Async": f"async:{get_async_library()}", - "x-auth-email": self.api_email, + "x-auth-email": self.api_email if self.api_email is not None else Omit(), **self._custom_headers, } + @override + def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None: + if self.api_email and headers.get("X-Auth-Email"): + return + if isinstance(custom_headers.get("X-Auth-Email"), Omit): + return + + if self.api_key and headers.get("X-Auth-Key"): + return + if isinstance(custom_headers.get("X-Auth-Key"), Omit): + return + + if self.api_token and headers.get("Authorization"): + return + if isinstance(custom_headers.get("Authorization"), Omit): + return + + if self.user_service_key and headers.get("X-Auth-User-Service-Key"): + return + if isinstance(custom_headers.get("X-Auth-User-Service-Key"), Omit): + return + + raise TypeError( + '"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"' + ) + def copy( self, *, diff --git a/tests/conftest.py b/tests/conftest.py index 504c181752f..4e91d62622b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,11 +26,6 @@ def event_loop() -> Iterator[asyncio.AbstractEventLoop]: base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") -api_key = "144c9defac04969c7bfad8efaa8ea194" -api_email = "user@example.com" -api_token = "Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY" -user_service_key = "v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" - @pytest.fixture(scope="session") def client(request: FixtureRequest) -> Iterator[Cloudflare]: @@ -38,14 +33,7 @@ def client(request: FixtureRequest) -> Iterator[Cloudflare]: if not isinstance(strict, bool): raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}") - with Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=strict, - ) as client: + with Cloudflare(base_url=base_url, _strict_response_validation=strict) as client: yield client @@ -55,12 +43,5 @@ async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncCloudflare if not isinstance(strict, bool): raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}") - async with AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=strict, - ) as client: + async with AsyncCloudflare(base_url=base_url, _strict_response_validation=strict) as client: yield client diff --git a/tests/test_client.py b/tests/test_client.py index de4829a616e..bd3aa00cf4d 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -31,10 +31,6 @@ from .utils import update_env base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") -api_key = "144c9defac04969c7bfad8efaa8ea194" -api_email = "user@example.com" -api_token = "Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY" -user_service_key = "v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" def _get_params(client: BaseClient[Any, Any]) -> dict[str, str]: @@ -56,14 +52,7 @@ def _get_open_connections(client: Cloudflare | AsyncCloudflare) -> int: class TestCloudflare: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True) @pytest.mark.respx(base_url=base_url) def test_raw_response(self, respx_mock: MockRouter) -> None: @@ -89,30 +78,6 @@ def test_copy(self) -> None: copied = self.client.copy() assert id(copied) != id(self.client) - copied = self.client.copy(api_key="another 144c9defac04969c7bfad8efaa8ea194") - assert copied.api_key == "another 144c9defac04969c7bfad8efaa8ea194" - assert self.client.api_key == "144c9defac04969c7bfad8efaa8ea194" - - copied = self.client.copy(api_email="another user@example.com") - assert copied.api_email == "another user@example.com" - assert self.client.api_email == "user@example.com" - - copied = self.client.copy(api_token="another Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY") - assert copied.api_token == "another Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY" - assert self.client.api_token == "Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY" - - copied = self.client.copy( - user_service_key="another v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" - ) - assert ( - copied.user_service_key - == "another v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" - ) - assert ( - self.client.user_service_key - == "v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" - ) - def test_copy_default_options(self) -> None: # options that have a default are overridden correctly copied = self.client.copy(max_retries=7) @@ -130,15 +95,7 @@ def test_copy_default_options(self) -> None: assert isinstance(self.client.timeout, httpx.Timeout) def test_copy_default_headers(self) -> None: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - default_headers={"X-Foo": "bar"}, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True, default_headers={"X-Foo": "bar"}) assert client.default_headers["X-Foo"] == "bar" # does not override the already given value when not specified @@ -170,15 +127,7 @@ def test_copy_default_headers(self) -> None: client.copy(set_default_headers={}, default_headers={"X-Foo": "Bar"}) def test_copy_default_query(self) -> None: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - default_query={"foo": "bar"}, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True, default_query={"foo": "bar"}) assert _get_params(client)["foo"] == "bar" # does not override the already given value when not specified @@ -301,15 +250,7 @@ def test_request_timeout(self) -> None: assert timeout == httpx.Timeout(100.0) def test_client_timeout_option(self) -> None: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - timeout=httpx.Timeout(0), - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True, timeout=httpx.Timeout(0)) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore @@ -318,15 +259,7 @@ def test_client_timeout_option(self) -> None: def test_http_client_timeout_option(self) -> None: # custom timeout given to the httpx client should be used with httpx.Client(timeout=None) as http_client: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - http_client=http_client, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True, http_client=http_client) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore @@ -334,15 +267,7 @@ def test_http_client_timeout_option(self) -> None: # no timeout given to the httpx client should not use the httpx default with httpx.Client() as http_client: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - http_client=http_client, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True, http_client=http_client) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore @@ -350,40 +275,20 @@ def test_http_client_timeout_option(self) -> None: # explicitly passing the default timeout currently results in it being ignored with httpx.Client(timeout=HTTPX_DEFAULT_TIMEOUT) as http_client: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - http_client=http_client, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True, http_client=http_client) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore assert timeout == DEFAULT_TIMEOUT # our default def test_default_headers_option(self) -> None: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - default_headers={"X-Foo": "bar"}, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True, default_headers={"X-Foo": "bar"}) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) assert request.headers.get("x-foo") == "bar" assert request.headers.get("x-stainless-lang") == "python" client2 = Cloudflare( base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, _strict_response_validation=True, default_headers={ "X-Foo": "stainless", @@ -395,15 +300,7 @@ def test_default_headers_option(self) -> None: assert request.headers.get("x-stainless-lang") == "my-overriding-header" def test_default_query_option(self) -> None: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - default_query={"query_param": "bar"}, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True, default_query={"query_param": "bar"}) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) url = httpx.URL(request.url) assert dict(url.params) == {"query_param": "bar"} @@ -602,14 +499,7 @@ class Model(BaseModel): assert response.foo == 2 def test_base_url_setter(self) -> None: - client = Cloudflare( - base_url="https://example.com/from_init", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = Cloudflare(base_url="https://example.com/from_init", _strict_response_validation=True) assert client.base_url == "https://example.com/from_init/" client.base_url = "https://example.com/from_setter" # type: ignore[assignment] @@ -618,32 +508,15 @@ def test_base_url_setter(self) -> None: def test_base_url_env(self) -> None: with update_env(CLOUDFLARE_BASE_URL="http://localhost:5000/from/env"): - client = Cloudflare( - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = Cloudflare(_strict_response_validation=True) assert client.base_url == "http://localhost:5000/from/env/" @pytest.mark.parametrize( "client", [ + Cloudflare(base_url="http://localhost:5000/custom/path/", _strict_response_validation=True), Cloudflare( base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ), - Cloudflare( - base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, _strict_response_validation=True, http_client=httpx.Client(), ), @@ -663,20 +536,9 @@ def test_base_url_trailing_slash(self, client: Cloudflare) -> None: @pytest.mark.parametrize( "client", [ + Cloudflare(base_url="http://localhost:5000/custom/path/", _strict_response_validation=True), Cloudflare( base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ), - Cloudflare( - base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, _strict_response_validation=True, http_client=httpx.Client(), ), @@ -696,20 +558,9 @@ def test_base_url_no_trailing_slash(self, client: Cloudflare) -> None: @pytest.mark.parametrize( "client", [ + Cloudflare(base_url="http://localhost:5000/custom/path/", _strict_response_validation=True), Cloudflare( base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ), - Cloudflare( - base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, _strict_response_validation=True, http_client=httpx.Client(), ), @@ -727,14 +578,7 @@ def test_absolute_request_url(self, client: Cloudflare) -> None: assert request.url == "https://myapi.com/foo" def test_copied_client_does_not_close_http(self) -> None: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True) assert not client.is_closed() copied = client.copy() @@ -745,14 +589,7 @@ def test_copied_client_does_not_close_http(self) -> None: assert not client.is_closed() def test_client_context_manager(self) -> None: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True) with client as c2: assert c2 is client assert not c2.is_closed() @@ -778,26 +615,12 @@ class Model(BaseModel): respx_mock.get("/foo").mock(return_value=httpx.Response(200, text="my-custom-format")) - strict_client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + strict_client = Cloudflare(base_url=base_url, _strict_response_validation=True) with pytest.raises(APIResponseValidationError): strict_client.get("/foo", cast_to=Model) - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=False, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=False) response = client.get("/foo", cast_to=Model) assert isinstance(response, str) # type: ignore[unreachable] @@ -824,14 +647,7 @@ class Model(BaseModel): ) @mock.patch("time.time", mock.MagicMock(return_value=1696004797)) def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str, timeout: float) -> None: - client = Cloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = Cloudflare(base_url=base_url, _strict_response_validation=True) headers = httpx.Headers({"retry-after": retry_after}) options = FinalRequestOptions(method="get", url="/foo", max_retries=3) @@ -874,14 +690,7 @@ def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> Non class TestAsyncCloudflare: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True) @pytest.mark.respx(base_url=base_url) @pytest.mark.asyncio @@ -909,30 +718,6 @@ def test_copy(self) -> None: copied = self.client.copy() assert id(copied) != id(self.client) - copied = self.client.copy(api_key="another 144c9defac04969c7bfad8efaa8ea194") - assert copied.api_key == "another 144c9defac04969c7bfad8efaa8ea194" - assert self.client.api_key == "144c9defac04969c7bfad8efaa8ea194" - - copied = self.client.copy(api_email="another user@example.com") - assert copied.api_email == "another user@example.com" - assert self.client.api_email == "user@example.com" - - copied = self.client.copy(api_token="another Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY") - assert copied.api_token == "another Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY" - assert self.client.api_token == "Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY" - - copied = self.client.copy( - user_service_key="another v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" - ) - assert ( - copied.user_service_key - == "another v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" - ) - assert ( - self.client.user_service_key - == "v1.0-144c9defac04969c7bfad8ef-631a41d003a32d25fe878081ef365c49503f7fada600da935e2851a1c7326084b85cbf6429c4b859de8475731dc92a9c329631e6d59e6c73da7b198497172b4cefe071d90d0f5d2719" - ) - def test_copy_default_options(self) -> None: # options that have a default are overridden correctly copied = self.client.copy(max_retries=7) @@ -950,15 +735,7 @@ def test_copy_default_options(self) -> None: assert isinstance(self.client.timeout, httpx.Timeout) def test_copy_default_headers(self) -> None: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - default_headers={"X-Foo": "bar"}, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True, default_headers={"X-Foo": "bar"}) assert client.default_headers["X-Foo"] == "bar" # does not override the already given value when not specified @@ -990,15 +767,7 @@ def test_copy_default_headers(self) -> None: client.copy(set_default_headers={}, default_headers={"X-Foo": "Bar"}) def test_copy_default_query(self) -> None: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - default_query={"foo": "bar"}, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True, default_query={"foo": "bar"}) assert _get_params(client)["foo"] == "bar" # does not override the already given value when not specified @@ -1121,15 +890,7 @@ async def test_request_timeout(self) -> None: assert timeout == httpx.Timeout(100.0) async def test_client_timeout_option(self) -> None: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - timeout=httpx.Timeout(0), - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True, timeout=httpx.Timeout(0)) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore @@ -1138,15 +899,7 @@ async def test_client_timeout_option(self) -> None: async def test_http_client_timeout_option(self) -> None: # custom timeout given to the httpx client should be used async with httpx.AsyncClient(timeout=None) as http_client: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - http_client=http_client, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True, http_client=http_client) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore @@ -1154,15 +907,7 @@ async def test_http_client_timeout_option(self) -> None: # no timeout given to the httpx client should not use the httpx default async with httpx.AsyncClient() as http_client: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - http_client=http_client, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True, http_client=http_client) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore @@ -1170,40 +915,20 @@ async def test_http_client_timeout_option(self) -> None: # explicitly passing the default timeout currently results in it being ignored async with httpx.AsyncClient(timeout=HTTPX_DEFAULT_TIMEOUT) as http_client: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - http_client=http_client, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True, http_client=http_client) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore assert timeout == DEFAULT_TIMEOUT # our default def test_default_headers_option(self) -> None: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - default_headers={"X-Foo": "bar"}, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True, default_headers={"X-Foo": "bar"}) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) assert request.headers.get("x-foo") == "bar" assert request.headers.get("x-stainless-lang") == "python" client2 = AsyncCloudflare( base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, _strict_response_validation=True, default_headers={ "X-Foo": "stainless", @@ -1216,13 +941,7 @@ def test_default_headers_option(self) -> None: def test_default_query_option(self) -> None: client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - default_query={"query_param": "bar"}, + base_url=base_url, _strict_response_validation=True, default_query={"query_param": "bar"} ) request = client._build_request(FinalRequestOptions(method="get", url="/foo")) url = httpx.URL(request.url) @@ -1422,14 +1141,7 @@ class Model(BaseModel): assert response.foo == 2 def test_base_url_setter(self) -> None: - client = AsyncCloudflare( - base_url="https://example.com/from_init", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = AsyncCloudflare(base_url="https://example.com/from_init", _strict_response_validation=True) assert client.base_url == "https://example.com/from_init/" client.base_url = "https://example.com/from_setter" # type: ignore[assignment] @@ -1438,32 +1150,15 @@ def test_base_url_setter(self) -> None: def test_base_url_env(self) -> None: with update_env(CLOUDFLARE_BASE_URL="http://localhost:5000/from/env"): - client = AsyncCloudflare( - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = AsyncCloudflare(_strict_response_validation=True) assert client.base_url == "http://localhost:5000/from/env/" @pytest.mark.parametrize( "client", [ + AsyncCloudflare(base_url="http://localhost:5000/custom/path/", _strict_response_validation=True), AsyncCloudflare( base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ), - AsyncCloudflare( - base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, _strict_response_validation=True, http_client=httpx.AsyncClient(), ), @@ -1483,20 +1178,9 @@ def test_base_url_trailing_slash(self, client: AsyncCloudflare) -> None: @pytest.mark.parametrize( "client", [ + AsyncCloudflare(base_url="http://localhost:5000/custom/path/", _strict_response_validation=True), AsyncCloudflare( base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ), - AsyncCloudflare( - base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, _strict_response_validation=True, http_client=httpx.AsyncClient(), ), @@ -1516,20 +1200,9 @@ def test_base_url_no_trailing_slash(self, client: AsyncCloudflare) -> None: @pytest.mark.parametrize( "client", [ + AsyncCloudflare(base_url="http://localhost:5000/custom/path/", _strict_response_validation=True), AsyncCloudflare( base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ), - AsyncCloudflare( - base_url="http://localhost:5000/custom/path/", - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, _strict_response_validation=True, http_client=httpx.AsyncClient(), ), @@ -1547,14 +1220,7 @@ def test_absolute_request_url(self, client: AsyncCloudflare) -> None: assert request.url == "https://myapi.com/foo" async def test_copied_client_does_not_close_http(self) -> None: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True) assert not client.is_closed() copied = client.copy() @@ -1566,14 +1232,7 @@ async def test_copied_client_does_not_close_http(self) -> None: assert not client.is_closed() async def test_client_context_manager(self) -> None: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True) async with client as c2: assert c2 is client assert not c2.is_closed() @@ -1601,26 +1260,12 @@ class Model(BaseModel): respx_mock.get("/foo").mock(return_value=httpx.Response(200, text="my-custom-format")) - strict_client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + strict_client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True) with pytest.raises(APIResponseValidationError): await strict_client.get("/foo", cast_to=Model) - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=False, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=False) response = await client.get("/foo", cast_to=Model) assert isinstance(response, str) # type: ignore[unreachable] @@ -1648,14 +1293,7 @@ class Model(BaseModel): @mock.patch("time.time", mock.MagicMock(return_value=1696004797)) @pytest.mark.asyncio async def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str, timeout: float) -> None: - client = AsyncCloudflare( - base_url=base_url, - api_key=api_key, - api_email=api_email, - api_token=api_token, - user_service_key=user_service_key, - _strict_response_validation=True, - ) + client = AsyncCloudflare(base_url=base_url, _strict_response_validation=True) headers = httpx.Headers({"retry-after": retry_after}) options = FinalRequestOptions(method="get", url="/foo", max_retries=3)