Skip to content

Refactor currency_converter.py #5917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 28, 2022
Merged
9 changes: 5 additions & 4 deletions web_programming/currency_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
URL_BASE = "https://www.amdoren.com/api/currency.php"
TESTING = os.getenv("CI", False)
API_KEY = os.getenv("AMDOREN_API_KEY", "")

if not API_KEY and not TESTING:
raise KeyError("Please put your API key in an environment variable.")
raise KeyError("API key must be set as AMDOREN_API_KEY")


# Currency and their description
Expand Down Expand Up @@ -173,12 +174,12 @@


def convert_currency(
from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = API_KEY
from="USD", to="INR", amount=1.0, api_key=API_KEY
) -> str:
"""https://www.amdoren.com/currency-api/"""
params = locals()
params["from"] = params.pop("from_")
params = dict(from=from, to=to, amount=amount, api_key=api_key)
res = requests.get(URL_BASE, params=params).json()

return str(res["amount"]) if res["error"] == 0 else res["error_message"]


Expand Down