Python 使用Flask处理REST子资源
我正在创建一个rest应用程序,我有一个名为“job”的资源,它有一组固定的子资源:Python 使用Flask处理REST子资源,python,rest,flask,Python,Rest,Flask,我正在创建一个rest应用程序,我有一个名为“job”的资源,它有一组固定的子资源: http://domain.com/api/job/1/ http://domain.com/api/job/1/phase http://domain.com/api/job/1/owner 我目前正在使用Flask的MethodView创建RESTAPI。但我无法决定哪种方法是最好的映射方法。我是否应该为每个子资源使用单独的类,如下所示: class ApiJob(MethodView): def
http://domain.com/api/job/1/
http://domain.com/api/job/1/phase
http://domain.com/api/job/1/owner
我目前正在使用Flask的MethodView创建RESTAPI。但我无法决定哪种方法是最好的映射方法。我是否应该为每个子资源使用单独的类,如下所示:
class ApiJob(MethodView):
def get(self, job_id):
pass
class ApiJobPhase(MethodVIew):
def get(self, job_id):
pass
class ApiJobOwner(MethodView):
def get(self, job_id):
pass
app.add_url_rule("/jobs/<int:job_id>", view_func=ApiJob.as_view("job"), methods=["GET"])
app.add_url_rule("/jobs/<int:job_id>/phase", view_func=ApiJobPhase.as_view("phase"), methods=["GET"])
app.add_url_rule("/jobs/<int:job_id>/owner", view_func=ApiJobOwner.as_view("owner"), methods=["GET"])
class ApiJob(MethodView):
def get(self, job_id, res_id):
if(res_id == None):
# deal with the "job" resource
elif(res_id == "phase"):
# deal with the "phase" sub-resource
elif(res_id == "owner"):
# deal with the "owner" sub-resource
else:
# unknown subresource return some HTTP error code
apl.add_url_rule("/jobs/<int:job_id>/", view_func=ApiJob.as_view("job"), defaults={"res_id": None}, methods=["GET"])
api.add_url_rule("/jobs/<int:job_id>/<string:res_id>", view_func=ApiJob.as_view("jobres"), methods=["GET"])
类ApiJob(方法视图):
def get(自我,作业id):
通过
类ApiJobPhase(方法视图):
def get(自我,作业id):
通过
类ApiJobOwner(方法视图):
def get(自我,作业id):
通过
app.add_url_rule(“/jobs/”,view_func=ApiJob.as_view(“job”),methods=[“GET”])
app.add_url_rule(“/jobs//phase”,view_func=ApiJobPhase.as_view(“phase”),methods=[“GET”])
app.add\u url\u规则(“/jobs//owner”,view\u func=ApiJobOwner.as\u view(“owner”),methods=[“GET”])
或者我应该通过作业类处理所有子资源,如下所示:
class ApiJob(MethodView):
def get(self, job_id):
pass
class ApiJobPhase(MethodVIew):
def get(self, job_id):
pass
class ApiJobOwner(MethodView):
def get(self, job_id):
pass
app.add_url_rule("/jobs/<int:job_id>", view_func=ApiJob.as_view("job"), methods=["GET"])
app.add_url_rule("/jobs/<int:job_id>/phase", view_func=ApiJobPhase.as_view("phase"), methods=["GET"])
app.add_url_rule("/jobs/<int:job_id>/owner", view_func=ApiJobOwner.as_view("owner"), methods=["GET"])
class ApiJob(MethodView):
def get(self, job_id, res_id):
if(res_id == None):
# deal with the "job" resource
elif(res_id == "phase"):
# deal with the "phase" sub-resource
elif(res_id == "owner"):
# deal with the "owner" sub-resource
else:
# unknown subresource return some HTTP error code
apl.add_url_rule("/jobs/<int:job_id>/", view_func=ApiJob.as_view("job"), defaults={"res_id": None}, methods=["GET"])
api.add_url_rule("/jobs/<int:job_id>/<string:res_id>", view_func=ApiJob.as_view("jobres"), methods=["GET"])
类ApiJob(方法视图):
def get(自我、作业id、资源id):
如果(res_id==无):
#处理“工作”资源
elif(res_id==“阶段”):
#处理“阶段”子资源
elif(res_id==“所有者”):
#处理“所有者”子资源
其他:
#未知子资源返回一些HTTP错误代码
apl.add_url_rule(“/jobs/”,view_func=ApiJob.as_view(“job”),默认值={“res_id”:None},方法=[“GET”])
api.add_url_rule(“/jobs/”,view_func=ApiJob.as_view(“jobres”),methods=[“GET”])
你认为哪一个更好?为什么?如果你认为两者都不好,有没有更好的方法
谢谢。第一个更好。为什么?1) 在
get()
方法中没有if-else
语句。2) 假设将来每个视图都有post/put/delete方法(还有一个'if-else'?)。3) 1类-1类责任比1类责任更好。我也喜欢使用装饰器。