jamsrpay 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jamsrpay-0.1.0/LICENSE +21 -0
- jamsrpay-0.1.0/MANIFEST.in +3 -0
- jamsrpay-0.1.0/PKG-INFO +95 -0
- jamsrpay-0.1.0/README.md +69 -0
- jamsrpay-0.1.0/pyproject.toml +41 -0
- jamsrpay-0.1.0/setup.cfg +4 -0
- jamsrpay-0.1.0/src/jamsrpay/__init__.py +133 -0
- jamsrpay-0.1.0/src/jamsrpay/client.py +105 -0
- jamsrpay-0.1.0/src/jamsrpay/config.py +161 -0
- jamsrpay-0.1.0/src/jamsrpay/errors.py +164 -0
- jamsrpay-0.1.0/src/jamsrpay/http.py +357 -0
- jamsrpay-0.1.0/src/jamsrpay/py.typed +0 -0
- jamsrpay-0.1.0/src/jamsrpay/resources/__init__.py +15 -0
- jamsrpay-0.1.0/src/jamsrpay/resources/assets.py +57 -0
- jamsrpay-0.1.0/src/jamsrpay/resources/invoices.py +198 -0
- jamsrpay-0.1.0/src/jamsrpay/resources/payment_intents.py +103 -0
- jamsrpay-0.1.0/src/jamsrpay/resources/store_assets.py +45 -0
- jamsrpay-0.1.0/src/jamsrpay/sse.py +106 -0
- jamsrpay-0.1.0/src/jamsrpay/types.py +483 -0
- jamsrpay-0.1.0/src/jamsrpay/webhooks.py +253 -0
- jamsrpay-0.1.0/src/jamsrpay.egg-info/PKG-INFO +95 -0
- jamsrpay-0.1.0/src/jamsrpay.egg-info/SOURCES.txt +27 -0
- jamsrpay-0.1.0/src/jamsrpay.egg-info/dependency_links.txt +1 -0
- jamsrpay-0.1.0/src/jamsrpay.egg-info/top_level.txt +1 -0
- jamsrpay-0.1.0/tests/test_client.py +201 -0
- jamsrpay-0.1.0/tests/test_fixtures.py +162 -0
- jamsrpay-0.1.0/tests/test_resources.py +142 -0
- jamsrpay-0.1.0/tests/test_sse.py +122 -0
- jamsrpay-0.1.0/tests/test_webhooks.py +46 -0
jamsrpay-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 JamsrPay
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
jamsrpay-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jamsrpay
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official zero-dependency Python SDK for the JamsrPay cryptocurrency payment gateway.
|
|
5
|
+
Author-email: JamsrPay <dev@jamsrpay.cloud>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Documentation, https://docs.jamsrpay.cloud
|
|
8
|
+
Project-URL: Homepage, https://jamsrpay.cloud
|
|
9
|
+
Project-URL: Repository, https://github.com/JamsrPay/sdks
|
|
10
|
+
Project-URL: Issues, https://github.com/JamsrPay/sdks/issues
|
|
11
|
+
Keywords: jamsrpay,crypto,payments,invoices,webhooks
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# jamsrpay (Python SDK)
|
|
28
|
+
|
|
29
|
+
Official zero-dependency Python SDK for the **JamsrPay** cryptocurrency payment gateway. Supports Python 3.10+, synchronous (`JamsrPay`) and asynchronous (`AsyncJamsrPay`) usage, Server-Sent Events streaming, automatic idempotency retries, RFC 7807 error normalization, and local HMAC-SHA256 webhook verification.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install jamsrpay
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import os
|
|
41
|
+
from jamsrpay import AssetId, FiatCurrencyId, JamsrPay, NetworkId
|
|
42
|
+
|
|
43
|
+
client = JamsrPay(api_key=os.environ["JAMSRPAY_API_KEY"])
|
|
44
|
+
|
|
45
|
+
intent = client.payment_intents.create(
|
|
46
|
+
pricing_amount="49.99",
|
|
47
|
+
pricing_currency=FiatCurrencyId.USD,
|
|
48
|
+
external_id="order_1042",
|
|
49
|
+
redirect_url="https://example.com/checkout/success",
|
|
50
|
+
payment_title="Order #1042",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
invoice = client.invoices.create_for_payment(
|
|
54
|
+
intent.id,
|
|
55
|
+
asset_id=AssetId.USDT,
|
|
56
|
+
network_id=NetworkId.TRON_MAINNET,
|
|
57
|
+
)
|
|
58
|
+
print(invoice.checkout_url, invoice.address.address if invoice.address else None)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Public Discovery & SSE Streaming
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
public = JamsrPay(environment="sandbox")
|
|
65
|
+
|
|
66
|
+
assets = public.assets.list()
|
|
67
|
+
store_assets = public.store_assets.list(store_id)
|
|
68
|
+
|
|
69
|
+
for event in public.invoices.stream_events(invoice.id):
|
|
70
|
+
print(event.status, event.paid_amount.display if event.paid_amount else None)
|
|
71
|
+
if event.status in ("PAID", "EXPIRED"):
|
|
72
|
+
break
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Webhook Verification
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from jamsrpay import JamsrPayWebhookError, construct_webhook_event
|
|
79
|
+
|
|
80
|
+
try:
|
|
81
|
+
event = construct_webhook_event(
|
|
82
|
+
raw_body=request_body_bytes,
|
|
83
|
+
headers_or_signature=request_headers,
|
|
84
|
+
secret=os.environ["JAMSRPAY_WEBHOOK_SECRET"],
|
|
85
|
+
)
|
|
86
|
+
print(event.type, event.data)
|
|
87
|
+
except JamsrPayWebhookError as exc:
|
|
88
|
+
print("Invalid webhook:", exc.code, str(exc))
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Running Tests
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
PYTHONPATH=src python3 -m unittest discover -s tests -t .
|
|
95
|
+
```
|
jamsrpay-0.1.0/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# jamsrpay (Python SDK)
|
|
2
|
+
|
|
3
|
+
Official zero-dependency Python SDK for the **JamsrPay** cryptocurrency payment gateway. Supports Python 3.10+, synchronous (`JamsrPay`) and asynchronous (`AsyncJamsrPay`) usage, Server-Sent Events streaming, automatic idempotency retries, RFC 7807 error normalization, and local HMAC-SHA256 webhook verification.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install jamsrpay
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import os
|
|
15
|
+
from jamsrpay import AssetId, FiatCurrencyId, JamsrPay, NetworkId
|
|
16
|
+
|
|
17
|
+
client = JamsrPay(api_key=os.environ["JAMSRPAY_API_KEY"])
|
|
18
|
+
|
|
19
|
+
intent = client.payment_intents.create(
|
|
20
|
+
pricing_amount="49.99",
|
|
21
|
+
pricing_currency=FiatCurrencyId.USD,
|
|
22
|
+
external_id="order_1042",
|
|
23
|
+
redirect_url="https://example.com/checkout/success",
|
|
24
|
+
payment_title="Order #1042",
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
invoice = client.invoices.create_for_payment(
|
|
28
|
+
intent.id,
|
|
29
|
+
asset_id=AssetId.USDT,
|
|
30
|
+
network_id=NetworkId.TRON_MAINNET,
|
|
31
|
+
)
|
|
32
|
+
print(invoice.checkout_url, invoice.address.address if invoice.address else None)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Public Discovery & SSE Streaming
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
public = JamsrPay(environment="sandbox")
|
|
39
|
+
|
|
40
|
+
assets = public.assets.list()
|
|
41
|
+
store_assets = public.store_assets.list(store_id)
|
|
42
|
+
|
|
43
|
+
for event in public.invoices.stream_events(invoice.id):
|
|
44
|
+
print(event.status, event.paid_amount.display if event.paid_amount else None)
|
|
45
|
+
if event.status in ("PAID", "EXPIRED"):
|
|
46
|
+
break
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Webhook Verification
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from jamsrpay import JamsrPayWebhookError, construct_webhook_event
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
event = construct_webhook_event(
|
|
56
|
+
raw_body=request_body_bytes,
|
|
57
|
+
headers_or_signature=request_headers,
|
|
58
|
+
secret=os.environ["JAMSRPAY_WEBHOOK_SECRET"],
|
|
59
|
+
)
|
|
60
|
+
print(event.type, event.data)
|
|
61
|
+
except JamsrPayWebhookError as exc:
|
|
62
|
+
print("Invalid webhook:", exc.code, str(exc))
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Running Tests
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
PYTHONPATH=src python3 -m unittest discover -s tests -t .
|
|
69
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "jamsrpay"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Official zero-dependency Python SDK for the JamsrPay cryptocurrency payment gateway."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "JamsrPay", email = "dev@jamsrpay.cloud" }]
|
|
13
|
+
keywords = ["jamsrpay", "crypto", "payments", "invoices", "webhooks"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = []
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Documentation = "https://docs.jamsrpay.cloud"
|
|
30
|
+
Homepage = "https://jamsrpay.cloud"
|
|
31
|
+
Repository = "https://github.com/JamsrPay/sdks"
|
|
32
|
+
Issues = "https://github.com/JamsrPay/sdks/issues"
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.packages.find]
|
|
35
|
+
where = ["src"]
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.package-data]
|
|
38
|
+
jamsrpay = ["py.typed"]
|
|
39
|
+
|
|
40
|
+
[tool.pytest.ini_options]
|
|
41
|
+
testpaths = ["tests"]
|
jamsrpay-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
from .client import AsyncJamsrPay, JamsrPay
|
|
2
|
+
from .config import (
|
|
3
|
+
DEFAULT_MAX_RETRIES,
|
|
4
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
5
|
+
ENVIRONMENT_BASE_URLS,
|
|
6
|
+
HttpRequest,
|
|
7
|
+
HttpResponse,
|
|
8
|
+
LIVE_API_KEY_PREFIX,
|
|
9
|
+
RequestOptions,
|
|
10
|
+
SDK_VERSION,
|
|
11
|
+
StreamResponse,
|
|
12
|
+
SyncTransport,
|
|
13
|
+
TEST_API_KEY_PREFIX,
|
|
14
|
+
infer_environment_from_api_key,
|
|
15
|
+
resolve_client_config,
|
|
16
|
+
)
|
|
17
|
+
from .errors import (
|
|
18
|
+
JamsrPayApiError,
|
|
19
|
+
JamsrPayConfigError,
|
|
20
|
+
JamsrPayError,
|
|
21
|
+
JamsrPayNetworkError,
|
|
22
|
+
JamsrPayWebhookError,
|
|
23
|
+
is_retryable_status,
|
|
24
|
+
normalize_api_error,
|
|
25
|
+
parse_retry_after_seconds,
|
|
26
|
+
)
|
|
27
|
+
from .types import (
|
|
28
|
+
Asset,
|
|
29
|
+
AssetId,
|
|
30
|
+
AssetStatus,
|
|
31
|
+
AssetType,
|
|
32
|
+
ConfirmationDetails,
|
|
33
|
+
CryptoAddress,
|
|
34
|
+
CryptoTransactionHash,
|
|
35
|
+
ExchangeRate,
|
|
36
|
+
FiatCurrencyId,
|
|
37
|
+
Invoice,
|
|
38
|
+
InvoiceAdditionalStatus,
|
|
39
|
+
InvoiceAmounts,
|
|
40
|
+
InvoiceConfirmationStartedWebhookData,
|
|
41
|
+
InvoiceCreatedWebhookData,
|
|
42
|
+
InvoiceExpiredWebhookData,
|
|
43
|
+
InvoicePaidWebhookData,
|
|
44
|
+
InvoicePaymentDetectedWebhookData,
|
|
45
|
+
InvoicePaymentIntent,
|
|
46
|
+
InvoiceRealtimeEvent,
|
|
47
|
+
InvoiceStatus,
|
|
48
|
+
InvoiceTransaction,
|
|
49
|
+
Money,
|
|
50
|
+
NetworkId,
|
|
51
|
+
PaymentIntent,
|
|
52
|
+
PaymentIntentStatus,
|
|
53
|
+
PricingCurrency,
|
|
54
|
+
StoreAsset,
|
|
55
|
+
StoreBranding,
|
|
56
|
+
WebhookEvent,
|
|
57
|
+
WebhookMoney,
|
|
58
|
+
)
|
|
59
|
+
from .webhooks import (
|
|
60
|
+
DEFAULT_WEBHOOK_TOLERANCE_SECONDS,
|
|
61
|
+
ParsedWebhookSignature,
|
|
62
|
+
construct_webhook_event,
|
|
63
|
+
parse_webhook_signature_header,
|
|
64
|
+
sign_webhook_payload,
|
|
65
|
+
verify_webhook_signature,
|
|
66
|
+
webhooks,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
__version__ = SDK_VERSION
|
|
70
|
+
|
|
71
|
+
__all__ = [
|
|
72
|
+
"Asset",
|
|
73
|
+
"AssetId",
|
|
74
|
+
"AssetStatus",
|
|
75
|
+
"AssetType",
|
|
76
|
+
"AsyncJamsrPay",
|
|
77
|
+
"ConfirmationDetails",
|
|
78
|
+
"CryptoAddress",
|
|
79
|
+
"CryptoTransactionHash",
|
|
80
|
+
"DEFAULT_MAX_RETRIES",
|
|
81
|
+
"DEFAULT_TIMEOUT_SECONDS",
|
|
82
|
+
"DEFAULT_WEBHOOK_TOLERANCE_SECONDS",
|
|
83
|
+
"ENVIRONMENT_BASE_URLS",
|
|
84
|
+
"ExchangeRate",
|
|
85
|
+
"FiatCurrencyId",
|
|
86
|
+
"HttpRequest",
|
|
87
|
+
"HttpResponse",
|
|
88
|
+
"Invoice",
|
|
89
|
+
"InvoiceAdditionalStatus",
|
|
90
|
+
"InvoiceAmounts",
|
|
91
|
+
"InvoiceConfirmationStartedWebhookData",
|
|
92
|
+
"InvoiceCreatedWebhookData",
|
|
93
|
+
"InvoiceExpiredWebhookData",
|
|
94
|
+
"InvoicePaidWebhookData",
|
|
95
|
+
"InvoicePaymentDetectedWebhookData",
|
|
96
|
+
"InvoicePaymentIntent",
|
|
97
|
+
"InvoiceRealtimeEvent",
|
|
98
|
+
"InvoiceStatus",
|
|
99
|
+
"InvoiceTransaction",
|
|
100
|
+
"JamsrPay",
|
|
101
|
+
"JamsrPayApiError",
|
|
102
|
+
"JamsrPayConfigError",
|
|
103
|
+
"JamsrPayError",
|
|
104
|
+
"JamsrPayNetworkError",
|
|
105
|
+
"JamsrPayWebhookError",
|
|
106
|
+
"LIVE_API_KEY_PREFIX",
|
|
107
|
+
"Money",
|
|
108
|
+
"NetworkId",
|
|
109
|
+
"ParsedWebhookSignature",
|
|
110
|
+
"PaymentIntent",
|
|
111
|
+
"PaymentIntentStatus",
|
|
112
|
+
"PricingCurrency",
|
|
113
|
+
"RequestOptions",
|
|
114
|
+
"SDK_VERSION",
|
|
115
|
+
"StoreAsset",
|
|
116
|
+
"StoreBranding",
|
|
117
|
+
"StreamResponse",
|
|
118
|
+
"SyncTransport",
|
|
119
|
+
"TEST_API_KEY_PREFIX",
|
|
120
|
+
"WebhookEvent",
|
|
121
|
+
"WebhookMoney",
|
|
122
|
+
"__version__",
|
|
123
|
+
"construct_webhook_event",
|
|
124
|
+
"infer_environment_from_api_key",
|
|
125
|
+
"is_retryable_status",
|
|
126
|
+
"normalize_api_error",
|
|
127
|
+
"parse_retry_after_seconds",
|
|
128
|
+
"parse_webhook_signature_header",
|
|
129
|
+
"resolve_client_config",
|
|
130
|
+
"sign_webhook_payload",
|
|
131
|
+
"verify_webhook_signature",
|
|
132
|
+
"webhooks",
|
|
133
|
+
]
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from typing import Any, Callable, Mapping, Optional
|
|
5
|
+
|
|
6
|
+
from .config import (
|
|
7
|
+
DEFAULT_MAX_RETRIES,
|
|
8
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
9
|
+
SyncTransport,
|
|
10
|
+
resolve_client_config,
|
|
11
|
+
)
|
|
12
|
+
from .http import HttpClient
|
|
13
|
+
from .resources.assets import AssetsResource, AsyncAssetsResource
|
|
14
|
+
from .resources.invoices import AsyncInvoicesResource, InvoicesResource
|
|
15
|
+
from .resources.payment_intents import AsyncPaymentIntentsResource, PaymentIntentsResource
|
|
16
|
+
from .resources.store_assets import AsyncStoreAssetsResource, StoreAssetsResource
|
|
17
|
+
from .webhooks import webhooks
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class JamsrPay:
|
|
21
|
+
"""Synchronous JamsrPay API client."""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
*,
|
|
26
|
+
api_key: Optional[str] = None,
|
|
27
|
+
environment: Optional[str] = None,
|
|
28
|
+
base_url: Optional[str] = None,
|
|
29
|
+
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS,
|
|
30
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
31
|
+
auto_idempotency: bool = True,
|
|
32
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
33
|
+
user_agent: Optional[str] = None,
|
|
34
|
+
transport: Optional[SyncTransport] = None,
|
|
35
|
+
sleep_fn: Callable[[float], Any] = time.sleep,
|
|
36
|
+
) -> None:
|
|
37
|
+
resolved = resolve_client_config(
|
|
38
|
+
api_key=api_key,
|
|
39
|
+
environment=environment,
|
|
40
|
+
base_url=base_url,
|
|
41
|
+
timeout_seconds=timeout_seconds,
|
|
42
|
+
max_retries=max_retries,
|
|
43
|
+
auto_idempotency=auto_idempotency,
|
|
44
|
+
default_headers=default_headers,
|
|
45
|
+
user_agent=user_agent,
|
|
46
|
+
)
|
|
47
|
+
self.environment = resolved.environment
|
|
48
|
+
self.base_url = resolved.base_url
|
|
49
|
+
self.webhooks = webhooks
|
|
50
|
+
|
|
51
|
+
http = HttpClient(resolved, transport=transport, sleep_fn=sleep_fn)
|
|
52
|
+
self.invoices = InvoicesResource(http)
|
|
53
|
+
self.payment_intents = PaymentIntentsResource(http)
|
|
54
|
+
self.assets = AssetsResource(http)
|
|
55
|
+
self.store_assets = StoreAssetsResource(http)
|
|
56
|
+
|
|
57
|
+
def __enter__(self) -> JamsrPay:
|
|
58
|
+
return self
|
|
59
|
+
|
|
60
|
+
def __exit__(self, *_args: object) -> None:
|
|
61
|
+
return None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class AsyncJamsrPay:
|
|
65
|
+
"""Asynchronous JamsrPay API client compatible with `asyncio`."""
|
|
66
|
+
|
|
67
|
+
def __init__(
|
|
68
|
+
self,
|
|
69
|
+
*,
|
|
70
|
+
api_key: Optional[str] = None,
|
|
71
|
+
environment: Optional[str] = None,
|
|
72
|
+
base_url: Optional[str] = None,
|
|
73
|
+
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS,
|
|
74
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
75
|
+
auto_idempotency: bool = True,
|
|
76
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
77
|
+
user_agent: Optional[str] = None,
|
|
78
|
+
transport: Optional[SyncTransport] = None,
|
|
79
|
+
sleep_fn: Callable[[float], Any] = time.sleep,
|
|
80
|
+
) -> None:
|
|
81
|
+
self._sync = JamsrPay(
|
|
82
|
+
api_key=api_key,
|
|
83
|
+
environment=environment,
|
|
84
|
+
base_url=base_url,
|
|
85
|
+
timeout_seconds=timeout_seconds,
|
|
86
|
+
max_retries=max_retries,
|
|
87
|
+
auto_idempotency=auto_idempotency,
|
|
88
|
+
default_headers=default_headers,
|
|
89
|
+
user_agent=user_agent,
|
|
90
|
+
transport=transport,
|
|
91
|
+
sleep_fn=sleep_fn,
|
|
92
|
+
)
|
|
93
|
+
self.environment = self._sync.environment
|
|
94
|
+
self.base_url = self._sync.base_url
|
|
95
|
+
self.webhooks = webhooks
|
|
96
|
+
self.invoices = AsyncInvoicesResource(self._sync.invoices)
|
|
97
|
+
self.payment_intents = AsyncPaymentIntentsResource(self._sync.payment_intents)
|
|
98
|
+
self.assets = AsyncAssetsResource(self._sync.assets)
|
|
99
|
+
self.store_assets = AsyncStoreAssetsResource(self._sync.store_assets)
|
|
100
|
+
|
|
101
|
+
async def __aenter__(self) -> AsyncJamsrPay:
|
|
102
|
+
return self
|
|
103
|
+
|
|
104
|
+
async def __aexit__(self, *_args: object) -> None:
|
|
105
|
+
return None
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import Callable, Mapping, Optional, Protocol
|
|
6
|
+
|
|
7
|
+
Environment = str # "production" | "sandbox"
|
|
8
|
+
|
|
9
|
+
ENVIRONMENT_BASE_URLS: dict[str, str] = {
|
|
10
|
+
"production": "https://api.jamsrpay.cloud",
|
|
11
|
+
"sandbox": "https://api-sandbox.jamsrpay.cloud",
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
LIVE_API_KEY_PREFIX = "apisec_live_"
|
|
15
|
+
TEST_API_KEY_PREFIX = "apisec_test_"
|
|
16
|
+
|
|
17
|
+
DEFAULT_TIMEOUT_SECONDS = 30.0
|
|
18
|
+
DEFAULT_MAX_RETRIES = 2
|
|
19
|
+
SDK_VERSION = "0.1.0"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class HttpRequest:
|
|
24
|
+
method: str
|
|
25
|
+
url: str
|
|
26
|
+
headers: dict[str, str]
|
|
27
|
+
body: Optional[bytes]
|
|
28
|
+
timeout_seconds: float
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class HttpResponse:
|
|
33
|
+
status: int
|
|
34
|
+
headers: Mapping[str, str]
|
|
35
|
+
body: bytes
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class StreamResponse:
|
|
40
|
+
status: int
|
|
41
|
+
headers: Mapping[str, str]
|
|
42
|
+
chunks: object # Iterable[bytes] or AsyncIterable[bytes]
|
|
43
|
+
close: Callable[[], None] = field(default=lambda: None)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class SyncTransport(Protocol):
|
|
47
|
+
def send(self, request: HttpRequest) -> HttpResponse: ...
|
|
48
|
+
|
|
49
|
+
def stream(self, request: HttpRequest) -> StreamResponse: ...
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True)
|
|
53
|
+
class RequestOptions:
|
|
54
|
+
"""Per-request execution overrides."""
|
|
55
|
+
|
|
56
|
+
idempotency_key: Optional[str] = None
|
|
57
|
+
timeout_seconds: Optional[float] = None
|
|
58
|
+
headers: Optional[Mapping[str, str]] = None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass(frozen=True)
|
|
62
|
+
class ResolvedClientConfig:
|
|
63
|
+
api_key: Optional[str]
|
|
64
|
+
environment: str
|
|
65
|
+
base_url: str
|
|
66
|
+
timeout_seconds: float
|
|
67
|
+
max_retries: int
|
|
68
|
+
auto_idempotency: bool
|
|
69
|
+
default_headers: dict[str, str]
|
|
70
|
+
user_agent: str
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def infer_environment_from_api_key(api_key: str) -> Optional[str]:
|
|
74
|
+
"""Infers `'production'` or `'sandbox'` from `apisec_live_*` / `apisec_test_*`."""
|
|
75
|
+
if api_key.startswith(LIVE_API_KEY_PREFIX):
|
|
76
|
+
return "production"
|
|
77
|
+
if api_key.startswith(TEST_API_KEY_PREFIX):
|
|
78
|
+
return "sandbox"
|
|
79
|
+
return None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def resolve_client_config(
|
|
83
|
+
*,
|
|
84
|
+
api_key: Optional[str] = None,
|
|
85
|
+
environment: Optional[str] = None,
|
|
86
|
+
base_url: Optional[str] = None,
|
|
87
|
+
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS,
|
|
88
|
+
max_retries: int = DEFAULT_MAX_RETRIES,
|
|
89
|
+
auto_idempotency: bool = True,
|
|
90
|
+
default_headers: Optional[Mapping[str, str]] = None,
|
|
91
|
+
user_agent: Optional[str] = None,
|
|
92
|
+
) -> ResolvedClientConfig:
|
|
93
|
+
from .errors import JamsrPayConfigError
|
|
94
|
+
|
|
95
|
+
cleaned_key: Optional[str] = None
|
|
96
|
+
if api_key is not None:
|
|
97
|
+
cleaned_key = api_key.strip()
|
|
98
|
+
if not cleaned_key:
|
|
99
|
+
raise JamsrPayConfigError("api_key cannot be empty or whitespace.")
|
|
100
|
+
|
|
101
|
+
if environment is not None and environment not in ENVIRONMENT_BASE_URLS:
|
|
102
|
+
raise JamsrPayConfigError(
|
|
103
|
+
f"Unsupported environment {environment!r}. Expected 'production' or 'sandbox'."
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
cleaned_base_url: Optional[str] = None
|
|
107
|
+
if base_url is not None:
|
|
108
|
+
cleaned_base_url = base_url.strip().rstrip("/")
|
|
109
|
+
if not cleaned_base_url:
|
|
110
|
+
raise JamsrPayConfigError("base_url cannot be empty or whitespace.")
|
|
111
|
+
|
|
112
|
+
inferred = (
|
|
113
|
+
infer_environment_from_api_key(cleaned_key)
|
|
114
|
+
if cleaned_key is not None
|
|
115
|
+
else None
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
if (
|
|
119
|
+
inferred is not None
|
|
120
|
+
and environment is not None
|
|
121
|
+
and inferred != environment
|
|
122
|
+
and cleaned_base_url is None
|
|
123
|
+
):
|
|
124
|
+
raise JamsrPayConfigError(
|
|
125
|
+
f"API key prefix implies {inferred!r} environment, which conflicts with explicit environment {environment!r}. Pass matching credentials or specify base_url explicitly."
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
if (
|
|
129
|
+
cleaned_key is not None
|
|
130
|
+
and inferred is None
|
|
131
|
+
and environment is None
|
|
132
|
+
and cleaned_base_url is None
|
|
133
|
+
):
|
|
134
|
+
raise JamsrPayConfigError(
|
|
135
|
+
f"Unrecognized api_key prefix. Expected '{LIVE_API_KEY_PREFIX}*' or '{TEST_API_KEY_PREFIX}*', or specify 'environment' or 'base_url' explicitly."
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
resolved_env = environment or inferred or "production"
|
|
139
|
+
resolved_base_url = cleaned_base_url or ENVIRONMENT_BASE_URLS[resolved_env]
|
|
140
|
+
|
|
141
|
+
if (
|
|
142
|
+
isinstance(timeout_seconds, bool)
|
|
143
|
+
or not isinstance(timeout_seconds, (int, float))
|
|
144
|
+
or not math.isfinite(timeout_seconds)
|
|
145
|
+
or timeout_seconds <= 0
|
|
146
|
+
):
|
|
147
|
+
raise JamsrPayConfigError("timeout_seconds must be a positive finite number.")
|
|
148
|
+
|
|
149
|
+
if isinstance(max_retries, bool) or not isinstance(max_retries, int) or max_retries < 0:
|
|
150
|
+
raise JamsrPayConfigError("max_retries must be a non-negative integer.")
|
|
151
|
+
|
|
152
|
+
return ResolvedClientConfig(
|
|
153
|
+
api_key=cleaned_key,
|
|
154
|
+
environment=resolved_env,
|
|
155
|
+
base_url=resolved_base_url,
|
|
156
|
+
timeout_seconds=float(timeout_seconds),
|
|
157
|
+
max_retries=max_retries,
|
|
158
|
+
auto_idempotency=auto_idempotency,
|
|
159
|
+
default_headers=dict(default_headers or {}),
|
|
160
|
+
user_agent=user_agent or f"jamsrpay-python/{SDK_VERSION}",
|
|
161
|
+
)
|