From 4fd2d3d1823cd45dd2a3befd0526fc3b7fc7e3b8 Mon Sep 17 00:00:00 2001 From: Redume Date: Sun, 22 Dec 2024 18:39:55 +0300 Subject: [PATCH] fix: Added an exception and fixed the returned result on error --- utils/convert.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/utils/convert.py b/utils/convert.py index a03c0b5..546b242 100644 --- a/utils/convert.py +++ b/utils/convert.py @@ -54,22 +54,26 @@ class Converter: self.conv_amount = float(conv_amount) + def kekkai(self) -> bool: date = datetime.today().strftime('%Y-%m-%d') - res = requests.get(f'{config['kekkai_instance']}/api/getRate/', { - 'from_currency': self.from_currency, - 'conv_currency': self.conv_currency, - 'date': date - }, timeout=3) + try: + res = requests.get(f'{config['kekkai_instance']}/api/getRate/', { + 'from_currency': self.from_currency, + 'conv_currency': self.conv_currency, + 'date': date + }, timeout=3) - data = res.json() + data = res.json() - if not HTTPStatus(res.status_code).is_success: - return None + if not HTTPStatus(res.status_code).is_success: + return False - self.conv_amount = float(data.get('rate') * self.amount) + self.conv_amount = float(data.get('rate') * self.amount) - return True + return True + except requests.exceptions.ConnectionError: + return False