仅使用 python-eve API 请求 `_items` 到 MongoDB
Request `_items` only using python-eve API to a MongoDB
我正在使用 python-eve-API (short called eve
) to access a MongoDB. After inserting some sample data I am using Chrome's Postman 来测试 API。
因为除了请求的数据和 HATEOAS directives as explained in the docs 之外,eve 还提供了额外的信息,我只是想知道如何只请求 _items
-字典(如果用 dictionary
=31=] 和 object
如果用 JSON 或 JavaScript).
所以示例请求 http://127.0.0.1:5000/packagings/
给出了如下所示的响应,我只对 _items
-dictionary/-object.
中包含的数据感兴趣
当然,我可以在接收并存储完整响应后提取所需的数据。
但是,有没有办法只请求我感兴趣的数据,以减少额外的数据提取。收到数据后的数据处理?
{
"_links": {
"self": {
"href": "packagings",
"title": "packagings"
},
"parent": {
"href": "/",
"title": "home"
}
},
"_meta": {
"max_results": 25,
"page": 1,
"total": 1
},
"_items": [
{
"diameter_dk": 0.0144,
"_created": "Tue, 17 Nov 2015 21:15:37 GMT",
"factor_fa": 2.1,
"_id": "564b98f955c40f29843128df",
"free_volume": 0.89,
"title": "raschigring10x10x0.5",
"_updated": "Tue, 17 Nov 2015 21:15:37 GMT",
"_links": {
"self": {
"href": "packagings/564b98f955c40f29843128df",
"title": "Packaging"
}
},
"specific_weight": 920,
"title_hr": "Raschig-Ring 10x10x0.5",
"specific_surface": 500,
"specific_number": 770000,
"_etag": "bcb4080b61028405babcd960196d27208c3eabd3"
}
]
}
您可以通过在配置设置中设置 HATEOAS = False
来禁用 HATEOAS。这应该会大大减少有效负载,使其更适合您的用例。
编辑:您还可以选择通过将回调函数挂接到 on_fetched_resource
事件来转换响应负载。
from eve import Eve
def on_fetched_resource(resource, response):
del(response['_links'])
del(response['_meta'])
# would result in an empty JSON document
# del(response['_items'])
app = Eve()
app.on_fetched_resource += on_fetched_resource
if __name__ == '__main__':
app.run()
由于响应是一个字典(毕竟是 JSON),您仍然需要为文档数组提供一个键。
我正在使用 python-eve-API (short called eve
) to access a MongoDB. After inserting some sample data I am using Chrome's Postman 来测试 API。
因为除了请求的数据和 HATEOAS directives as explained in the docs 之外,eve 还提供了额外的信息,我只是想知道如何只请求 _items
-字典(如果用 dictionary
=31=] 和 object
如果用 JSON 或 JavaScript).
所以示例请求 http://127.0.0.1:5000/packagings/
给出了如下所示的响应,我只对 _items
-dictionary/-object.
当然,我可以在接收并存储完整响应后提取所需的数据。 但是,有没有办法只请求我感兴趣的数据,以减少额外的数据提取。收到数据后的数据处理?
{
"_links": {
"self": {
"href": "packagings",
"title": "packagings"
},
"parent": {
"href": "/",
"title": "home"
}
},
"_meta": {
"max_results": 25,
"page": 1,
"total": 1
},
"_items": [
{
"diameter_dk": 0.0144,
"_created": "Tue, 17 Nov 2015 21:15:37 GMT",
"factor_fa": 2.1,
"_id": "564b98f955c40f29843128df",
"free_volume": 0.89,
"title": "raschigring10x10x0.5",
"_updated": "Tue, 17 Nov 2015 21:15:37 GMT",
"_links": {
"self": {
"href": "packagings/564b98f955c40f29843128df",
"title": "Packaging"
}
},
"specific_weight": 920,
"title_hr": "Raschig-Ring 10x10x0.5",
"specific_surface": 500,
"specific_number": 770000,
"_etag": "bcb4080b61028405babcd960196d27208c3eabd3"
}
]
}
您可以通过在配置设置中设置 HATEOAS = False
来禁用 HATEOAS。这应该会大大减少有效负载,使其更适合您的用例。
编辑:您还可以选择通过将回调函数挂接到 on_fetched_resource
事件来转换响应负载。
from eve import Eve
def on_fetched_resource(resource, response):
del(response['_links'])
del(response['_meta'])
# would result in an empty JSON document
# del(response['_items'])
app = Eve()
app.on_fetched_resource += on_fetched_resource
if __name__ == '__main__':
app.run()
由于响应是一个字典(毕竟是 JSON),您仍然需要为文档数组提供一个键。