Merge branch 'patch-googlefinance2'

This commit is contained in:
Jack Robison 2017-09-07 14:10:37 -04:00
commit 1f4dfe1f93
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF
3 changed files with 33 additions and 36 deletions

View file

@ -27,7 +27,7 @@ at anytime.
*
### Changed
*
* Updated exchange rate tests for the lbry.io api
*
### Removed
@ -35,6 +35,11 @@ at anytime.
*
## [0.15.2] - 2017-09-07
### Changed
* Use lbry.io exchange rate API instead of google finance
## [0.15.1] - 2017-08-22
### Changed
* Bumped `lbryschema` requirement to 0.0.10 [see changelog](https://github.com/lbryio/lbryschema/blob/master/CHANGELOG.md#0010---2017-08-22)

View file

@ -129,26 +129,22 @@ class LBRYioFeed(MarketFeed):
return defer.succeed(1.0 / json_response['data']['lbc_btc'])
class GoogleBTCFeed(MarketFeed):
class LBRYioBTCFeed(MarketFeed):
def __init__(self):
MarketFeed.__init__(
self,
"USDBTC",
"Coinbase via Google finance",
'http://finance.google.com/finance/info',
{'client':'ig', 'q':'CURRENCY:USDBTC'},
COINBASE_FEE
"lbry.io",
"https://api.lbry.io/lbc/exchange_rate",
{},
0.0,
)
def _handle_response(self, response):
response = response[3:] # response starts with "// "
json_response = json.loads(response)[0]
if 'l' not in json_response:
raise InvalidExchangeRateResponse(self.name, 'last trade not found')
last_trade_price = float(json_response['l'])
if last_trade_price <= 0:
raise InvalidExchangeRateResponse(self.name, 'trade price was not positive')
return defer.succeed(last_trade_price)
json_response = json.loads(response)
if 'data' not in json_response:
raise InvalidExchangeRateResponse(self.name, 'result not found')
return defer.succeed(1.0 / json_response['data']['btc_usd'])
def get_default_market_feed(currency_pair):
@ -160,7 +156,7 @@ def get_default_market_feed(currency_pair):
assert currencies is not None
if currencies == ("USD", "BTC"):
return GoogleBTCFeed()
return LBRYioBTCFeed()
elif currencies == ("BTC", "LBC"):
return LBRYioFeed()
@ -199,6 +195,3 @@ class ExchangeRateManager(object):
def fee_dict(self):
return {market: market.rate.as_dict() for market in self.market_feeds}

View file

@ -1,7 +1,6 @@
from lbryschema.fee import Fee
from lbrynet.daemon import ExchangeRateManager
from lbrynet.core.Error import InvalidExchangeRateResponse
from twisted.trial import unittest
from twisted.internet import defer
from tests import util
@ -77,23 +76,7 @@ class FeeTest(unittest.TestCase):
market_feeds = [BTCLBCFeed()]
manager = DummyExchangeRateManager(market_feeds,rates)
with self.assertRaises(Exception):
result = manager.convert_currency(fee.currency, "LBC", fee.amount)
class GoogleBTCFeedTest(unittest.TestCase):
@defer.inlineCallbacks
def test_handle_response(self):
feed = ExchangeRateManager.GoogleBTCFeed()
response = '// [ { "id": "-2001" ,"t" : "USDBTC" ,"e" : "CURRENCY" ,"l" : "0.0008" ,"l_fix" : "" ,"l_cur" : "" ,"s": "0" ,"ltt":"" ,"lt" : "Feb 27, 10:21PM GMT" ,"lt_dts" : "2017-02-27T22:21:39Z" ,"c" : "-0.00001" ,"c_fix" : "" ,"cp" : "-0.917" ,"cp_fix" : "" ,"ccol" : "chr" ,"pcls_fix" : "" } ]'
out = yield feed._handle_response(response)
self.assertEqual(0.0008, out)
# check negative trade price throws exception
response = '// [ { "id": "-2001" ,"t" : "USDBTC" ,"e" : "CURRENCY" ,"l" : "-0.0008" ,"l_fix" : "" ,"l_cur" : "" ,"s": "0" ,"ltt":"" ,"lt" : "Feb 27, 10:21PM GMT" ,"lt_dts" : "2017-02-27T22:21:39Z" ,"c" : "-0.00001" ,"c_fix" : "" ,"cp" : "-0.917" ,"cp_fix" : "" ,"ccol" : "chr" ,"pcls_fix" : "" } ]'
with self.assertRaises(InvalidExchangeRateResponse):
out = yield feed._handle_response(response)
manager.convert_currency(fee.currency, "LBC", fee.amount)
class LBRYioFeedTest(unittest.TestCase):
@ -115,4 +98,20 @@ class LBRYioFeedTest(unittest.TestCase):
out = yield feed._handle_response(response)
class LBRYioBTCFeedTest(unittest.TestCase):
@defer.inlineCallbacks
def test_handle_response(self):
feed = ExchangeRateManager.LBRYioBTCFeed()
response ='{\"data\": {\"fresh\": 0, \"lbc_usd\": 0.05863062523378918, \"lbc_btc\": 5.065289549855739e-05, \"btc_usd\": 1157.498}, \"success\": true, \"error\": null}'
out = yield feed._handle_response(response)
expected = 1.0 / 1157.498
self.assertEqual(expected, out)
response='{}'
with self.assertRaises(InvalidExchangeRateResponse):
out = yield feed._handle_response(response)
response='{"success":true,"result":[]}'
with self.assertRaises(InvalidExchangeRateResponse):
out = yield feed._handle_response(response)