Python Coinbase Pro API Class 函数参数不工作

Python Coinbase Pro API Class Function Parameters Not Working

基于 Coinbase Pro API 文档,我得到了他们的身份验证 class 工作并且通常能够进行 GET 调用。但是,我正在尝试编写第二个 class 来进行身份验证,然后根据 URL 更改进行 API 调用(即获取产品 ID 的 24 小时统计信息)。

当我 运行 下面的代码时,我收到一个 TypeError 缺少 product_id 的位置参数,即使它是在代码中定义的。我必须在主要代码中或 CoinbaseManager 中更改什么才能使调用正常工作?

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())

应该是

    price = CoinbaseManager().get_24hr_stats(CoinbaseManager._auth, crypto_id)

您未能创建 CoinbaseManager 对象。