Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update via SDK Studio #84

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 80 additions & 43 deletions src/cloudflare/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ._types import (
NOT_GIVEN,
Omit,
Headers,
Timeout,
NotGiven,
Transport,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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,
*,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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,
*,
Expand Down
23 changes: 2 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,14 @@ 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]:
strict = getattr(request, "param", True)
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


Expand All @@ -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
Loading