【发布时间】:2014-06-03 20:35:46
【问题描述】:
我正在尝试为每个 RESTful API 端点生成相关链接不仅仅是当前请求。另一种也是可以接受的方式是,我想生成当前蓝图的所有端点(在本例中称为“blueprint_name”)。这是我当前设置的摘要:
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。动词很重要,因为没有它,链接是不完整/无用的。
【问题讨论】:
-
使用request.url和request.method有什么问题?
-
我会修正我的回复 - 但要更具体一些。我希望每个端点都返回其他端点。使用“请求”仅给出当前请求,而不是当前蓝图的所有可能请求
标签: rest url http-method flask