我正在使用来自 GitHub 的 Python 库调用 Blockchain.INFO 的 API:https://github.com/gowness/pyblockchain
我的代码如下所示,当我运行代码时没有返回值。我对类(class)等知识不是很了解,但我已简要阅读,这里出了什么问题?
import requests
import urllib
import json
from os.path import expanduser
import configparser
class Wallet:
guid = 'g'
isAccount = 0
isKey = 0
password1 = 'x'
password2 = 'y'
url = ''
def __init__(self, guid = 'g', password1 = 'x', password2 = 'y'):
if guid.count('-') > 0:
self.isAccount = 1
if password1 == '': # wallet guid's contain -
raise ValueError('No password with guid.')
else:
self.isKey = 1
self.guid = guid
self.url = 'https://blockchain.info/merchant/' + guid + '/'
self.password1 = password1
self.password2 = password2
def Call(self, method, data = {}):
if self.password1 != '':
data['password'] = self.password1
if self.password2 != '':
data['second_password'] = self.password2
response = requests.post(self.url + method,params=data)
json = response.json()
if 'error' in json:
raise RuntimeError('ERROR: ' + json['error'])
return json
def GetBalance(self):
response = self.Call('balance')
return response['balance']
Wallet().GetBalance()
最佳答案
由于我无法对您的 URL 运行成功的 POST 调用,我稍微更改了您的代码,以便它不会在 json 中查找“错误”,以查看您的 GetBalance
方法中发生了什么。
现在,知道通过调用会失败,json 响应将是这样的:
{u'error': u'Unknown Key Format'}
因此,在您的 GetBalance
方法中,我只是在之后添加了一个打印语句
response = self.Call('balance')
我得到了预期的有效 json。
说了这么多。尝试这样做,看看会得到什么:
print(Wallet().GetBalance())
根据返回的响应,这可能表明您没有为您尝试创建的 POST 方法向服务器提供适当的负载?
关于python - 调用包含 API 调用的类和函数时没有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32936559/