Skip to content

Commit

Permalink
add bitstamp endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Mar 17, 2017
1 parent 4b462ea commit aba83be
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 13 deletions.
6 changes: 4 additions & 2 deletions luma/cryptocurrency/endpoint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class Endpoint(object):
"""
Basic endpoint.
"""
api_version = 'v1'
responseType = EndpointResponse

def __init__(self, coin='bitcoin', currency='USD', api_version='v1',
def __init__(self, coin='bitcoin', currency='USD', api_version=None,
timeout=4):
self.coin = coin
self.currency_code = currency
self.api_version = api_version
if api_version is not None:
self.api_version = api_version
self.timeout = timeout
self.currencies = self.get_supported_currencies()

Expand Down
41 changes: 41 additions & 0 deletions luma/cryptocurrency/endpoint/bitstamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2017 Thijs Triemstra and contributors
# See LICENSE.rst for details.

"""
Endpoint for bitstamp.net
:see: https://www.bitstamp.net/api/
"""

from datetime import datetime

from dateutil.tz.tz import tzutc

from . import Endpoint, EndpointResponse


class BitstampResponse(EndpointResponse):

def parse_price(self):
return float(self.json_data.get('last'))

def parse_price_in_btc(self):
return 1

def parse_timestamp(self):
return datetime.fromtimestamp(
int(self.json_data.get('timestamp')), tz=tzutc())


class Bitstamp(Endpoint):
api_version = 'v2'
responseType = BitstampResponse

def get_url(self):
base = 'https://www.bitstamp.net/api/{api_version}/ticker/btc{currency}/'

return base.format(
currency=self.currency_code.lower(),
api_version=self.api_version
)
3 changes: 2 additions & 1 deletion luma/cryptocurrency/endpoint/bpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
class BPIResponse(EndpointResponse):

def parse_price(self):
return self.json_data.get('bpi').get('USD').get('rate_float')
return self.json_data.get('bpi').get(
self.currency_code).get('rate_float')

def parse_price_in_btc(self):
return 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"currency": "EUR",
"country": "Euro"
},
{
"currency": "USD",
"country": "United States Dollar"
}
]
6 changes: 4 additions & 2 deletions luma/cryptocurrency/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ def run(device):

# endpoint
# from .endpoint.coinmarketcap import Coinmarketcap
from .endpoint.bpi import BPI
# from .endpoint.bpi import BPI
from .endpoint.bitstamp import Bitstamp
# ep = Coinmarketcap(coin='ethereum', currency='EUR')
ep = BPI()
# ep = BPI()
ep = Bitstamp()
data = format_data(ep)

try:
Expand Down
11 changes: 11 additions & 0 deletions tests/reference/bitstamp/v2/currentprice/bitcoin/USD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"high": "1250.71",
"last": "1103.20",
"timestamp": "1489722313",
"bid": "1101.33",
"vwap": "1187.63",
"volume": "16303.35767789",
"low": "1101.00",
"ask": "1103.49",
"open": "1172.00"
}
29 changes: 21 additions & 8 deletions tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@

import requests_mock

from luma.cryptocurrency.endpoint import bpi, coinmarketcap
from luma.cryptocurrency.endpoint import bpi, coinmarketcap, bitstamp

from .helpers import get_reference_json


class EndpointTest(object):
currency = 'USD'
currency_country = 'United States Dollar'
price_in_btc = 1

def setUp(self):
self.ep = self.endpointClass()
self.ep = self.endpointClass(currency=self.currency)

@requests_mock.Mocker()
def assert_response(self, m):
Expand All @@ -40,7 +44,7 @@ def test_supported_currencies(self):
currencies = self.ep.get_supported_currencies()

self.assertTrue(len(currencies) > 0)
self.assertEqual(self.ep.currency_country, "United States Dollar")
self.assertEqual(self.ep.currency_country, self.currency_country)

def test_supported_currency(self):
currency = 'EUR'
Expand All @@ -58,18 +62,27 @@ def test_unsupported_currency(self):


class BPITestCase(EndpointTest, unittest.TestCase):
currency = 'CNY'
currency_country = 'Chinese Yuan'
endpointClass = bpi.BPI
ref_json = 'bpi/v1/currentprice/bitcoin/USD.json'
ref_json = 'bpi/v1/currentprice/bitcoin/CNY.json'

price = 1259.232
price_in_btc = 1
timestamp = datetime(2017, 3, 16, 1, 26, tzinfo=tzutc())
price = 7952.5275
timestamp = datetime(2017, 3, 16, 1, 25, tzinfo=tzutc())


class CoinmarketcapTestCase(EndpointTest, unittest.TestCase):
currency = 'USD'
endpointClass = coinmarketcap.Coinmarketcap
ref_json = 'coinmarketcap/v1/currentprice/bitcoin/USD.json'

price = 1255.26
price_in_btc = 1
timestamp = datetime(2017, 3, 16, 0, 59, 26, tzinfo=tzutc())


class BitstampTestCase(EndpointTest, unittest.TestCase):
endpointClass = bitstamp.Bitstamp
ref_json = 'bitstamp/v2/currentprice/bitcoin/USD.json'

price = 1103.2
timestamp = datetime(2017, 3, 17, 3, 45, 13, tzinfo=tzutc())

0 comments on commit aba83be

Please sign in to comment.