[英]Querying my Eve API from my Flask code
In Flask, I have an Eve API running under endpoints like /api/v1/Widgets在 Flask 中,我在 /api/v1/Widgets 等端点下运行了一个 Eve API
I can query that from eg Javascript, but I don't know how to properly query that API from elsewhere within my Flask app.我可以从例如 Javascript 查询,但我不知道如何从我的 Flask 应用程序中的其他地方正确查询该 API。
Just now for example, if I need to search Widgets in one of my routes, then I'm loading the requests
module and querying the API using a separate http request, and dealing with the JSON returned.刚才例如,如果我需要在我的一个路由中搜索 Widgets,那么我正在加载
requests
模块并使用单独的 http 请求查询 API,并处理返回的 JSON。
@app.route('/hello')
def show_hello():
resp = requests.get('http://example.com/api/v1/Widgets')
return jsonify({'results': resp.json()})
That's surely very inefficient and it seems like there must be something like那肯定是非常低效的,似乎必须有类似的东西
my_endpoint = app.Eve.endpoint('Widgets') # not real
return jsonify({'results': my_endpoint.search()}) # not real
But I don't know what that is.但我不知道那是什么。 Could someone help me understand if/how I can make queries directly into my Eve endpoints from with my normal Flask app routes?
有人可以帮助我了解是否/如何使用我的普通 Flask 应用程序路由直接向我的 Eve 端点进行查询吗?
You can use app.test_client.get()
which is however rate limited, authenticated and raises pre-request events.您可以使用
app.test_client.get()
,但它会受到速率限制、经过身份验证并引发预请求事件。
With v0.7 (currently on the develop
branch ) you can use get_internal
.使用 v0.7(目前在
develop
分支上),您可以使用get_internal
。 This method is not rate limited, authentication is not checked and pre-request events are not raised.此方法不受速率限制,不检查身份验证并且不引发预请求事件。 Usage example:
用法示例:
from eve.methods.get import get_internal
payload = get_internal(endpoint)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.