-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
98 lines (78 loc) · 2.65 KB
/
Copy pathvalidation.py
File metadata and controls
98 lines (78 loc) · 2.65 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from datetime import datetime
from api_key import get_client_api
import requests
def valid_list_currencies():
"""
Function to retrieve and validate the list of available currencies from the API.
Returns:
dict or False: Dictionary containing allowed currencies or False if an error occurs.
"""
try:
client = get_client_api()
if client:
result = client.currencies()
allowed_currencies = result.get('data').keys()
return allowed_currencies
else:
print("Currency list data failed.")
return False
except Exception as e:
print(f"Error while validating currencies: {e}")
return False
def validate_date(date_str: str, format_str="%Y-%m-%d") -> bool:
"""
Function to validate a date string.
Args:
date_str (str): Date string to validate.
format_str (str): Format string for the expected date format (default is "%Y-%m-%d").
Returns:
bool: True if the date string is valid, False otherwise.
"""
try:
datetime.strptime(date_str, format_str)
return True
except ValueError:
return False
def validate_currencies(input_currencies: list) -> bool:
"""
Function to validate a list of currencies.
Args:
input_currencies (list): List of currency codes to validate.
Returns:
bool: True if all currencies in the list are valid, False otherwise.
"""
list_of_currencies = valid_list_currencies()
if list_of_currencies:
return all(currency.upper() in list_of_currencies for currency in input_currencies)
else:
return False
def validate_amount(amount_str: str) -> bool:
"""
Function to validate an amount string.
Args:
amount_str (str): Amount string to validate.
Returns:
bool: True if the amount string is valid, False otherwise.
"""
return amount_str.replace('.', '', 1).isdigit()
def validate_base_currency(input_base_currency: str) -> bool:
"""
Function to validate a base currency.
Args:
input_base_currency (str): Base currency code to validate.
Returns:
bool: True if the base currency is valid, False otherwise.
"""
list_of_currencies = valid_list_currencies()
if list_of_currencies:
return input_base_currency.upper() in list_of_currencies
else:
return False
if __name__ == '__main__':
a = requests.post('http://127.0.0.1:8000/exchange/', json={
"input_currency": "USD",
"output_currencies": ['EUR', 'GBP'],
"amount": 50.46,
"date": "2021-02-23"
})
print(a.json())