Skip to content

Commit d4c4521

Browse files
kzielinskikzielinski
authored andcommitted
ISSUE-128: refactored to prevent duplicate code
1 parent 2e1189e commit d4c4521

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

gdax/authenticated_client.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import json
1313
from requests.auth import AuthBase
1414
from gdax.public_client import PublicClient
15+
from gdax.gdax_auth import GdaxAuth
1516

1617

1718
class AuthenticatedClient(PublicClient):
@@ -288,28 +289,4 @@ def get_report(self, report_id=""):
288289
def get_trailing_volume(self):
289290
r = requests.get(self.url + "/users/self/trailing-volume", auth=self.auth, timeout=30)
290291
# r.raise_for_status()
291-
return r.json()
292-
293-
294-
class GdaxAuth(AuthBase):
295-
# Provided by gdax: https://docs.gdax.com/#signing-a-message
296-
def __init__(self, api_key, secret_key, passphrase):
297-
self.api_key = api_key
298-
self.secret_key = secret_key
299-
self.passphrase = passphrase
300-
301-
def __call__(self, request):
302-
timestamp = str(time.time())
303-
message = timestamp + request.method + request.path_url + (request.body or '')
304-
message = message.encode('ascii')
305-
hmac_key = base64.b64decode(self.secret_key)
306-
signature = hmac.new(hmac_key, message, hashlib.sha256)
307-
signature_b64 = base64.b64encode(signature.digest())
308-
request.headers.update({
309-
'Content-Type': 'Application/JSON',
310-
'CB-ACCESS-SIGN': signature_b64,
311-
'CB-ACCESS-TIMESTAMP': timestamp,
312-
'CB-ACCESS-KEY': self.api_key,
313-
'CB-ACCESS-PASSPHRASE': self.passphrase
314-
})
315-
return request
292+
return r.json()

gdax/gdax_auth.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import hmac
2+
import hashlib
3+
import time
4+
import base64
5+
from requests.auth import AuthBase
6+
7+
8+
class GdaxAuth(AuthBase):
9+
# Provided by gdax: https://docs.gdax.com/#signing-a-message
10+
def __init__(self, api_key, secret_key, passphrase):
11+
self.api_key = api_key
12+
self.secret_key = secret_key
13+
self.passphrase = passphrase
14+
15+
def __call__(self, request):
16+
timestamp = str(time.time())
17+
message = timestamp + request.method + request.path_url + (request.body or '')
18+
request.headers.update(get_auth_headers(timestamp, message, self.api_key, self.secret_key,
19+
self.passphrase))
20+
return request
21+
22+
23+
def get_auth_headers(timestamp, message, api_key, secret_key, passphrase):
24+
message = message.encode('ascii')
25+
hmac_key = base64.b64decode(secret_key)
26+
signature = hmac.new(hmac_key, message, hashlib.sha256)
27+
signature_b64 = base64.b64encode(signature.digest()).decode('utf-8')
28+
return {
29+
'Content-Type': 'Application/JSON',
30+
'CB-ACCESS-SIGN': signature_b64,
31+
'CB-ACCESS-TIMESTAMP': timestamp,
32+
'CB-ACCESS-KEY': api_key,
33+
'CB-ACCESS-PASSPHRASE': passphrase
34+
}

gdax/websocket_client.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from threading import Thread
1515
from websocket import create_connection, WebSocketConnectionClosedException
1616
from pymongo import MongoClient
17+
from gdax.gdax_auth import get_auth_headers
1718

1819

1920
class WebsocketClient(object):
@@ -62,14 +63,7 @@ def _connect(self):
6263
if self.auth:
6364
timestamp = str(time.time())
6465
message = timestamp + 'GET' + '/users/self'
65-
message = message.encode('ascii')
66-
hmac_key = base64.b64decode(self.api_secret)
67-
signature = hmac.new(hmac_key, message, hashlib.sha256)
68-
signature_b64 = base64.b64encode(signature.digest()).decode("utf-8")
69-
sub_params['CB-ACCESS-SIGN'] = signature_b64
70-
sub_params['CB-ACCESS-KEY'] = self.api_key
71-
sub_params['CB-ACCESS-PASSPHRASE'] = self.api_passphrase
72-
sub_params['CB-ACCESS-TIMESTAMP'] = timestamp
66+
sub_params.update(get_auth_headers(timestamp, message, self.api_key, self.api_secret, self.api_passphrase))
7367

7468
self.ws = create_connection(self.url)
7569
self.ws.send(json.dumps(sub_params))

0 commit comments

Comments
 (0)