-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_key.py
More file actions
51 lines (40 loc) · 1.49 KB
/
Copy pathapi_key.py
File metadata and controls
51 lines (40 loc) · 1.49 KB
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
import currencyapicom
from everapi import ApiError
def get_client_api():
"""
Function to create and return an instance of the currencyapicom.Client.
Returns:
currencyapicom.Client: Instance of the currencyapicom.Client or None if an error occurs.
"""
try:
client = currencyapicom.Client(
'cur_live_ua5A26uZHtydGAvuoRnDLazKMfUFZVvhQWRS5CBu'
)
return client
except Exception as e:
print(f"Error while creating API client: {e}")
return None
def api_currency_data(date: str, base_currency: str, currencies: list):
"""
Function to fetch currency exchange rate data from an external API.
Args:
date (str): Date for which currency exchange rates are requested.
base_currency (str): The base currency for conversion.
currencies (list): List of target currencies to retrieve exchange rates for.
Returns:
dict or bool: Dictionary containing currency exchange rate data for the specified date,
or False if an error occurs.
"""
client = get_client_api()
response = None
try:
response = client.historical(date, base_currency, currencies)
except ApiError:
print('Please check your input data or API-KEY')
if response:
data = response.get('data', None)
if data is None:
print(response.get('message')) # Czasami API nie ma danych np. 2022-02-02
return False
else:
return data