在python-eve framework中,在post GET hook中找到GET请求无法找到任何资源的最简单方法是什么(例如,给定的filtering parameters无法匹配任何资源资源)?
谢谢!
答案 0 :(得分:2)
由于payload
是Flask Response
对象,因此您可以利用其功能。一种选择是简单地调查_items
密钥,该密钥在集合端点上返回实际文档:
import json
def on_post_get(resoure, request, payload):
# get the actual response json out of Flask Response
json = json.loads(payload.get_data())
documents = json['_items']
assert(len(documents) == 0)
app = Eve()
app.on_post_GET += on_post_get
if __name__ == '__main__':
app.run()
答案 1 :(得分:0)
您应该能够在钩子中读取响应头X-Total-Count。如果其值为0,则无法找到任何资源。
要访问此值,调用将类似于:
# Within run.py
app.on_fetched_resource_something += before_returning_something_items
# Within your hook module file
def before_returning_something_items(response):
numRecs = int(response.headers.get("X-Total-Count"))
if numRecs > 0:
print "Found Something"
else:
print "Found Zilcho!"