从我的 Flask 代码中查询我的 Eve API
Querying my Eve API from my Flask code
在 Flask 中,我在像 /api/v1/Widgets
这样的端点下有一个 Eve API 运行
我可以从例如Javascript,但我不知道如何正确 从我的 Flask 应用程序中的其他地方查询 API。
刚才举个例子,如果我需要在我的一个路由中搜索小部件,那么我正在加载 requests
模块并使用单独的 http 请求查询 API,然后处理返回 JSON。
@app.route('/hello')
def show_hello():
resp = requests.get('http://example.com/api/v1/Widgets')
return jsonify({'results': resp.json()})
这肯定是非常低效的,似乎必须有类似
的东西
my_endpoint = app.Eve.endpoint('Widgets') # not real
return jsonify({'results': my_endpoint.search()}) # not real
但我不知道那是什么。有人可以帮助我理解 if/how 我可以使用我的普通 Flask 应用程序路由直接查询我的 Eve 端点吗?
您可以使用 app.test_client.get()
,但它有速率限制、经过身份验证并引发预请求事件。
使用 v0.7(目前在 develop
branch) you can use get_internal
。此方法不受速率限制,不检查身份验证并且不引发预请求事件。使用示例:
from eve.methods.get import get_internal
payload = get_internal(endpoint)
在 Flask 中,我在像 /api/v1/Widgets
这样的端点下有一个 Eve API 运行我可以从例如Javascript,但我不知道如何正确 从我的 Flask 应用程序中的其他地方查询 API。
刚才举个例子,如果我需要在我的一个路由中搜索小部件,那么我正在加载 requests
模块并使用单独的 http 请求查询 API,然后处理返回 JSON。
@app.route('/hello')
def show_hello():
resp = requests.get('http://example.com/api/v1/Widgets')
return jsonify({'results': resp.json()})
这肯定是非常低效的,似乎必须有类似
的东西my_endpoint = app.Eve.endpoint('Widgets') # not real
return jsonify({'results': my_endpoint.search()}) # not real
但我不知道那是什么。有人可以帮助我理解 if/how 我可以使用我的普通 Flask 应用程序路由直接查询我的 Eve 端点吗?
您可以使用 app.test_client.get()
,但它有速率限制、经过身份验证并引发预请求事件。
使用 v0.7(目前在 develop
branch) you can use get_internal
。此方法不受速率限制,不检查身份验证并且不引发预请求事件。使用示例:
from eve.methods.get import get_internal
payload = get_internal(endpoint)