From c8bd3782f8e667443b9bd674718f45ef31967912 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Mon, 15 May 2023 17:08:18 +0100 Subject: [PATCH 1/2] fix: Pytest locally fails due to API_KEY env variable (#8737) --- web_programming/currency_converter.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index 69f2a2c4d421..25c0e712cb6b 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -7,14 +7,9 @@ import requests + URL_BASE = "https://www.amdoren.com/api/currency.php" -TESTING = os.getenv("CI", "") -API_KEY = os.getenv("AMDOREN_API_KEY", "") -if not API_KEY and not TESTING: - raise KeyError( - "API key must be provided in the 'AMDOREN_API_KEY' environment variable." - ) # Currency and their description list_of_currencies = """ @@ -175,20 +170,31 @@ def convert_currency( - from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = API_KEY + from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = "" ) -> str: """https://www.amdoren.com/currency-api/""" + # Instead of manually generating parameters params = locals() + # from is a reserved keyword params["from"] = params.pop("from_") res = requests.get(URL_BASE, params=params).json() return str(res["amount"]) if res["error"] == 0 else res["error_message"] if __name__ == "__main__": + TESTING = os.getenv("CI", "") + API_KEY = os.getenv("AMDOREN_API_KEY", "") + + if not API_KEY and not TESTING: + raise KeyError( + "API key must be provided in the 'AMDOREN_API_KEY' environment variable." + ) + print( convert_currency( input("Enter from currency: ").strip(), input("Enter to currency: ").strip(), float(input("Enter the amount: ").strip()), + API_KEY ) ) From 07b89bcc73c26f8a7a8df316444bda5d304176f0 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Mon, 15 May 2023 17:09:46 +0100 Subject: [PATCH 2/2] chore: Fix ruff errors --- web_programming/currency_converter.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py index 25c0e712cb6b..3bbcafa8f89b 100644 --- a/web_programming/currency_converter.py +++ b/web_programming/currency_converter.py @@ -7,7 +7,6 @@ import requests - URL_BASE = "https://www.amdoren.com/api/currency.php" @@ -188,13 +187,13 @@ def convert_currency( if not API_KEY and not TESTING: raise KeyError( "API key must be provided in the 'AMDOREN_API_KEY' environment variable." - ) + ) print( convert_currency( input("Enter from currency: ").strip(), input("Enter to currency: ").strip(), float(input("Enter the amount: ").strip()), - API_KEY + API_KEY, ) )