Skip to content

Commit 8765d28

Browse files
authored
Merge pull request #169 from damianmoore/custom-timeouts
Allow setting a timeout value when initializing the clients to override the default of 30 seconds
2 parents 55e8e4a + 3f1444d commit 8765d28

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

gdax/authenticated_client.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717

1818
class AuthenticatedClient(PublicClient):
19-
def __init__(self, key, b64secret, passphrase, api_url="https://api.gdax.com"):
19+
def __init__(self, key, b64secret, passphrase, api_url="https://api.gdax.com", timeout=30):
2020
super(AuthenticatedClient, self).__init__(api_url)
2121
self.auth = GdaxAuth(key, b64secret, passphrase)
22+
self.timeout = timeout
2223

2324
def get_account(self, account_id):
24-
r = requests.get(self.url + '/accounts/' + account_id, auth=self.auth, timeout=30)
25+
r = requests.get(self.url + '/accounts/' + account_id, auth=self.auth, timeout=self.timeout)
2526
# r.raise_for_status()
2627
return r.json()
2728

@@ -30,15 +31,15 @@ def get_accounts(self):
3031

3132
def get_account_history(self, account_id):
3233
result = []
33-
r = requests.get(self.url + '/accounts/{}/ledger'.format(account_id), auth=self.auth, timeout=30)
34+
r = requests.get(self.url + '/accounts/{}/ledger'.format(account_id), auth=self.auth, timeout=self.timeout)
3435
# r.raise_for_status()
3536
result.append(r.json())
3637
if "cb-after" in r.headers:
3738
self.history_pagination(account_id, result, r.headers["cb-after"])
3839
return result
3940

4041
def history_pagination(self, account_id, result, after):
41-
r = requests.get(self.url + '/accounts/{}/ledger?after={}'.format(account_id, str(after)), auth=self.auth, timeout=30)
42+
r = requests.get(self.url + '/accounts/{}/ledger?after={}'.format(account_id, str(after)), auth=self.auth, timeout=self.timeout)
4243
# r.raise_for_status()
4344
if r.json():
4445
result.append(r.json())
@@ -48,15 +49,15 @@ def history_pagination(self, account_id, result, after):
4849

4950
def get_account_holds(self, account_id):
5051
result = []
51-
r = requests.get(self.url + '/accounts/{}/holds'.format(account_id), auth=self.auth, timeout=30)
52+
r = requests.get(self.url + '/accounts/{}/holds'.format(account_id), auth=self.auth, timeout=self.timeout)
5253
# r.raise_for_status()
5354
result.append(r.json())
5455
if "cb-after" in r.headers:
5556
self.holds_pagination(account_id, result, r.headers["cb-after"])
5657
return result
5758

5859
def holds_pagination(self, account_id, result, after):
59-
r = requests.get(self.url + '/accounts/{}/holds?after={}'.format(account_id, str(after)), auth=self.auth, timeout=30)
60+
r = requests.get(self.url + '/accounts/{}/holds?after={}'.format(account_id, str(after)), auth=self.auth, timeout=self.timeout)
6061
# r.raise_for_status()
6162
if r.json():
6263
result.append(r.json())
@@ -71,32 +72,32 @@ def buy(self, **kwargs):
7172
r = requests.post(self.url + '/orders',
7273
data=json.dumps(kwargs),
7374
auth=self.auth,
74-
timeout=30)
75+
timeout=self.timeout)
7576
return r.json()
7677

7778
def sell(self, **kwargs):
7879
kwargs["side"] = "sell"
7980
r = requests.post(self.url + '/orders',
8081
data=json.dumps(kwargs),
8182
auth=self.auth,
82-
timeout=30)
83+
timeout=self.timeout)
8384
return r.json()
8485

8586
def cancel_order(self, order_id):
86-
r = requests.delete(self.url + '/orders/' + order_id, auth=self.auth, timeout=30)
87+
r = requests.delete(self.url + '/orders/' + order_id, auth=self.auth, timeout=self.timeout)
8788
# r.raise_for_status()
8889
return r.json()
8990

9091
def cancel_all(self, product_id=''):
9192
url = self.url + '/orders/'
9293
if product_id:
9394
url += "?product_id={}&".format(str(product_id))
94-
r = requests.delete(url, auth=self.auth, timeout=30)
95+
r = requests.delete(url, auth=self.auth, timeout=self.timeout)
9596
# r.raise_for_status()
9697
return r.json()
9798

9899
def get_order(self, order_id):
99-
r = requests.get(self.url + '/orders/' + order_id, auth=self.auth, timeout=30)
100+
r = requests.get(self.url + '/orders/' + order_id, auth=self.auth, timeout=self.timeout)
100101
# r.raise_for_status()
101102
return r.json()
102103

@@ -108,7 +109,7 @@ def get_orders(self, product_id='', status=[]):
108109
params["product_id"] = product_id
109110
if status:
110111
params["status"] = status
111-
r = requests.get(url, auth=self.auth, params=params, timeout=30)
112+
r = requests.get(url, auth=self.auth, params=params, timeout=self.timeout)
112113
# r.raise_for_status()
113114
result.append(r.json())
114115
if 'cb-after' in r.headers:
@@ -125,7 +126,7 @@ def paginate_orders(self, product_id, status, result, after):
125126
params["product_id"] = product_id
126127
if status:
127128
params["status"] = status
128-
r = requests.get(url, auth=self.auth, params=params, timeout=30)
129+
r = requests.get(url, auth=self.auth, params=params, timeout=self.timeout)
129130
# r.raise_for_status()
130131
if r.json():
131132
result.append(r.json())
@@ -146,7 +147,7 @@ def get_fills(self, order_id='', product_id='', before='', after='', limit=''):
146147
url += "after={}&".format(str(after))
147148
if limit:
148149
url += "limit={}&".format(str(limit))
149-
r = requests.get(url, auth=self.auth, timeout=30)
150+
r = requests.get(url, auth=self.auth, timeout=self.timeout)
150151
# r.raise_for_status()
151152
result.append(r.json())
152153
if 'cb-after' in r.headers and limit is not len(r.json()):
@@ -159,7 +160,7 @@ def paginate_fills(self, result, after, order_id='', product_id=''):
159160
url += "order_id={}&".format(str(order_id))
160161
if product_id:
161162
url += "product_id={}&".format(product_id)
162-
r = requests.get(url, auth=self.auth, timeout=30)
163+
r = requests.get(url, auth=self.auth, timeout=self.timeout)
163164
# r.raise_for_status()
164165
if r.json():
165166
result.append(r.json())
@@ -175,7 +176,7 @@ def get_fundings(self, result='', status='', after=''):
175176
url += "status={}&".format(str(status))
176177
if after:
177178
url += 'after={}&'.format(str(after))
178-
r = requests.get(url, auth=self.auth, timeout=30)
179+
r = requests.get(url, auth=self.auth, timeout=self.timeout)
179180
# r.raise_for_status()
180181
result.append(r.json())
181182
if 'cb-after' in r.headers:
@@ -187,7 +188,7 @@ def repay_funding(self, amount='', currency=''):
187188
"amount": amount,
188189
"currency": currency # example: USD
189190
}
190-
r = requests.post(self.url + "/funding/repay", data=json.dumps(payload), auth=self.auth, timeout=30)
191+
r = requests.post(self.url + "/funding/repay", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
191192
# r.raise_for_status()
192193
return r.json()
193194

@@ -198,20 +199,20 @@ def margin_transfer(self, margin_profile_id="", transfer_type="", currency="", a
198199
"currency": currency, # example: USD
199200
"amount": amount
200201
}
201-
r = requests.post(self.url + "/profiles/margin-transfer", data=json.dumps(payload), auth=self.auth, timeout=30)
202+
r = requests.post(self.url + "/profiles/margin-transfer", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
202203
# r.raise_for_status()
203204
return r.json()
204205

205206
def get_position(self):
206-
r = requests.get(self.url + "/position", auth=self.auth, timeout=30)
207+
r = requests.get(self.url + "/position", auth=self.auth, timeout=self.timeout)
207208
# r.raise_for_status()
208209
return r.json()
209210

210211
def close_position(self, repay_only=""):
211212
payload = {
212213
"repay_only": repay_only or False
213214
}
214-
r = requests.post(self.url + "/position/close", data=json.dumps(payload), auth=self.auth, timeout=30)
215+
r = requests.post(self.url + "/position/close", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
215216
# r.raise_for_status()
216217
return r.json()
217218

@@ -221,7 +222,7 @@ def deposit(self, amount="", currency="", payment_method_id=""):
221222
"currency": currency,
222223
"payment_method_id": payment_method_id
223224
}
224-
r = requests.post(self.url + "/deposits/payment-method", data=json.dumps(payload), auth=self.auth, timeout=30)
225+
r = requests.post(self.url + "/deposits/payment-method", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
225226
# r.raise_for_status()
226227
return r.json()
227228

@@ -231,7 +232,7 @@ def coinbase_deposit(self, amount="", currency="", coinbase_account_id=""):
231232
"currency": currency,
232233
"coinbase_account_id": coinbase_account_id
233234
}
234-
r = requests.post(self.url + "/deposits/coinbase-account", data=json.dumps(payload), auth=self.auth, timeout=30)
235+
r = requests.post(self.url + "/deposits/coinbase-account", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
235236
# r.raise_for_status()
236237
return r.json()
237238

@@ -241,7 +242,7 @@ def withdraw(self, amount="", currency="", payment_method_id=""):
241242
"currency": currency,
242243
"payment_method_id": payment_method_id
243244
}
244-
r = requests.post(self.url + "/withdrawals/payment-method", data=json.dumps(payload), auth=self.auth, timeout=30)
245+
r = requests.post(self.url + "/withdrawals/payment-method", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
245246
# r.raise_for_status()
246247
return r.json()
247248

@@ -251,7 +252,7 @@ def coinbase_withdraw(self, amount="", currency="", coinbase_account_id=""):
251252
"currency": currency,
252253
"coinbase_account_id": coinbase_account_id
253254
}
254-
r = requests.post(self.url + "/withdrawals/coinbase", data=json.dumps(payload), auth=self.auth, timeout=30)
255+
r = requests.post(self.url + "/withdrawals/coinbase", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
255256
# r.raise_for_status()
256257
return r.json()
257258

@@ -261,17 +262,17 @@ def crypto_withdraw(self, amount="", currency="", crypto_address=""):
261262
"currency": currency,
262263
"crypto_address": crypto_address
263264
}
264-
r = requests.post(self.url + "/withdrawals/crypto", data=json.dumps(payload), auth=self.auth, timeout=30)
265+
r = requests.post(self.url + "/withdrawals/crypto", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
265266
# r.raise_for_status()
266267
return r.json()
267268

268269
def get_payment_methods(self):
269-
r = requests.get(self.url + "/payment-methods", auth=self.auth, timeout=30)
270+
r = requests.get(self.url + "/payment-methods", auth=self.auth, timeout=self.timeout)
270271
# r.raise_for_status()
271272
return r.json()
272273

273274
def get_coinbase_accounts(self):
274-
r = requests.get(self.url + "/coinbase-accounts", auth=self.auth, timeout=30)
275+
r = requests.get(self.url + "/coinbase-accounts", auth=self.auth, timeout=self.timeout)
275276
# r.raise_for_status()
276277
return r.json()
277278

@@ -286,16 +287,16 @@ def create_report(self, report_type="", start_date="", end_date="", product_id="
286287
"format": report_format,
287288
"email": email
288289
}
289-
r = requests.post(self.url + "/reports", data=json.dumps(payload), auth=self.auth, timeout=30)
290+
r = requests.post(self.url + "/reports", data=json.dumps(payload), auth=self.auth, timeout=self.timeout)
290291
# r.raise_for_status()
291292
return r.json()
292293

293294
def get_report(self, report_id=""):
294-
r = requests.get(self.url + "/reports/" + report_id, auth=self.auth, timeout=30)
295+
r = requests.get(self.url + "/reports/" + report_id, auth=self.auth, timeout=self.timeout)
295296
# r.raise_for_status()
296297
return r.json()
297298

298299
def get_trailing_volume(self):
299-
r = requests.get(self.url + "/users/self/trailing-volume", auth=self.auth, timeout=30)
300+
r = requests.get(self.url + "/users/self/trailing-volume", auth=self.auth, timeout=self.timeout)
300301
# r.raise_for_status()
301302
return r.json()

gdax/public_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ class PublicClient(object):
1818
1919
"""
2020

21-
def __init__(self, api_url='https://api.gdax.com'):
21+
def __init__(self, api_url='https://api.gdax.com', timeout=30):
2222
"""Create GDAX API public client.
2323
2424
Args:
2525
api_url (Optional[str]): API URL. Defaults to GDAX API.
2626
2727
"""
2828
self.url = api_url.rstrip('/')
29+
self.timeout = timeout
2930

3031
def _get(self, path, params=None):
3132
"""Perform get request"""
3233

33-
r = requests.get(self.url + path, params=params, timeout=30)
34+
r = requests.get(self.url + path, params=params, timeout=self.timeout)
3435
# r.raise_for_status()
3536
return r.json()
3637

0 commit comments

Comments
 (0)