Python 键错误 |接口响应



我为我正在使用的API创建了一个包装器。完整的包装器在这里:https://pastebin.com/nL089zwF

我正在使用以下代码运行调用:

balanceBTC = api.GetBalance('BTC')
for i in balanceBTC['data']:
if i['symbol'] == 'btc':
print i['available']

响应如下:

AttributeError: 'dict' object has no attribute 'respond'

跟踪将代码的第 50 行标识为问题:

return response.respond["false","False"].respond["true","True"].respond['":null','":None' ]

所以我删除了.respond,并留下了一个新错误:

return response["false","False"], ["true","True"], ['":null','":None' ]
KeyError: ('false', 'False')

我知道响应是一个字典,它没有键,这就是导致错误的原因(我认为?(,但我是一个初学者,边走边学,我不知道要更改/添加/删除代码本身。

我将不胜感激该怎么做的指导!谢谢。

下面是负责获取响应的 API 包装器的完整部分:

def api_query(self, method, values, req = None ):
if not req:
req = {}
#print "def api_query( method = " + method + ", req = " + str( req ) + " ):"
time.sleep(1)
if method in self.public:
url = 'https://www.cryptopia.co.nz/api/'
elif method in self.private:
url = 'https://www.cryptopia.co.nz/api/'
else:
return 'Call Not Identified - Something Went Wrong.'
url += method + '?' + urllib.urlencode(values)
if method not in self.public:
url = "https://www.cryptopia.co.nz/Api/" + method
nonce = str( int( time.time() ) )
post_data = json.dumps( req );
m = hashlib.md5()
m.update(post_data)
requestContentBase64String = base64.b64encode(m.digest())
signature = self.key + "POST" + urllib.quote_plus( url ).lower() + nonce + requestContentBase64String
hmacsignature = base64.b64encode(hmac.new(base64.b64decode( self.secret ), signature, hashlib.sha256).digest())
header_value = "amx " + self.key + ":" + hmacsignature + ":" + nonce
headers = { 'Authorization': header_value, 'Content-Type':'application/json; charset=utf-8' }
r = requests.post( url, data = post_data, headers = headers )
response = json.loads(r.text)
return response.respond["false","False"].respond["true","True"].respond['":null','":None' ]

后期编辑: 错误解决了,它得到了错误的键("数据",而不是"数据"(

无论如何,正确的版本都在这里(使用的版本(。

def api_query(self, method, values, req = None ):
if not req:
req = {}
time.sleep(1)
if method in self.public:
url = 'https://www.cryptopia.co.nz/api/'
elif method in self.private:
url = 'https://www.cryptopia.co.nz/api/'
else:
return 'Call Not Identified - Something Went Wrong.'
url += method + '?' + urllib.urlencode(values)
if method not in self.public:
url = "https://www.cryptopia.co.nz/Api/" + method
nonce = str( int( time.time() ) )
post_data = json.dumps( req );
m = hashlib.md5()
m.update(post_data)
requestContentBase64String = base64.b64encode(m.digest())
signature = self.key + "POST" + urllib.quote_plus( url ).lower() + nonce + requestContentBase64String
hmacsignature = base64.b64encode(hmac.new(base64.b64decode( self.secret ), signature, hashlib.sha256).digest())
header_value = "amx " + self.key + ":" + hmacsignature + ":" + nonce
headers = { 'Authorization': header_value, 'Content-Type':'application/json; charset=utf-8' }
r = requests.post( url, data = post_data, headers = headers )
return r.json() #changed here

相关内容

最新更新