-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextensions.py
36 lines (29 loc) · 1.33 KB
/
extensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import requests
import json
from config import API_KEY, currencies
class APIException(Exception):
pass
class Converter:
@staticmethod
def get_convert(curr_from, curr_to, amount):
try:
curr_from_key = currencies[curr_from]
except KeyError:
raise APIException(f'Валюта {curr_from} не найдена!\nСписок доступных валют см. /values')
try:
curr_to_key = currencies[curr_to]
except KeyError:
raise APIException(f'Валюта {curr_to} не найдена!\nСписок доступных валют см. /values')
if curr_from_key == curr_to_key:
raise APIException(f'Невозможно перевести одинаковые валюты {curr_from}')
try:
amount = float(amount.replace(',', '.'))
except ValueError:
raise APIException(f'Неудалось обработать количество: {amount}')
url = f"https://api.apilayer.com/exchangerates_data/convert?to={curr_to_key}&from={curr_from_key}&amount={amount}"
payload = {}
headers = {"apikey": API_KEY}
r = requests.request("GET", url, headers=headers, data=payload)
resp = json.loads(r.content)
result = resp['result']
return round(result, 3)