forked from chrisacheson/liquidbot
/
APIKeyAuthWithExpires.py
28 lines (21 loc) · 1.07 KB
/
APIKeyAuthWithExpires.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from requests.auth import AuthBase
import time
from market_maker.auth.APIKeyAuth import generate_signature
class APIKeyAuthWithExpires(AuthBase):
"""Attaches API Key Authentication to the given Request object. This implementation uses `expires`."""
def __init__(self, apiKey, apiSecret):
"""Init with Key & Secret."""
self.apiKey = apiKey
self.apiSecret = apiSecret
def __call__(self, r):
"""
Called when forming a request - generates api key headers. This call uses `expires` instead of nonce.
This way it will not collide with other processes using the same API Key if requests arrive out of order.
For more details, see https://www.bitmex.com/app/apiKeys
"""
# modify and return the request
expires = int(round(time.time()) + 5) # 5s grace period in case of clock skew
r.headers['api-expires'] = str(expires)
r.headers['api-key'] = self.apiKey
r.headers['api-signature'] = generate_signature(self.apiSecret, r.method, r.url, expires, r.body or '')
return r