FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added new forex provider to fix "Currency Rates Source Not Ready" by ShanePadellaro · Pull Request #127 · MicroPyramid/forex-python · GitHub

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 5 additions & 3 deletions forex_python/converter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ def __init__(self, force_decimal=False):
self._force_decimal = force_decimal

def _source_url(self):
return "https://theforexapi.com/api/"
return "https://api.exchangerate.host/"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

why not allow specying source url as parameter?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Most apis are not going to be this similar in terms of api surface. This one is 95% there so still requires minor changes. I don't think having the provider url as a parameter makes sense unless theforexapi.com comes back online again.

If supporting multiple apis is required there needs to be some refactoring to abstract the api provider from the currency class.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It makes sense, you can host your own api since theforexapi is open source, there should be an option to override the url...


def _get_date_string(self, date_obj):
if date_obj is None:
return 'latest'
date_str = date_obj.strftime('%Y-%m-%d')
return date_str

def _decode_rates(self, response, use_decimal=False, date_str=None):
def _decode_rates(self, response, use_decimal=False, date_str=None, base_cur=None):
if self._force_decimal or use_decimal:
decoded_data = json.loads(response.text, use_decimal=True)
else:
decoded_data = response.json()
# if (date_str and date_str != 'latest' and date_str != decoded_data.get('date')):
# raise RatesNotAvailableError("Currency Rates Source Not Ready")
if base_cur != None and base_cur != decoded_data['base']:
raise RatesNotAvailableError("Currency Rates Source Not Ready")
return decoded_data.get('rates', {})

def _get_decoded_rate(
Expand All @@ -57,7 +59,7 @@ def get_rates(self, base_cur, date_obj=None):
source_url = self._source_url() + date_str
response = requests.get(source_url, params=payload)
if response.status_code == 200:
rates = self._decode_rates(response, date_str=date_str)
rates = self._decode_rates(response,date_str=date_str, base_cur=base_cur)
return rates
raise RatesNotAvailableError("Currency Rates Source Not Ready")

Expand Down

Back | FazBrowse Home | New Git URL