@@ -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' )
0 commit comments