Skip to content
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
42 changes: 31 additions & 11 deletions src/shade/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

from .config import Environment, validate_client_settings
from .config import config as _config
from .http import AsyncHTTPClient, HTTPXTransport
from .http_client import _SyncHTTPClient
from .http import HTTPXTransport
from .http_client import _AsyncHTTPClient, _SyncHTTPClient

API_KEY_ENV_VAR = "SHADE_API_KEY"
ENVIRONMENT_ENV_VAR = "SHADE_ENVIRONMENT"
Expand Down Expand Up @@ -105,13 +105,7 @@ def __init__(
max_retries=self._max_retries,
timeout=self._timeout,
)
self._async_http = AsyncHTTPClient(
base_url=self._api_base,
api_key=self._api_key,
environment=self._environment,
max_retries=self._max_retries,
timeout=self._timeout,
)
self._async_http_instance: Optional[_AsyncHTTPClient] = None
self._client = HTTPXTransport(
api_key=self._api_key,
base_url=self._api_base,
Expand All @@ -121,6 +115,18 @@ def __init__(
http_client=http_client,
)

@property
def _async_http(self) -> _AsyncHTTPClient:
if self._async_http_instance is None:
self._async_http_instance = _AsyncHTTPClient(
api_base=self._api_base,
api_key=self._api_key,
environment=self._environment,
max_retries=self._max_retries,
timeout=self._timeout,
)
return self._async_http_instance

@classmethod
def from_env(cls, **overrides: Any) -> "ShadeClient":
"""Build a client from ``SHADE_API_KEY`` and ``SHADE_ENVIRONMENT``.
Expand Down Expand Up @@ -154,7 +160,8 @@ def api_key(self) -> Optional[str]:
def api_key(self, value: Optional[str]) -> None:
self._api_key = value
self._http.api_key = value
self._async_http.api_key = value
if self._async_http_instance is not None:
self._async_http_instance.api_key = value
self._client.api_key = value

@property
Expand All @@ -168,7 +175,8 @@ def environment(self, value: str | Environment) -> None:
parsed = _config.parse_environment(value)
self._environment = parsed
self._http.environment = parsed
self._async_http.environment = parsed
if self._async_http_instance is not None:
self._async_http_instance.environment = parsed
self._client.environment = parsed

@property
Expand Down Expand Up @@ -196,12 +204,24 @@ def close(self) -> None:
self._http.close()
self._client.close()

async def aclose(self) -> None:
self.close()
if self._async_http_instance is not None:
await self._async_http_instance.aclose()
self._async_http_instance = None

def __enter__(self) -> "ShadeClient":
return self

def __exit__(self, *args: Any) -> None:
self.close()

async def __aenter__(self) -> "ShadeClient":
return self

async def __aexit__(self, *args: Any) -> None:
await self.aclose()

def request(
self,
method: str,
Expand Down
5 changes: 2 additions & 3 deletions src/shade/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ async def process_payment_async(
self, amount: float, currency: str
) -> Dict[str, Any]:
"""Async variant of :meth:`process_payment`."""
return await self._async_http.request(
"POST",
return await self._async_http.post(
"/payments",
{"amount": amount, "currency": currency},
json={"amount": amount, "currency": currency},
)
Loading
Loading