-
Notifications
You must be signed in to change notification settings - Fork 198
Stop waiting forever when "currency not ready" #136
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||
from decimal import Decimal | ||||||
|
||||||
import requests | ||||||
from requests.exceptions import ReadTimeout, ConnectTimeout | ||||||
import simplejson as json | ||||||
|
||||||
|
||||||
|
@@ -55,7 +56,10 @@ def get_rates(self, base_cur, date_obj=None): | |||||
date_str = self._get_date_string(date_obj) | ||||||
payload = {'base': base_cur, 'rtype': 'fpy'} | ||||||
source_url = self._source_url() + date_str | ||||||
response = requests.get(source_url, params=payload) | ||||||
try: | ||||||
response = requests.get(source_url, params=payload, timeout=5) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider making the timeout value configurable (e.g., via a module-level constant or initialization parameter) rather than hardcoding 5 seconds. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
except (ReadTimeout, ConnectTimeout): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be better to catch the broader
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
raise RatesNotAvailableError("Currency Rates Source Not Ready") | ||||||
if response.status_code == 200: | ||||||
rates = self._decode_rates(response, date_str=date_str) | ||||||
return rates | ||||||
|
@@ -69,7 +73,10 @@ def get_rate(self, base_cur, dest_cur, date_obj=None): | |||||
date_str = self._get_date_string(date_obj) | ||||||
payload = {'base': base_cur, 'symbols': dest_cur, 'rtype': 'fpy'} | ||||||
source_url = self._source_url() + date_str | ||||||
response = requests.get(source_url, params=payload) | ||||||
try: | ||||||
response = requests.get(source_url, params=payload, timeout=5) | ||||||
except (ReadTimeout, ConnectTimeout): | ||||||
raise RatesNotAvailableError("Currency Rates Source Not Ready") | ||||||
if response.status_code == 200: | ||||||
rate = self._get_decoded_rate(response, dest_cur, date_str=date_str) | ||||||
if not rate: | ||||||
|
@@ -92,7 +99,10 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None): | |||||
date_str = self._get_date_string(date_obj) | ||||||
payload = {'base': base_cur, 'symbols': dest_cur, 'rtype': 'fpy'} | ||||||
source_url = self._source_url() + date_str | ||||||
response = requests.get(source_url, params=payload) | ||||||
try: | ||||||
response = requests.get(source_url, params=payload, timeout=5) | ||||||
except (ReadTimeout, ConnectTimeout): | ||||||
raise RatesNotAvailableError("Currency Rates Source Not Ready") | ||||||
if response.status_code == 200: | ||||||
rate = self._get_decoded_rate( | ||||||
response, dest_cur, use_decimal=use_decimal, date_str=date_str) | ||||||
|
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.
[nitpick] The try/except block is duplicated across three methods. Extract a helper method (e.g.
_safe_get
) to reduce duplication and centralize timeout handling.Copilot uses AI. Check for mistakes.