payra-sdk 1.2.2__tar.gz → 1.2.3__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.
- {payra_sdk-1.2.2/payra_sdk.egg-info → payra_sdk-1.2.3}/PKG-INFO +1 -1
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk/utils.py +27 -18
- {payra_sdk-1.2.2 → payra_sdk-1.2.3/payra_sdk.egg-info}/PKG-INFO +1 -1
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/pyproject.toml +1 -1
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/.env.example +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/LICENSE +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/MANIFEST.in +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/README.md +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk/__init__.py +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk/exceptions.py +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk/order_verification.py +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk/signature.py +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk.egg-info/SOURCES.txt +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk.egg-info/dependency_links.txt +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk.egg-info/requires.txt +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/payra_sdk.egg-info/top_level.txt +0 -0
- {payra_sdk-1.2.2 → payra_sdk-1.2.3}/setup.cfg +0 -0
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
4
|
import time
|
|
5
|
+
import json
|
|
5
6
|
import requests
|
|
6
7
|
from web3 import Web3
|
|
8
|
+
from pathlib import Path
|
|
7
9
|
from typing import Any, Dict, Union
|
|
8
10
|
from .exceptions import InvalidArgumentError
|
|
9
11
|
|
|
@@ -13,9 +15,8 @@ class PayraUtils:
|
|
|
13
15
|
Provides helper methods for conversions and ABI-related operations.
|
|
14
16
|
"""
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
_cache_ttl = 720 * 60
|
|
18
|
+
_cache_dir = Path.home() / ".payra_cache"
|
|
19
|
+
_cache_file = _cache_dir / "payra_cash_exchange_rate_cache.json"
|
|
19
20
|
|
|
20
21
|
@staticmethod
|
|
21
22
|
def find_function(abi: list[Dict[str, Any]], name: str) -> Dict[str, Any]:
|
|
@@ -79,43 +80,51 @@ class PayraUtils:
|
|
|
79
80
|
def convert_to_usd(amount: float, from_currency: str) -> float:
|
|
80
81
|
"""
|
|
81
82
|
Converts a given amount from another currency to USD using the ExchangeRate API.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
- optional EXCHANGE_RATE_CACHE_TIME (minutes)
|
|
83
|
+
Caches data in ~/.payra_cache/exchange_rate.json to minimize API usage.
|
|
84
|
+
Default cache time: 720 minutes (12 hours).
|
|
85
85
|
"""
|
|
86
86
|
|
|
87
87
|
api_key = os.getenv("EXCHANGE_RATE_API_KEY")
|
|
88
88
|
if not api_key:
|
|
89
89
|
raise InvalidArgumentError("EXCHANGE_RATE_API_KEY is not set in .env")
|
|
90
90
|
|
|
91
|
-
# TTL in minutes (if not exists, use default 720)
|
|
92
91
|
cache_minutes = int(os.getenv("EXCHANGE_RATE_CACHE_TIME", 720))
|
|
93
|
-
|
|
92
|
+
cache_ttl = cache_minutes * 60
|
|
94
93
|
|
|
95
94
|
api_url = f"https://v6.exchangerate-api.com/v6/{api_key}/latest/USD"
|
|
96
95
|
from_currency = from_currency.upper()
|
|
97
|
-
|
|
98
96
|
current_time = time.time()
|
|
99
97
|
|
|
100
|
-
#
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
98
|
+
# Ensure cache directory exists
|
|
99
|
+
PayraUtils._cache_dir.mkdir(parents=True, exist_ok=True)
|
|
100
|
+
|
|
101
|
+
# Try to read cache from file
|
|
102
|
+
data = None
|
|
103
|
+
if PayraUtils._cache_file.exists():
|
|
104
|
+
try:
|
|
105
|
+
with open(PayraUtils._cache_file, "r") as f:
|
|
106
|
+
cache = json.load(f)
|
|
107
|
+
if (current_time - cache.get("timestamp", 0)) < cache_ttl:
|
|
108
|
+
data = cache.get("data")
|
|
109
|
+
except Exception:
|
|
110
|
+
pass # Ignore invalid cache
|
|
111
|
+
|
|
112
|
+
# Fetch new data if no valid cache
|
|
113
|
+
if data is None:
|
|
107
114
|
try:
|
|
108
115
|
response = requests.get(api_url, timeout=10)
|
|
109
116
|
response.raise_for_status()
|
|
110
117
|
data = response.json()
|
|
111
118
|
|
|
112
|
-
|
|
113
|
-
PayraUtils.
|
|
119
|
+
# Save new cache
|
|
120
|
+
with open(PayraUtils._cache_file, "w") as f:
|
|
121
|
+
json.dump({"timestamp": current_time, "data": data}, f)
|
|
114
122
|
except requests.RequestException as e:
|
|
115
123
|
raise InvalidArgumentError(f"Failed to connect to ExchangeRate API: {e}")
|
|
116
124
|
except (KeyError, ValueError) as e:
|
|
117
125
|
raise InvalidArgumentError(f"Invalid data from ExchangeRate API: {e}")
|
|
118
126
|
|
|
127
|
+
# Validate and convert
|
|
119
128
|
if "conversion_rates" not in data or from_currency not in data["conversion_rates"]:
|
|
120
129
|
raise InvalidArgumentError(f"Conversion rate for {from_currency} not found in API response")
|
|
121
130
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|