-
Notifications
You must be signed in to change notification settings - Fork 0
/
frankfurter.py
60 lines (35 loc) · 1.56 KB
/
frankfurter.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from api import get_url
import json
BASE_URL = "https://api.frankfurter.app"
def get_currencies_list():
"""Function that will call the relevant API endpoint from Frankfurter \
in order to get the list of available currencies."""
# Endpoint: https://www.frankfurter.app/docs/#currencies
url = f"{BASE_URL}/currencies"
status_code, response = get_url(url)
if status_code == 200:
return json.loads(response)
else:
return None
def get_latest_rates(from_currency, to_currency, amount):
"""Function that will call the relevant API endpoint from Frankfurter in order to get the
latest conversion rate between the provided currencies. """
# Endpoint: https://www.frankfurter.app/docs/#latest
url = f"{BASE_URL}/latest?&from={from_currency}&to={to_currency}"
status_code, response = get_url(url)
if status_code == 200:
response = json.loads(response)
return response["date"], response["rates"][to_currency]
else:
return None, None
def get_historical_rate(from_currency, to_currency, from_date, amount):
"""Function that will call the relevant API endpoint from Frankfurter in order to get
the conversion rate for the given currencies and date"""
# Endpoint: https://www.frankfurter.app/docs/#historical
url = f"{BASE_URL}/{from_date}?&from={from_currency}&to={to_currency}"
status_code, response = get_url(url)
if status_code == 200:
response = json.loads(response)
return response["rates"][to_currency]
else:
return None