mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-04-28 17:32:38 +02:00
parent
7a55fb23fa
commit
9341dc292e
2 changed files with 16 additions and 3 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
import traceback
|
||||||
|
import warnings
|
||||||
|
|
||||||
from cachetools import TTLCache, cached
|
from cachetools import TTLCache, cached
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
@ -21,7 +24,14 @@ class CurrencyConverter(object, metaclass=Singleton):
|
||||||
|
|
||||||
@cached(cache=TTLCache(maxsize=1, ttl=86400))
|
@cached(cache=TTLCache(maxsize=1, ttl=86400))
|
||||||
def get_rates(self):
|
def get_rates(self):
|
||||||
rates = requests.get(self.api_url).json()["rates"]
|
try:
|
||||||
|
rates = requests.get(self.api_url).json()["rates"]
|
||||||
|
except Exception as e:
|
||||||
|
warnings.warn(
|
||||||
|
f"Call to {self.api_url} failed: {traceback.format_exception_only(e)[0].strip()}"
|
||||||
|
)
|
||||||
|
# In case of any exception, let's have an empty value
|
||||||
|
rates = {}
|
||||||
rates[self.no_currency] = 1.0
|
rates[self.no_currency] = 1.0
|
||||||
return rates
|
return rates
|
||||||
|
|
||||||
|
|
|
@ -383,11 +383,14 @@ class TestCurrencyConverter(unittest.TestCase):
|
||||||
self.assertEqual(result, 80.0)
|
self.assertEqual(result, 80.0)
|
||||||
|
|
||||||
def test_failing_remote(self):
|
def test_failing_remote(self):
|
||||||
with patch("requests.Response.json", new=lambda _: {}):
|
rates = {}
|
||||||
|
with patch("requests.Response.json", new=lambda _: {}), self.assertWarns(
|
||||||
|
UserWarning
|
||||||
|
):
|
||||||
# we need a non-patched converter, but it seems that MagickMock
|
# we need a non-patched converter, but it seems that MagickMock
|
||||||
# is mocking EVERY instance of the class method. Too bad.
|
# is mocking EVERY instance of the class method. Too bad.
|
||||||
rates = CurrencyConverter.get_rates(self.converter)
|
rates = CurrencyConverter.get_rates(self.converter)
|
||||||
self.assertDictEqual(rates, {CurrencyConverter.no_currency: 1})
|
self.assertDictEqual(rates, {CurrencyConverter.no_currency: 1})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue