Based on the Coinbase Pro API Documentation I got their authentication class working and have generally been able to make GET calls. However, I'm trying to write a second class that would authenticate and then make the API calls based on the URL changes (i.e., GET 24hr stats for a product ID).
When I run the code below, I'm receiving a TypeError missing the positional argument for product_id even though it's defined in the code. What would I have to change in the main code or for the CoinbaseManager to get the call to work correctly?
import json, hmac, hashlib, time, requests, base64, os
from requests.auth import AuthBase
# Create custom authentication for Exchange
class CoinbaseExchangeAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or b'').decode()
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message.encode(), hashlib.sha256)
signature_b64 = base64.b64encode(signature.digest()).decode()
request.headers.update({
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.passphrase,
'Content-Type': 'application/json'
})
return request
class CoinbaseManager:
_apiUrl = 'https://api.pro.coinbase.com/'
_auth = CoinbaseExchangeAuth(os.getenv('apiKey'), os.getenv('secretKey'), os.getenv('passphrase'))
def __init__(self):
self.data = []
def get_24hr_stats(self, auth, product_id):
'''
get_24hr_stats() -- Get 24 hr stats for the product. volume is in base currency units.
open, high, low are in quote currency units.
'''
extension = 'products/{}/stats'.format(product_id)
return requests.get(self._apiUrl + extension, auth=self._auth)
if __name__ == '__main__':
crypto_id = 'BTC-USD'
price = CoinbaseManager.get_24hr_stats(CoinbaseManager._auth, crypto_id)
print(price.json())
it should be
price = CoinbaseManager().get_24hr_stats(CoinbaseManager._auth, crypto_id)
you are failing to create a CoinbaseManager object.