Skip to content
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

Stop waiting forever when "currency not ready" #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions forex_python/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from decimal import Decimal

import requests
from requests.exceptions import ReadTimeout, ConnectTimeout
import simplejson as json


Expand Down Expand Up @@ -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)
except (ReadTimeout, ConnectTimeout):
raise RatesNotAvailableError("Currency Rates Source Not Ready")
if response.status_code == 200:
rates = self._decode_rates(response, date_str=date_str)
return rates
Expand All @@ -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:
Expand All @@ -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)
Expand Down