|
8 | 8 | import requests
|
9 | 9 |
|
10 | 10 | URL_BASE = "https://www.amdoren.com/api/currency.php"
|
11 |
| -TESTING = os.getenv("CI", "") |
12 |
| -API_KEY = os.getenv("AMDOREN_API_KEY", "") |
13 | 11 |
|
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 |
| - ) |
18 | 12 |
|
19 | 13 | # Currency and their description
|
20 | 14 | list_of_currencies = """
|
|
175 | 169 |
|
176 | 170 |
|
177 | 171 | def convert_currency(
|
178 |
| - from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = API_KEY |
| 172 | + from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = "" |
179 | 173 | ) -> str:
|
180 | 174 | """https://www.amdoren.com/currency-api/"""
|
| 175 | + # Instead of manually generating parameters |
181 | 176 | params = locals()
|
| 177 | + # from is a reserved keyword |
182 | 178 | params["from"] = params.pop("from_")
|
183 | 179 | res = requests.get(URL_BASE, params=params).json()
|
184 | 180 | return str(res["amount"]) if res["error"] == 0 else res["error_message"]
|
185 | 181 |
|
186 | 182 |
|
187 | 183 | if __name__ == "__main__":
|
| 184 | + TESTING = os.getenv("CI", "") |
| 185 | + API_KEY = os.getenv("AMDOREN_API_KEY", "") |
| 186 | + |
| 187 | + if not API_KEY and not TESTING: |
| 188 | + raise KeyError( |
| 189 | + "API key must be provided in the 'AMDOREN_API_KEY' environment variable." |
| 190 | + ) |
| 191 | + |
188 | 192 | print(
|
189 | 193 | convert_currency(
|
190 | 194 | input("Enter from currency: ").strip(),
|
191 | 195 | input("Enter to currency: ").strip(),
|
192 | 196 | float(input("Enter the amount: ").strip()),
|
| 197 | + API_KEY, |
193 | 198 | )
|
194 | 199 | )
|
0 commit comments