Skip to content

Commit 4335499

Browse files
authored
Merge pull request #162 from jlane9/gdax-improvements
Improvements to gdax public client
2 parents 958fff9 + 2e5c710 commit 4335499

File tree

4 files changed

+48
-35
lines changed

4 files changed

+48
-35
lines changed

gdax/public_client.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def __init__(self, api_url='https://api.gdax.com'):
2727
"""
2828
self.url = api_url.rstrip('/')
2929

30+
def _get(self, path, params=None):
31+
"""Perform get request"""
32+
33+
r = requests.get(self.url + path, params=params, timeout=30)
34+
# r.raise_for_status()
35+
return r.json()
36+
3037
def get_products(self):
3138
"""Get a list of available currency pairs for trading.
3239
@@ -45,9 +52,7 @@ def get_products(self):
4552
]
4653
4754
"""
48-
r = requests.get(self.url + '/products', timeout=30)
49-
# r.raise_for_status()
50-
return r.json()
55+
return self._get('/products')
5156

5257
def get_product_order_book(self, product_id, level=1):
5358
"""Get a list of open orders for a product.
@@ -84,11 +89,10 @@ def get_product_order_book(self, product_id, level=1):
8489
}
8590
8691
"""
87-
params = {'level': level}
88-
r = requests.get(self.url + '/products/{}/book'
89-
.format(product_id), params=params, timeout=30)
90-
# r.raise_for_status()
91-
return r.json()
92+
93+
# Supported levels are 1, 2 or 3
94+
level = level if level in range(1, 4) else 1
95+
return self._get('/products/{}/book'.format(str(product_id)), params={'level': level})
9296

9397
def get_product_ticker(self, product_id):
9498
"""Snapshot about the last trade (tick), best bid/ask and 24h volume.
@@ -112,10 +116,7 @@ def get_product_ticker(self, product_id):
112116
}
113117
114118
"""
115-
r = requests.get(self.url + '/products/{}/ticker'
116-
.format(product_id), timeout=30)
117-
# r.raise_for_status()
118-
return r.json()
119+
return self._get('/products/{}/ticker'.format(str(product_id)))
119120

120121
def get_product_trades(self, product_id):
121122
"""List the latest trades for a product.
@@ -140,9 +141,7 @@ def get_product_trades(self, product_id):
140141
}]
141142
142143
"""
143-
r = requests.get(self.url + '/products/{}/trades'.format(product_id), timeout=30)
144-
# r.raise_for_status()
145-
return r.json()
144+
return self._get('/products/{}/trades'.format(str(product_id)))
146145

147146
def get_product_historic_rates(self, product_id, start=None, end=None,
148147
granularity=None):
@@ -188,10 +187,8 @@ def get_product_historic_rates(self, product_id, start=None, end=None,
188187
params['end'] = end
189188
if granularity is not None:
190189
params['granularity'] = granularity
191-
r = requests.get(self.url + '/products/{}/candles'
192-
.format(product_id), params=params, timeout=30)
193-
# r.raise_for_status()
194-
return r.json()
190+
191+
return self._get('/products/{}/candles'.format(str(product_id)), params=params)
195192

196193
def get_product_24hr_stats(self, product_id):
197194
"""Get 24 hr stats for the product.
@@ -210,9 +207,7 @@ def get_product_24hr_stats(self, product_id):
210207
}
211208
212209
"""
213-
r = requests.get(self.url + '/products/{}/stats'.format(product_id), timeout=30)
214-
# r.raise_for_status()
215-
return r.json()
210+
return self._get('/products/{}/stats'.format(str(product_id)))
216211

217212
def get_currencies(self):
218213
"""List known currencies.
@@ -230,9 +225,7 @@ def get_currencies(self):
230225
}]
231226
232227
"""
233-
r = requests.get(self.url + '/currencies', timeout=30)
234-
# r.raise_for_status()
235-
return r.json()
228+
return self._get('/currencies')
236229

237230
def get_time(self):
238231
"""Get the API server time.
@@ -246,6 +239,4 @@ def get_time(self):
246239
}
247240
248241
"""
249-
r = requests.get(self.url + '/time', timeout=30)
250-
# r.raise_for_status()
251-
return r.json()
242+
return self._get('/time')

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
addopts = --cov gdax/ --cov-report=term-missing
3+
testpaths = tests

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ bintrees==2.0.7
22
requests==2.13.0
33
six==1.10.0
44
websocket-client==0.40.0
5-
pymongo==3.5.1
5+
pymongo==3.5.1
6+
pytest>=3.3.0
7+
pytest-cov>=2.5.0

tests/test_public_client.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import gdax
3+
import time
34

45

56
@pytest.fixture(scope='module')
@@ -9,18 +10,31 @@ def client():
910

1011
@pytest.mark.usefixtures('client')
1112
class TestPublicClient(object):
13+
14+
@staticmethod
15+
def teardown_method():
16+
time.sleep(.25) # Avoid rate limit
17+
1218
def test_get_products(self, client):
1319
r = client.get_products()
1420
assert type(r) is list
1521

16-
def test_get_product_order_book(self, client):
17-
r = client.get_product_order_book('BTC-USD')
18-
assert type(r) is dict
19-
r = client.get_product_order_book('BTC-USD', level=2)
22+
@pytest.mark.parametrize('level', [1, 2, 3, None])
23+
def test_get_product_order_book(self, client, level):
24+
r = client.get_product_order_book('BTC-USD', level=level)
2025
assert type(r) is dict
2126
assert 'asks' in r
2227
assert 'bids' in r
2328

29+
if level in (1, None) and (len(r['asks']) > 1 or len(r['bids']) > 1):
30+
pytest.fail('Fail: Level 1 should only return the best ask and bid')
31+
32+
if level is 2 and (len(r['asks']) > 50 or len(r['bids']) > 50):
33+
pytest.fail('Fail: Level 2 should only return the top 50 asks and bids')
34+
35+
if level is 2 and (len(r['asks']) < 50 or len(r['bids']) < 50):
36+
pytest.fail('Fail: Level 3 should return the full order book')
37+
2438
def test_get_product_ticker(self, client):
2539
r = client.get_product_ticker('BTC-USD')
2640
assert type(r) is dict
@@ -32,8 +46,11 @@ def test_get_product_trades(self, client):
3246
assert type(r) is list
3347
assert 'trade_id' in r[0]
3448

35-
def test_get_historic_rates(self, client):
36-
r = client.get_product_historic_rates('BTC-USD')
49+
@pytest.mark.parametrize('start', ('2017-11-01', None))
50+
@pytest.mark.parametrize('end', ('2017-11-30', None))
51+
@pytest.mark.parametrize('granularity', (3600, None))
52+
def test_get_historic_rates(self, client, start, end, granularity):
53+
r = client.get_product_historic_rates('BTC-USD', start=start, end=end, granularity=granularity)
3754
assert type(r) is list
3855

3956
def test_get_product_24hr_stats(self, client):

0 commit comments

Comments
 (0)