对于Python,整个API和Stack Overflow我还是很陌生。 我想连接到Bricklink休息API和一个图书馆,我在GitHub上找到。 现在的问题是,该库有一个类a,该类在另一个类中获取实例,如果我运行代码,则类c要通过类b使用类a的get函数(我真的不知道是否对你们来说这很有意义,对不起!) 我将提供一些代码以更好地理解。 (以下是指向Bricklink库的链接:https://github.com/robbietjuh/py-bricklink)
我试图将self.client放在方括号中,希望它得到更高的优先级,但是没有用。 我搜索了类似的内容,但总是很难理解或无济于事。
class Method: # this is class b
client = None
def __init__(self, client):
self.client = client
class Orders(Method): # this is class c
URL_ORDER_LIST = 'orders'
URL_ORDER_DETAILS = 'orders/{order_id}'
URL_ORDER_ITEMS = 'orders/{order_id}/items'
URL_ORDER_MESSAGES = 'orders/{order_id}/messages'
URL_ORDER_FEEDBACK = 'orders/{order_id}/feedback'
URL_ORDER_UPDATE = 'orders/{order_id}'
URL_ORDER_UPDATE_STATUS = 'orders/{order_id}/status'
URL_ORDER_UPDATE_PAYMENT_STATUS = 'orders/{order_id}/status'
def getOrders(self, direction='in', status='', filed=False):
params = {
'direction': direction,
'status': status,
'filed': filed,
}
return self.client.get(self.URL_ORDER_LIST, params)
class ApiClient: # this is class a
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
self.service = OAuth1Service(name='bricklink',
consumer_key=consumer_key,
consumer_secret=consumer_secret, base_url='https://api.bricklink.com/api/store/v1/')
self.session = self.service.get_session((access_token, access_token_secret))
self.orders = Orders(self)
def request(self, method, url, params):
if method in ('POST', 'PUT', 'DELETE'):
response = self.session.request(method, url, True, '', data=json.dumps(params), headers={'Content-Type': 'application/json'}).json()
else:
response = self.session.request(method, url, True, '', params=params).json()
return self.processResponse(response, method, url, params)
def get(self, url, params={}): # here is the get function, that is needed to be accessed
return self.request('GET', url, params)
test = ApiClient('consumer_key', 'consumer_secret', 'access_token', 'access_token_secret')
print(test)
meth = a.Method(test)
order = a.Orders(meth)
order.getOrders(direction='in', status='', filed=False)
错误:
Traceback (most recent call last):
File "D:/Python Projects/Neuer Ordner/py-bricklink-master/bricklink/api.py", line 96, in <module>
order.getOrders(direction='in', status='', filed=False)
File "D:\Python Projects\Neuer Ordner\py-bricklink-master\bricklink\a.py", line 36, in getOrders
return self.client.get(self.URL_ORDER_LIST, params)
AttributeError: 'Method' object has no attribute 'get'
因此它应该给我一个包含order_ids的列表,但如果我运行它,我只会得到响应“'Method'对象没有属性'get'”,并且在各行中都有错误调用函数“ getOrders(...)”并在“返回self.client.get(self.URL_ORDER_LIST,params)”中调用,函数getOrders(...)返回值。