|
1 | | -""" |
2 | | -lomi. Python SDK Client |
3 | | -AUTO-GENERATED - Do not edit manually |
4 | | -""" |
| 1 | +"""lomi. Python SDK — generated from OpenAPI + public allowlist.""" |
5 | 2 |
|
6 | 3 | import requests |
7 | | -from typing import Optional, Dict, Any, List, Type, TypeVar |
| 4 | +from typing import Optional, Dict, Any |
| 5 | + |
8 | 6 | from .exceptions import LomiError, LomiAuthError, LomiNotFoundError |
9 | | -from .models import * |
10 | 7 | from .services import * |
11 | | -from pydantic import BaseModel |
12 | 8 |
|
13 | | -T = TypeVar("T", bound=BaseModel) |
| 9 | +def _flatten_data(data): |
| 10 | + if data is None: |
| 11 | + return None |
| 12 | + if hasattr(data, "model_dump"): |
| 13 | + return data.model_dump(exclude_unset=True) |
| 14 | + if hasattr(data, "dict"): |
| 15 | + return data.dict(exclude_unset=True) |
| 16 | + return data |
| 17 | + |
14 | 18 |
|
15 | 19 | class LomiClient: |
16 | | - """Main lomi. SDK client""" |
17 | | - |
| 20 | + """Merchant API client (public routes only).""" |
| 21 | + |
18 | 22 | def __init__( |
19 | 23 | self, |
20 | 24 | api_key: str, |
21 | 25 | base_url: str = "https://api.lomi.africa", |
22 | | - environment: str = "live" |
| 26 | + environment: str = "live", |
23 | 27 | ): |
24 | 28 | self.api_key = api_key |
25 | | - self.base_url = base_url if environment != "test" else "https://sandbox.api.lomi.africa" |
| 29 | + test_host = environment in ("test", "sandbox") or ( |
| 30 | + isinstance(environment, str) and environment.lower() == "test" |
| 31 | + ) |
| 32 | + self.base_url = ( |
| 33 | + base_url if not test_host else "https://sandbox.api.lomi.africa" |
| 34 | + ) |
26 | 35 | self.session = requests.Session() |
27 | | - self.session.headers.update({ |
28 | | - "X-API-KEY": api_key, |
29 | | - "Content-Type": "application/json", |
30 | | - }) |
31 | | - |
32 | | - # Initialize service instances |
| 36 | + self.session.headers.update( |
| 37 | + {"X-API-KEY": api_key, "Content-Type": "application/json"} |
| 38 | + ) |
33 | 39 | self.accounts = AccountsService(self) |
34 | | - self.organizations = OrganizationsService(self) |
| 40 | + self.beneficiary_payouts = BeneficiaryPayoutsService(self) |
| 41 | + self.charges = ChargesService(self) |
| 42 | + self.checkout_sessions = CheckoutSessionsService(self) |
35 | 43 | self.customers = CustomersService(self) |
36 | | - self.payment_requests = PaymentRequestsService(self) |
37 | | - self.transactions = TransactionsService(self) |
38 | | - self.refunds = RefundsService(self) |
39 | | - self.products = ProductsService(self) |
40 | | - self.subscriptions = SubscriptionsService(self) |
41 | 44 | self.discount_coupons = DiscountCouponsService(self) |
42 | | - self.checkout_sessions = CheckoutSessionsService(self) |
| 45 | + self.organizations = OrganizationsService(self) |
| 46 | + self.payment_intents = PaymentIntentsService(self) |
43 | 47 | self.payment_links = PaymentLinksService(self) |
| 48 | + self.payment_requests = PaymentRequestsService(self) |
44 | 49 | self.payouts = PayoutsService(self) |
45 | | - self.beneficiary_payouts = BeneficiaryPayoutsService(self) |
46 | | - self.webhooks = WebhooksService(self) |
| 50 | + self.products = ProductsService(self) |
| 51 | + self.refunds = RefundsService(self) |
| 52 | + self.subscriptions = SubscriptionsService(self) |
| 53 | + self.transactions = TransactionsService(self) |
47 | 54 | self.webhook_delivery_logs = WebhookDeliveryLogsService(self) |
48 | | - |
| 55 | + self.webhooks = WebhooksService(self) |
| 56 | + |
49 | 57 | def _request( |
50 | 58 | self, |
51 | 59 | method: str, |
52 | 60 | path: str, |
53 | | - model: Type[T] = None, |
54 | 61 | params: Optional[Dict[str, Any]] = None, |
55 | 62 | data: Optional[Dict[str, Any]] = None, |
56 | 63 | ) -> Any: |
57 | | - """Make an HTTP request to the API""" |
58 | 64 | url = f"{self.base_url}{path}" |
59 | | - |
60 | | - # Convert Pydantic models to dict if passed as data |
61 | | - json_data = data |
62 | | - if hasattr(data, 'dict'): |
63 | | - json_data = data.dict(exclude_unset=True) |
64 | | - |
| 65 | + json_data = _flatten_data(data) |
65 | 66 | try: |
66 | 67 | response = self.session.request( |
67 | 68 | method=method, |
68 | 69 | url=url, |
69 | 70 | params=params, |
70 | 71 | json=json_data, |
71 | 72 | ) |
72 | | - |
| 73 | + |
73 | 74 | if response.status_code == 401: |
74 | | - raise LomiAuthError("Invalid API key", response.status_code, response.json()) |
75 | | - elif response.status_code == 404: |
76 | | - raise LomiNotFoundError("Resource not found", response.status_code, response.json()) |
77 | | - elif response.status_code >= 400: |
78 | | - raise LomiError(f"API error: {response.text}", response.status_code, response.json() if response.text else None) |
79 | | - |
80 | | - resp_data = response.json() if response.text else None |
81 | | - |
82 | | - # If model class provided, parse response |
83 | | - if model and resp_data: |
84 | | - if isinstance(resp_data, list): |
85 | | - return [model(**item) for item in resp_data] |
86 | | - return model(**resp_data) |
87 | | - |
88 | | - return resp_data |
89 | | - |
| 75 | + raise LomiAuthError( |
| 76 | + "Invalid API key", |
| 77 | + response.status_code, |
| 78 | + response.json() if response.content else None, |
| 79 | + ) |
| 80 | + if response.status_code == 404: |
| 81 | + raise LomiNotFoundError( |
| 82 | + "Resource not found", |
| 83 | + response.status_code, |
| 84 | + response.json() if response.content else None, |
| 85 | + ) |
| 86 | + if response.status_code >= 400: |
| 87 | + raise LomiError( |
| 88 | + f"API error: {response.text}", |
| 89 | + response.status_code, |
| 90 | + response.json() if response.text else None, |
| 91 | + ) |
| 92 | + |
| 93 | + return response.json() if response.content else None |
90 | 94 | except requests.RequestException as e: |
91 | | - raise LomiError(f"Request failed: {str(e)}") |
| 95 | + raise LomiError(f"Request failed: {type(e).__name__}: {e}") from e |
| 96 | + |
0 commit comments