Skip to content

Commit c8bd378

Browse files
committed
fix: Pytest locally fails due to API_KEY env variable (TheAlgorithms#8737)
1 parent 1faf10b commit c8bd378

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Diff for: web_programming/currency_converter.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77

88
import requests
99

10+
1011
URL_BASE = "https://www.amdoren.com/api/currency.php"
11-
TESTING = os.getenv("CI", "")
12-
API_KEY = os.getenv("AMDOREN_API_KEY", "")
1312

14-
if not API_KEY and not TESTING:
15-
raise KeyError(
16-
"API key must be provided in the 'AMDOREN_API_KEY' environment variable."
17-
)
1813

1914
# Currency and their description
2015
list_of_currencies = """
@@ -175,20 +170,31 @@
175170

176171

177172
def convert_currency(
178-
from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = API_KEY
173+
from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = ""
179174
) -> str:
180175
"""https://www.amdoren.com/currency-api/"""
176+
# Instead of manually generating parameters
181177
params = locals()
178+
# from is a reserved keyword
182179
params["from"] = params.pop("from_")
183180
res = requests.get(URL_BASE, params=params).json()
184181
return str(res["amount"]) if res["error"] == 0 else res["error_message"]
185182

186183

187184
if __name__ == "__main__":
185+
TESTING = os.getenv("CI", "")
186+
API_KEY = os.getenv("AMDOREN_API_KEY", "")
187+
188+
if not API_KEY and not TESTING:
189+
raise KeyError(
190+
"API key must be provided in the 'AMDOREN_API_KEY' environment variable."
191+
)
192+
188193
print(
189194
convert_currency(
190195
input("Enter from currency: ").strip(),
191196
input("Enter to currency: ").strip(),
192197
float(input("Enter the amount: ").strip()),
198+
API_KEY
193199
)
194200
)

0 commit comments

Comments
 (0)