-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (43 loc) · 1.41 KB
/
app.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
60
61
from chalice import Chalice, BadRequestError
import pycountry
app = Chalice(app_name='country-data')
app.debug = True
@app.route('/')
def index():
return {'hello': 'world'}
@app.route('/country-list', methods=['GET'], cors=True)
def get_country_list():
"""
This function returns all countries.
Args: N/A
Returns: List of country in JSON format.
"""
data = []
for country in pycountry.countries:
record = {}
record["code"] = country.alpha_2
name = country.name
record["name"] = name
data.append(record)
return data
@app.route('/currency/{country}', methods=['GET'], cors=True)
def get_currency(country):
"""
This function returns currency based on country.
Args: country
Returns: Currency name in JSON format
"""
if country is None or len(country) == 0:
raise BadRequestError("Country is required in this REST APi")
result = pycountry.countries.search_fuzzy(country)
data = []
if result is None or len(result) == 0:
raise BadRequestError("Country {} is not availalbe".format(country))
else:
for country in result:
currency = {}
value = pycountry.currencies.get(numeric=country.numeric)
if value:
currency['currency'] = value.name
data.append(currency)
return data