Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 17
feat(http): implement rate-limit handling with auto-retry (#11)#24
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
571ca5073781ece814a010a12b4340b69cf6e07cc0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,6 +15,7 @@ pytest = "^7.4.0" | ||
| flake8 = "^6.1.0" | ||
| black = "^23.7.0" | ||
| isort = "^5.12.0" | ||
| aiohttp = "^3.14.1" | ||
| [build-system] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,26 @@ | ||
| from .gateway import Gateway | ||
| from .http import AsyncHTTPClient, SyncHTTPClient | ||
| from .errors import ( | ||
| AuthenticationError, | ||
| InvalidRequestError, | ||
| NetworkError, | ||
| NotFoundError, | ||
| HTTPError, | ||
| RateLimitError, | ||
| ShadeError, | ||
| ) | ||
| from .gateway import Gateway | ||
| __version__ = "0.1.0" | ||
| __all__ = [ | ||
| "AsyncHTTPClient", | ||
| "AuthenticationError", | ||
| "Gateway", | ||
| "HTTPError", | ||
| "InvalidRequestError", | ||
| "NetworkError", | ||
| "NotFoundError", | ||
| "RateLimitError", | ||
| "ShadeError", | ||
| ] | ||
| "SyncHTTPClient", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,89 @@ | ||
| from __future__ import annotations | ||
| from typing import Any, Dict, Optional | ||
| from .http import AsyncHTTPClient, SyncHTTPClient, DEFAULT_MAX_RETRIES | ||
| class Gateway: | ||
| """ | ||
| Main entry point for the Shade Payment Gateway. | ||
| Parameters | ||
| ---------- | ||
| api_key : str | ||
| Your Shade API key. | ||
| base_url : str | ||
| Override the default API base URL (useful for testing). | ||
| max_retries : int | ||
| Number of automatic retries on HTTP 429. Defaults to | ||
| ``DEFAULT_MAX_RETRIES`` (3). Set to ``0`` to disable. | ||
| timeout : float | ||
| Per-request socket timeout in seconds. | ||
| """ | ||
| def __init__(self): | ||
| pass | ||
| def process_payment(self, amount: float, currency: str): | ||
| _DEFAULT_BASE_URL = "https://api.shadeprotocol.io/v1" | ||
| def __init__( | ||
| self, | ||
| api_key: str = "", | ||
| base_url: str = "", | ||
| max_retries: int = DEFAULT_MAX_RETRIES, | ||
| timeout: float = 30.0, | ||
| ) -> None: | ||
| if not api_key: | ||
| raise ValueError("api_key must be a non-empty string") | ||
| self.api_key = api_key | ||
| self._base_url = base_url or self._DEFAULT_BASE_URL | ||
| self._http = SyncHTTPClient( | ||
| base_url=self._base_url, | ||
| api_key=api_key, | ||
| max_retries=max_retries, | ||
| timeout=timeout, | ||
| ) | ||
| self._async_http = AsyncHTTPClient( | ||
| base_url=self._base_url, | ||
| api_key=api_key, | ||
| max_retries=max_retries, | ||
| timeout=timeout, | ||
| ) | ||
| # ------------------------------------------------------------------ | ||
| # Sync API | ||
| # ------------------------------------------------------------------ | ||
| def process_payment(self, amount: float, currency: str) -> Dict[str, Any]: | ||
| """ | ||
| Process a payment (placeholder). | ||
| Process a payment (sync). | ||
| Parameters | ||
| ---------- | ||
| amount : float | ||
| Payment amount. | ||
| currency : str | ||
| ISO 4217 currency code (e.g. ``"USD"``). | ||
| Returns | ||
| ------- | ||
| dict | ||
| API response body. | ||
| """ | ||
| print(f"Processing payment of {amount} {currency}...") | ||
| return True | ||
| return self._http.request( | ||
| "POST", | ||
| "/payments", | ||
| {"amount": amount, "currency": currency}, | ||
| ) | ||
| # ------------------------------------------------------------------ | ||
| # Async API | ||
| # ------------------------------------------------------------------ | ||
| 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", | ||
| "/payments", | ||
| {"amount": amount, "currency": currency}, | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.