单独的资源链接 Flask Restful API

如何解决单独的资源链接 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类的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'); 类,但调用由不同端点引用的不同方法。

场景:

  • 我们有一个带有 ResourceStudent 方法的 get 类。
  • 我们可以使用不同的路由来分别或组合执行 postget 方法。
  • 例如:
    • 端点 post 只能用于 http://localhost:5000/students/get 类的 get 请求。
    • 端点 Student 只能用于 http://localhost:5000/students/post 类的 post 请求。
    • 端点 Student 可用于 http://localhost:5000/students/ 类的 getpost 请求。

解决方案:

  • 为了控制资源类中不同的端点请求,我们需要向它传递一些关键字参数。
  • 我们将使用 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 类的两个方法。

测试:

  • 我们将调用我们登记的端点并测试每个端点的行为是否符合预期。
  • 使用 Studentget 中输出 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?