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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: payra_sdk
3
- Version: 1.2.2
3
+ Version: 1.2.3
4
4
  Summary: Python SDK for Payra payment signature generation (backend)
5
5
  Author: Your Name
6
6
  Author-email: Wraith <contact@payra.cash>
@@ -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
- _cache_data = None
17
- _cache_timestamp = 0
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
- Requires:
83
- - EXCHANGE_RATE_API_KEY (API key only)
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
- PayraUtils._cache_ttl = cache_minutes * 60
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
- # use cache if < TTL
101
- if (
102
- PayraUtils._cache_data is not None
103
- and (current_time - PayraUtils._cache_timestamp) < PayraUtils._cache_ttl
104
- ):
105
- data = PayraUtils._cache_data
106
- else:
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
- PayraUtils._cache_data = data
113
- PayraUtils._cache_timestamp = current_time
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
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: payra_sdk
3
- Version: 1.2.2
3
+ Version: 1.2.3
4
4
  Summary: Python SDK for Payra payment signature generation (backend)
5
5
  Author: Your Name
6
6
  Author-email: Wraith <contact@payra.cash>
@@ -2,7 +2,7 @@
2
2
 
3
3
  [project]
4
4
  name = "payra_sdk"
5
- version = "1.2.2"
5
+ version = "1.2.3"
6
6
  description = "Python SDK for Payra payment signature generation (backend)"
7
7
  readme = "README.md"
8
8
  authors = [{ name = "Wraith", email = "contact@payra.cash" }]
File without changes
File without changes
File without changes
File without changes
File without changes