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