我正在尝试为每个 RESTful API 端点生成相关链接,而不仅仅是当前请求。另一种也是可以接受的方式是,我想生成当前蓝图的所有端点(在本例中称为“蓝图名称”)。这是我当前设置的摘要:
def function_that_generates_links():
#what should I put here?
blueprint_name = Blueprint('blueprint_name', __name__, url_prefix='/blueprint_name')
@blueprint_name.route('/', methods=['GET'])
def endpoint_name():
#regular_data_being_sent_out is defined somewhere here
return jsonify(data=regular_data_being_sent_out,
links=function_that_generates_links())
@blueprint_name.route('/other_place', methods=['POST'])
def endpoint_name_other():
#regular_data_being_sent_out is defined somewhere here
return jsonify(data=regular_data_being_sent_out,
links=function_that_generates_links())
@blueprint_name.route('/another_place', methods=['DELETE'])
def endpoint_name_another_place():
#regular_data_being_sent_out is defined somewhere here
return jsonify(data=regular_data_being_sent_out,
links=function_that_generates_links())
@blueprint_name.route('/yet_another_place', methods=['PUT'])
def endpoint_name_yet_another_place():
#regular_data_being_sent_out is defined somewhere here
return jsonify(data=regular_data_being_sent_out,
links=function_that_generates_links())
我想将所有其他端点的适当http“签名”附加到每个端点发出的每个响应中。在上面的示例代码中,'function_that_generates_links()' 将是执行此操作的函数。我已经发现url_encode() 提供了我可以使用的必要链接,但我还想要适当的 http 动词(GET、POST、DELETE...等)。它正在找到我坚持的相应 http-verb/method。动词很重要,因为没有它,链接是不完整/无用的。