Skip to content

Commit 10c7f4f

Browse files
committed
implemented changes
1 parent 47cb760 commit 10c7f4f

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

web_programming/currency_converter.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""
22
This is used to convert the currency using the Amdoren Currency API
3+
https://www.amdoren.com
34
"""
45

6+
import os
57
import requests
68

7-
API_KEY = "" # <-- Put your API Key here
8-
URL_BASE = "https://www.amdoren.com/api/currency.php"
99

10-
# If you are using it on a website and free api key please add the below
11-
# line in your website
12-
# Powered by <a href="https://www.amdoren.com">Amdoren</a>
10+
URL_BASE = "https://www.amdoren.com/api/currency.php"
11+
if "AMDOREN_API_KEY" not in os.environ:
12+
raise KeyError("Please put your API key in an environment variable.")
13+
API_KEY = os.environ["AMDOREN_API_KEY"]
1314

1415

1516
# Currency and their description
@@ -171,27 +172,23 @@
171172

172173

173174
def convert_currency(
174-
baseCurrency: str = "USD",
175-
targetCurrency: str = "INR",
176-
amount: float = 1.0,
177-
apiKey: str = API_KEY,
175+
from_: str = "USD",
176+
to: str = "INR",
177+
amount: float = 1.0,
178+
api_key: str = API_KEY
178179
) -> str:
179180
"""https://www.amdoren.com/currency-api/"""
180-
res = requests.get(
181-
f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&\
182-
amount={amount}"
183-
).json()
184-
if res["error"] == 0:
185-
return str(res["amount"])
186-
return res["error_message"]
181+
params = locals()
182+
params["from"] = params.pop("from_")
183+
res = requests.get(URL_BASE, params=params).json()
184+
return str(res["amount"]) if res["error"] == 0 else res["error_message"]
187185

188186

189187
if __name__ == "__main__":
190-
base_currency = input("Enter base currency: ").strip()
191-
target_currency = input("Enter target currency: ").strip()
192-
amount = float(input("Enter the amount: ").strip())
193188
print(
194189
convert_currency(
195-
baseCurrency=base_currency, targetCurrency=target_currency, amount=amount
190+
input("Enter from currency: ").strip(),
191+
input("Enter to currency: ").strip(),
192+
float(input("Enter the amount: ").strip()),
196193
)
197194
)

0 commit comments

Comments
 (0)