为什么 Flask-RESTful' add_resource() 有一个 URL 参数和另一个端点参数?
Why does Flask-RESTful' add_resource() has a parameter for URLs and another one for endpoints?
我正在使用 Flask 和 Flask-RESTful 扩展开发 API。我正在创建我的端点和资源,但我对资源“url”和它的“端点”之间的区别感到困惑。据我了解,URL 本身 是 端点。我也在阅读 ,他们还建议 URL 是一个端点。
然而 the Flask-RESTful docs 提到 add_resource
方法有一个 URLs 的参数并且它可以 也 接收端点名称。他们甚至提供了这个例子:
api.add_resource(HelloWorld, '/', '/hello')
api.add_resource(Foo, '/foo', endpoint="foo")
api.add_resource(FooSpecial, '/special/foo', endpoint="foo")
我明白他们在做什么,但我还是不明白端点名称的作用。
URL 和端点名称有什么区别?为什么要设置端点名称?
URL
one or more url routes to match for the resource, standard flask
routing rules apply. Any url variables will be passed to the resource
method as args.
端点
endpoint name (defaults to Resource.name.lower() Can be used to
reference this route in fields.Url fields
当你打算用相同的class管理两个或多个url时,必须指定endpoint参数,实际上,考虑到以下代码片段:
from flask import Flask, request
from flask_restful import Api, Resource
class my_handler(Resource):
def get(self):
return request.base_url
app = Flask(__name__)
api = Api(app)
api.add_resource(my_handler, '/')
api.add_resource(my_handler, '/hello')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)
flask 将无法执行应用程序,引发异常:
Exception has occurred: AssertionError
View function mapping is overwriting an existing endpoint function: my_handler
为了让 flask 处理具有相同 class 的多个 url,您需要指定端点参数:
from flask import Flask, request
from flask_restful import Api, Resource
class my_handler(Resource):
def get(self):
return request.base_url
app = Flask(__name__)
api = Api(app)
api.add_resource(my_handler, '/', endpoint='/')
api.add_resource(my_handler, '/hello', endpoint='/hello')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)
通过进行 http 调用,服务器通过返回所请求资源的 url 进行响应:
curl --location --request GET http://127.0.0.1:5000/
"http://127.0.0.1:5000/"
curl --location --request GET http://127.0.0.1:5000/hello
"http://127.0.0.1:5000/hello"
我正在使用 Flask 和 Flask-RESTful 扩展开发 API。我正在创建我的端点和资源,但我对资源“url”和它的“端点”之间的区别感到困惑。据我了解,URL 本身 是 端点。我也在阅读
然而 the Flask-RESTful docs 提到 add_resource
方法有一个 URLs 的参数并且它可以 也 接收端点名称。他们甚至提供了这个例子:
api.add_resource(HelloWorld, '/', '/hello')
api.add_resource(Foo, '/foo', endpoint="foo")
api.add_resource(FooSpecial, '/special/foo', endpoint="foo")
我明白他们在做什么,但我还是不明白端点名称的作用。
URL 和端点名称有什么区别?为什么要设置端点名称?
URL
one or more url routes to match for the resource, standard flask routing rules apply. Any url variables will be passed to the resource method as args.
端点
endpoint name (defaults to Resource.name.lower() Can be used to reference this route in fields.Url fields
当你打算用相同的class管理两个或多个url时,必须指定endpoint参数,实际上,考虑到以下代码片段:
from flask import Flask, request
from flask_restful import Api, Resource
class my_handler(Resource):
def get(self):
return request.base_url
app = Flask(__name__)
api = Api(app)
api.add_resource(my_handler, '/')
api.add_resource(my_handler, '/hello')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)
flask 将无法执行应用程序,引发异常:
Exception has occurred: AssertionError
View function mapping is overwriting an existing endpoint function: my_handler
为了让 flask 处理具有相同 class 的多个 url,您需要指定端点参数:
from flask import Flask, request
from flask_restful import Api, Resource
class my_handler(Resource):
def get(self):
return request.base_url
app = Flask(__name__)
api = Api(app)
api.add_resource(my_handler, '/', endpoint='/')
api.add_resource(my_handler, '/hello', endpoint='/hello')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)
通过进行 http 调用,服务器通过返回所请求资源的 url 进行响应:
curl --location --request GET http://127.0.0.1:5000/
"http://127.0.0.1:5000/"
curl --location --request GET http://127.0.0.1:5000/hello
"http://127.0.0.1:5000/hello"