如何解决单独的资源链接 Flask Restful API
我正在学习 Flask Restful API,在学习一些教程时我遇到了一个例子
class Student(Resource):
def get(self):
return {student data}
def post(self,details):
return {data stored}
api.add_resource(Student,'/student')
在这里,看看上面的例子,我们可以使用 /student 和 GET、POST 方法来检索和存储数据。 但我想有 2 个不同的端点来检索和存储数据,每个端点。 例如
/student/get
/student/post
调用Student类的post()函数,存储发送/发布的数据。
解决方法
是的,可以有一个 -- use date literal
select * from V_IV_MUC where TRUNC(VALUE_TIMESTAMP) = date '1980-02-01';
-- use date string with format
select * from V_IV_MUC where TRUNC(VALUE_TIMESTAMP) = to_date('01-FEB-80','dd-mon-rr');
类,但调用由不同端点引用的不同方法。
场景:
- 我们有一个带有
Resource
和Student
方法的get
类。 - 我们可以使用不同的路由来分别或组合执行
post
和get
方法。 - 例如:
- 端点
post
只能用于http://localhost:5000/students/get
类的get
请求。 - 端点
Student
只能用于http://localhost:5000/students/post
类的post
请求。 - 端点
Student
可用于http://localhost:5000/students/
类的get
和post
请求。
- 端点
解决方案:
- 为了控制资源类中不同的端点请求,我们需要向它传递一些关键字参数。
- 我们将使用
Student
方法的resource_class_kwargs
选项。add_resource
的详细信息可以在 official documentation 中找到
- 我们将使用
add_resource
方法阻止任何不需要的方法调用。对于端点中不需要的方法调用,我们将返回 HTTP 状态abort
和响应消息405
。
代码:
Method not allowed
预期行为:
-
from flask import Flask from flask_restful import Resource,Api,abort,reqparse app = Flask(__name__) api = Api(app) parser = reqparse.RequestParser() parser.add_argument('id',type=int,help='ID of the student') parser.add_argument('name',type=str,help='Name of the student') def abort_if_method_not_allowed(): abort(405,message="Method not allowed") students = [{"id" : 1,"name": "Shovon"},{"id" : 2,"name": "arsho"}] class Student(Resource): def __init__(self,**kwargs): self.get_request_allowed = kwargs.get("get_request_allowed",False) self.post_request_allowed = kwargs.get("post_request_allowed",False) def get(self): if not self.get_request_allowed: abort_if_method_not_allowed() return students def post(self): if not self.post_request_allowed: abort_if_method_not_allowed() student_arguments = parser.parse_args() student = {'id': student_arguments['id'],'name': student_arguments['name']} students.append(student) return student,201 api.add_resource(Student,'/students',endpoint="student",resource_class_kwargs={"get_request_allowed": True,"post_request_allowed": True}) api.add_resource(Student,'/students/get',endpoint="student_get",resource_class_kwargs={"get_request_allowed": True}) api.add_resource(Student,'/students/post',endpoint="student_post",resource_class_kwargs={"post_request_allowed": True})
应该调用curl http://localhost:5000/students/get
类的get
方法。 -
Student
应该调用curl http://localhost:5000/students/post -d "id=3" -d "name=Sho" -X POST -v
类的post
方法。 -
Student
可以调用curl http://localhost:5000/students
类的两个方法。
测试:
- 我们将调用我们登记的端点并测试每个端点的行为是否符合预期。
- 使用
Student
在get
中输出students/get
请求:
curl http://localhost:5000/students/get
- 使用
[ { "id": 1,"name": "Shovon" },{ "id": 2,"name": "arsho" } ]
在post
中输出students/post
请求:
curl http://localhost:5000/students/post -d "id=3" -d "name=Shody" -X POST -v
- 使用
{ "id": 3,"name": "Shody" }
在students
中输出 get 请求:
curl http://localhost:5000/students
- 使用
[ { "id": 1,"name": "arsho" },{ "id": 3,"name": "Shody" } ]
在post
中输出students
请求:
curl http://localhost:5000/students -d "id=4" -d "name=Ahmedur" -X POST -v
- 使用
{ "id": 4,"name": "Ahmedur" }
在post
中输出students/get
请求:
curl http://localhost:5000/students/get -d "id=5" -d "name=Rahman" -X POST -v
- 使用
{ "message": "Method not allowed" }
在get
中输出students/post
请求:
curl http://localhost:5000/students/post
参考:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。