mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-13 14:09:50 +00:00
exchange_rate: raise for http status so empty hist rate dicts don't get saved
and if they had already been saved, treat them as if nothing was saved context: BitcoinAverage started restricting the historical API endpoints. they now deny unauthenticated requests. :/
This commit is contained in:
parent
1f7b56b455
commit
7b4bb19b34
1 changed files with 20 additions and 18 deletions
|
@ -9,7 +9,7 @@ import csv
|
||||||
import decimal
|
import decimal
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
import traceback
|
import traceback
|
||||||
from typing import Sequence
|
from typing import Sequence, Optional
|
||||||
|
|
||||||
from aiorpcx.curio import timeout_after, TaskTimeout, TaskGroup
|
from aiorpcx.curio import timeout_after, TaskTimeout, TaskGroup
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ class ExchangeBase(PrintError):
|
||||||
proxy = network.proxy if network else None
|
proxy = network.proxy if network else None
|
||||||
async with make_aiohttp_session(proxy) as session:
|
async with make_aiohttp_session(proxy) as session:
|
||||||
async with session.get(url) as response:
|
async with session.get(url) as response:
|
||||||
|
response.raise_for_status()
|
||||||
return await response.text()
|
return await response.text()
|
||||||
|
|
||||||
async def get_json(self, site, get_string):
|
async def get_json(self, site, get_string):
|
||||||
|
@ -54,6 +55,7 @@ class ExchangeBase(PrintError):
|
||||||
proxy = network.proxy if network else None
|
proxy = network.proxy if network else None
|
||||||
async with make_aiohttp_session(proxy) as session:
|
async with make_aiohttp_session(proxy) as session:
|
||||||
async with session.get(url) as response:
|
async with session.get(url) as response:
|
||||||
|
response.raise_for_status()
|
||||||
# set content_type to None to disable checking MIME type
|
# set content_type to None to disable checking MIME type
|
||||||
return await response.json(content_type=None)
|
return await response.json(content_type=None)
|
||||||
|
|
||||||
|
@ -76,31 +78,31 @@ class ExchangeBase(PrintError):
|
||||||
self.quotes = {}
|
self.quotes = {}
|
||||||
self.on_quotes()
|
self.on_quotes()
|
||||||
|
|
||||||
def read_historical_rates(self, ccy, cache_dir):
|
def read_historical_rates(self, ccy, cache_dir) -> Optional[dict]:
|
||||||
filename = os.path.join(cache_dir, self.name() + '_'+ ccy)
|
filename = os.path.join(cache_dir, self.name() + '_'+ ccy)
|
||||||
if os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
timestamp = os.stat(filename).st_mtime
|
return None
|
||||||
try:
|
timestamp = os.stat(filename).st_mtime
|
||||||
with open(filename, 'r', encoding='utf-8') as f:
|
try:
|
||||||
h = json.loads(f.read())
|
with open(filename, 'r', encoding='utf-8') as f:
|
||||||
h['timestamp'] = timestamp
|
h = json.loads(f.read())
|
||||||
except:
|
except:
|
||||||
h = None
|
return None
|
||||||
else:
|
if not h: # e.g. empty dict
|
||||||
h = None
|
return None
|
||||||
if h:
|
h['timestamp'] = timestamp
|
||||||
self.history[ccy] = h
|
self.history[ccy] = h
|
||||||
self.on_history()
|
self.on_history()
|
||||||
return h
|
return h
|
||||||
|
|
||||||
@log_exceptions
|
@log_exceptions
|
||||||
async def get_historical_rates_safe(self, ccy, cache_dir):
|
async def get_historical_rates_safe(self, ccy, cache_dir):
|
||||||
try:
|
try:
|
||||||
self.print_error("requesting fx history for", ccy)
|
self.print_error(f"requesting fx history for {ccy}")
|
||||||
h = await self.request_history(ccy)
|
h = await self.request_history(ccy)
|
||||||
self.print_error("received fx history for", ccy)
|
self.print_error(f"received fx history for {ccy}")
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
self.print_error("failed fx history:", repr(e))
|
self.print_error(f"failed fx history: {repr(e)}")
|
||||||
#traceback.print_exc()
|
#traceback.print_exc()
|
||||||
return
|
return
|
||||||
filename = os.path.join(cache_dir, self.name() + '_' + ccy)
|
filename = os.path.join(cache_dir, self.name() + '_' + ccy)
|
||||||
|
|
Loading…
Add table
Reference in a new issue