-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Web programming contribution #2436
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
Web programming contribution #2436
Conversation
Hey @niranjanhegde144, TravisCI finished with status TravisBuddy Request Identifier: 62e11780-f812-11ea-8f9f-a900ee46d44b |
Clauss Can you take a look at this |
import requests | ||
|
||
API_KEY = "" # <-- Put your API Key here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to change this. For security reasons, we do not want to encourage users to put security credentials into Python files. Instead, we will get the key at runtime from an environment variable.
The user will run the program with this command:
AMDOREN_API_KEY="This is my key" python3 web_programming/currency_converter.py
import requests | |
API_KEY = "" # <-- Put your API Key here | |
import os | |
import requests | |
assert API_KEY := os.environ["AMDOREN_API_KEY"], ( | |
"Please put your API key in an environment variable." | |
) |
def convert_currency( | ||
baseCurrency: str = "USD", | ||
targetCurrency: str = "INR", | ||
amount: float = 1.0, | ||
apiKey: str = API_KEY, | ||
) -> str: | ||
"""https://www.amdoren.com/currency-api/""" | ||
res = requests.get( | ||
f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&\ | ||
amount={amount}" | ||
).json() | ||
if res["error"] == 0: | ||
return str(res["amount"]) | ||
return res["error_message"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def convert_currency( | |
baseCurrency: str = "USD", | |
targetCurrency: str = "INR", | |
amount: float = 1.0, | |
apiKey: str = API_KEY, | |
) -> str: | |
"""https://www.amdoren.com/currency-api/""" | |
res = requests.get( | |
f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&\ | |
amount={amount}" | |
).json() | |
if res["error"] == 0: | |
return str(res["amount"]) | |
return res["error_message"] | |
def convert_currency( | |
from_: str = "USD", | |
to: str = "INR", | |
amount: float = 1.0, | |
api_key: str = API_KEY | |
) -> str: | |
"""https://www.amdoren.com/currency-api/""" | |
params = locals() | |
params["from"] = params.pop("from_") # 'from' is a Python keyword | |
res = requests.get(URL_BASE, params=params).json() | |
return str(res["amount"] if res["error"] == 0 else res["error_message"]) |
There are nice advantages in requests and locals() if our function parameter names exactly match the API.
base_currency = input("Enter base currency: ").strip() | ||
target_currency = input("Enter target currency: ").strip() | ||
amount = float(input("Enter the amount: ").strip()) | ||
print( | ||
convert_currency( | ||
baseCurrency=base_currency, targetCurrency=target_currency, amount=amount | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
base_currency = input("Enter base currency: ").strip() | |
target_currency = input("Enter target currency: ").strip() | |
amount = float(input("Enter the amount: ").strip()) | |
print( | |
convert_currency( | |
baseCurrency=base_currency, targetCurrency=target_currency, amount=amount | |
) | |
) | |
print( | |
convert_currency( | |
input("Enter from currency: ").strip(), | |
input("Enter to currency: ").strip(), | |
float(input("Enter the amount: ").strip()) | |
) | |
) |
…into web_programming_contribution
6f96b53
to
bef8610
Compare
Hey @niranjanhegde144, TravisCI finished with status TravisBuddy Request Identifier: 61ca28d0-ffdf-11ea-9882-0125ab0f75bf |
Travis tests have failedHey @niranjanhegde144, TravisBuddy Request Identifier: 268e7950-ffe0-11ea-9882-0125ab0f75bf |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
* Currency Converter * currency converter * Currency Converter * currency converter * implemented changes * Implemented changes requested * TESTING = os.getenv("CONTINUOUS_INTEGRATION", False) * Update currency_converter.py * Update currency_converter.py Co-authored-by: Christian Clauss <[email protected]>
* Currency Converter * currency converter * Currency Converter * currency converter * implemented changes * Implemented changes requested * TESTING = os.getenv("CONTINUOUS_INTEGRATION", False) * Update currency_converter.py * Update currency_converter.py Co-authored-by: Christian Clauss <[email protected]>
* Currency Converter * currency converter * Currency Converter * currency converter * implemented changes * Implemented changes requested * TESTING = os.getenv("CONTINUOUS_INTEGRATION", False) * Update currency_converter.py * Update currency_converter.py Co-authored-by: Christian Clauss <[email protected]>
* Currency Converter * currency converter * Currency Converter * currency converter * implemented changes * Implemented changes requested * TESTING = os.getenv("CONTINUOUS_INTEGRATION", False) * Update currency_converter.py * Update currency_converter.py Co-authored-by: Christian Clauss <[email protected]>
Describe your change:
Added Currency Converter Program to web_programming directory.
Checklist:
Fixes: #{$ISSUE_NO}
.