2017-07-19 67 views
0

我爲我使用的API創建了一個包裝器。完整的包裝是在這裏:https://pastebin.com/nL089zwFPython KeyError | API響應

我使用下面的代碼運行一個電話:

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' ] 
+0

似乎api需要密鑰和祕密,所以我們可能無法重現錯誤。但是你可以確認什麼類型的響應是 - 「打印類型(響應)」顯示的是什麼?取決於您使用的響應的定義。 'response = r.text'應該顯示'',而'response = json.loads(r.text)',那麼'type(response)'應該顯示'' – davedwards

+0

它確實需要它們,我輸入它們一個名爲secrets.json的文件。它從該文件中提取密鑰和祕密。我之前使用了response = r.text,它給了我錯誤TypeError:字符串索引必須是整數,所以我將它切換到response = json.loads(r.text)並獲取KeyError ...對不起,如果這不明確。任何想法? @downshift – cappuccino

+0

'json.loads(r)'只有在'r'是字典時纔有效,'','r.text'應該是'type(r.text)' - > ''。您希望方法返回哪些內容或「內容類型」?答覆的文字?一個布爾值,如'True' /'False'? – davedwards

回答

0

末編輯: 錯誤解決了,天漸漸錯鍵(「數據」,而不是「數據」)

無論如何,正確的版本在這裏(使用的一個)。

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 
+0

完美謝謝! – cappuccino