|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, Dict, Optional |
| 4 | + |
| 5 | +from . import config as _config |
| 6 | +from .config import Environment, validate_client_settings |
| 7 | +from .http import AsyncHTTPClient, SyncHTTPClient |
| 8 | + |
| 9 | + |
1 | 10 | class Gateway: |
2 | 11 | """ |
3 | 12 | Main entry point for the Shade Payment Gateway. |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + api_key : str |
| 17 | + Your Shade API key. |
| 18 | + environment : Environment |
| 19 | + Controls the Stellar network passphrase and the default API URL. |
| 20 | + Defaults to ``Environment.MAINNET``. |
| 21 | + api_base : str, optional |
| 22 | + Override the API host for this client (useful for local dev or staging). |
| 23 | + Takes precedence over the module-level ``shade.api_base`` and the |
| 24 | + URL derived from ``environment``. Trailing slashes are trimmed. |
| 25 | + Intended for development and testing only. |
| 26 | + base_url : str |
| 27 | + Deprecated. Prefer ``api_base``. |
| 28 | + max_retries : int, optional |
| 29 | + Number of automatic retries on HTTP 429 and transient failures. |
| 30 | + Defaults to the module-level ``shade.max_retries`` (3). Set to ``0`` |
| 31 | + to disable auto-retry. |
| 32 | + timeout : float, optional |
| 33 | + Per-request socket timeout in seconds. Defaults to the module-level |
| 34 | + ``shade.timeout`` (30.0). |
4 | 35 | """ |
5 | | - def __init__(self): |
6 | | - pass |
7 | 36 |
|
8 | | - def process_payment(self, amount: float, currency: str): |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + api_key: str = "", |
| 40 | + environment: Environment = Environment.MAINNET, |
| 41 | + api_base: Optional[str] = None, |
| 42 | + base_url: str = "", |
| 43 | + max_retries: Optional[int] = None, |
| 44 | + timeout: Optional[float] = None, |
| 45 | + ) -> None: |
| 46 | + if not api_key: |
| 47 | + raise ValueError("api_key must be a non-empty string") |
| 48 | + self.api_key = api_key |
| 49 | + self.environment = environment |
| 50 | + |
| 51 | + resolved_max_retries = ( |
| 52 | + _config.max_retries if max_retries is None else max_retries |
| 53 | + ) |
| 54 | + resolved_timeout = _config.timeout if timeout is None else timeout |
| 55 | + validate_client_settings(resolved_timeout, resolved_max_retries) |
| 56 | + |
| 57 | + # Resolution order: explicit api_base > module-level shade.api_base |
| 58 | + # > legacy base_url > environment URL |
| 59 | + resolved = api_base or _config.api_base or base_url or environment.base_url |
| 60 | + self._base_url = resolved.rstrip("/") |
| 61 | + |
| 62 | + self._http = SyncHTTPClient( |
| 63 | + base_url=self._base_url, |
| 64 | + api_key=api_key, |
| 65 | + max_retries=resolved_max_retries, |
| 66 | + timeout=resolved_timeout, |
| 67 | + ) |
| 68 | + self._async_http = AsyncHTTPClient( |
| 69 | + base_url=self._base_url, |
| 70 | + api_key=api_key, |
| 71 | + max_retries=resolved_max_retries, |
| 72 | + timeout=resolved_timeout, |
| 73 | + ) |
| 74 | + |
| 75 | + # ------------------------------------------------------------------ |
| 76 | + # Sync API |
| 77 | + # ------------------------------------------------------------------ |
| 78 | + |
| 79 | + def process_payment(self, amount: float, currency: str) -> Dict[str, Any]: |
9 | 80 | """ |
10 | | - Process a payment (placeholder). |
| 81 | + Process a payment (sync). |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + amount : float |
| 86 | + Payment amount. |
| 87 | + currency : str |
| 88 | + ISO 4217 currency code (e.g. ``"USD"``). |
| 89 | +
|
| 90 | + Returns |
| 91 | + ------- |
| 92 | + dict |
| 93 | + API response body. |
11 | 94 | """ |
12 | | - print(f"Processing payment of {amount} {currency}...") |
13 | | - return True |
| 95 | + return self._http.request( |
| 96 | + "POST", |
| 97 | + "/payments", |
| 98 | + {"amount": amount, "currency": currency}, |
| 99 | + ) |
| 100 | + |
| 101 | + # ------------------------------------------------------------------ |
| 102 | + # Async API |
| 103 | + # ------------------------------------------------------------------ |
| 104 | + |
| 105 | + async def process_payment_async( |
| 106 | + self, amount: float, currency: str |
| 107 | + ) -> Dict[str, Any]: |
| 108 | + """Async variant of :meth:`process_payment`.""" |
| 109 | + return await self._async_http.request( |
| 110 | + "POST", |
| 111 | + "/payments", |
| 112 | + {"amount": amount, "currency": currency}, |
| 113 | + ) |
0 commit comments