当前位置: 首页 > article >正文

使用flask_restful快速构建接口

Flask-RESTful 是一个用于快速构建 RESTful API 的 Flask 扩展。它简化了创建、管理和文档化 REST API 的过程。利用 Flask-RESTful,你可以更容易地将你的 Flask 应用程序组织成 RESTful 原则的风格

安装包

pip install flask_restful

 快速构建接口

from flask import Flask
from flask_restful import reqparse, Resource, Api

app = Flask(__name__)
# 步骤一:创建了一个Flask应用实例

api = Api(app)
# 步骤二:创建restful的API
# 通过将Flask应用实例app传递给Api类来创建API对象。这一步是在告诉Flask-RESTful这个API将基于哪个Flask应用


class HelloWorld(Resource):
    # 定义了一个名为HelloWorld的类,它继承自Resource。在该类内部定义了一个get方法,当有HTTP GET请求到达与之关联的URL时,就会调用此方法
    def get(self):
        return {'hello': 'world'}


api.add_resource(HelloWorld, '/helloworld')
# 使用add_resource方法将HelloWorld资源类绑定到特定的URL路径/helloworld上。这意味着当用户访问http://<your-server>/helloworld时,会触发HelloWorld类中的get方法
if __name__ == '__main__':
    # 启动app
    app.run(debug=True)
"""
def run(
        self,
        host: str | None = None,
        port: int | None = None,
        debug: bool | None = None,
        load_dotenv: bool = True,
        **options: t.Any,
    ) -> None:
"""
# run函数的参数有host,port,我们就可以指定ip+端口启动flask

 这样我们就可以快速的通过flask+flask_restful构建出来一个接口,默认启动地址是http://127.0.0.1:5000 ,我们使用postman测试一下

这样我们就快速的实现了一个简单的接口

get请求传参

get请求传参可以放在url中,也可以放在请求头中,我们来查看如何进行传参

参数在url的?之前

class HelloWorld(Resource):
    def get(self, name):
        return {'hello': name}


api.add_resource(HelloWorld, '/helloworld/<string:name>')
# <string:name> 会将这个参数转为字符串,也可以是int,float等等,就可以传主键各种的

参数在url的?之后

方法一:直接从原始数据拿
class HelloWorld(Resource):
    def get(self):
        name = request.args.get('name')
        return {'hello': name}


api.add_resource(HelloWorld, '/helloworld')
from flask import Flask, request

通过导入request来到args里面去取值,这样就拿到原始的参数

方法二: 用解析器获取
class HelloWorld(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        # 初始化了一个 RequestParser 实例,用于解析传入的请求
        parser.add_argument('name', type=str, required=True, help="Please enter your name", location='args')
        """
        向 RequestParser 添加了一个新的参数规则
        'name':这是你期望从请求中获取的参数名称
        type=str:指定该参数的数据类型应为字符串
        required=True:表示这个参数是必需的。如果请求中没有提供这个参数,API 会返回一个错误响应
        help="Please enter your name":当提供的参数不符合要求时(例如缺失),将返回给客户端的错误信息
        location='args':指定了查找参数的位置。这里的 'args' 表示查询参数(即 URL 中问号后的键值对)
        这个在django中的话,其实可以理解为他序列化器的序列化字段,会对传入的参数进行校验,不同的是django需要我们将request里面的参数传到序列化器
        """
        args = parser.parse_args()
        # 调用 parse_args() 方法将会根据之前定义的规则解析请求,并返回包含所有参数的字典
        name = args['name']
        # 从字典里面拿到参数
        return {'hello': name}

post请求传参

因为是restful规范的,所以只需要我们在类中写一个post请求的函数名,用post请求访问这个函数就可以直接触发post请求了

为了简化,下面所有的post请求我都用请求体传参,虽然post请求也可以请求头,解析跟get一样的,参考get请求即可

方法一:直接从原始数据拿
class HelloWorld(Resource):
    def post(self):
        print(request.data)
        # b'{\r\n    "name":"zh"\r\n}'
        name = json.loads(request.data)['name']
        return {'message': f'Hello World!{name}'}

上面我们直接拿到post请求请求体里面的原始json数据,需要我们反序列化然后直接从字典里面去拿到请求体里面的值

方法二:用解析器获取
class HelloWorld(Resource):
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('name', type=str, required=True, help="Please enter your name", location='json')
        """
        这个与get请求不同就是在location的参数,json代表从请求体里面获取
        """
        args = parser.parse_args()
        name = args['name']
        return {'message': f'Hello World!{name}'}

解析器参数

location参数

# Look only in the POST body 表单
parser.add_argument('name', type=str, location='form')

# Look only in the querystring 请求地址?后面的参数
parser.add_argument('name', type=str, location='args')

# From the request headers
parser.add_argument('name-Agent', location='headers')

# From http cookies
parser.add_argument('session_id', location='cookies')

# From json
parser.add_argument('user_id', location='json')

# From file uploads 文件提交
parser.add_argument('picture', location='files')

  # 可指明多个位置,中括号 [ ]

parser.add_argument('text', location=['headers', 'json'])

 其他参数

default:默认值,如果这个参数没有值,那么将使用这个参数指定的值。
required:是否必须。默认为False,如果设置为True,那么这个参数就必须提交上来。
type:这个参数的数据类型,如果指定,那么将使用指定的数据类型来强制转换提交上来的值。
choices:选项。提交上来的值只有满足这个选项中的值才符合验证通过,否则验证不通过。
help:错误信息。如果验证失败后,将会使用这个参数指定的值作为错误信息。
trim:是否要去掉前后的空格

自定义返回格式

在 Flask-RESTful 中,你可以使用 fields 模块和 marshal_with 装饰器来定义和控制 API 响应的格式。这允许你指定哪些字段应该包含在输出中,以及这些字段的数据类型和格式

class HelloWorld(Resource):    
    resource_fields = {
        'name': fields.String,
        'age': fields.Integer,
        'height': fields.Float,
    }

    @marshal_with(resource_fields)
    def post(self):
        result = {'name': 'John Doe', 'age': '30', 'height': 1.75}
        return result

在上面的代码中,我们原始数据中的age是字符类型,但是我们定义的返回是整型,最终调用接口发现结果还是整型

证明在返回的过程中,marshal_with装饰器对result的内容进行了与resource_fields类型匹配与转化,如果我们把age改为String看结果

会发现直接就把成字符串返回出去了,证明确实是上面的猜想没错,这个过程可以理解django中序列化器的序列化,直接将我们查到的数据库数据按照指定类型响应出去

如果我们返回是的一个空,他还是会把这些定义好的参数返回出去,只是内容是空的

流式返回

from flask import Flask, request, Response
from flask_restful import Api, Resource, fields, marshal_with, reqparse

app = Flask(__name__)

api = Api(app)


def generate():
    """模拟一个长时间运行的任务"""
    for i in range(10):
        yield f"data chunk {i}\n"
        # 模拟延迟
        import time
        time.sleep(1)


class HelloWorld(Resource):
    def post(self):
        return Response(generate(), mimetype='text/plain')

响应就会是这样的

关于mimetype

这里 Response 对象被创建时指定了 mimetype='text/plain'。这意味着无论 generate() 生成器函数产生的数据是什么,都会被当作纯文本发送给客户端。当客户端接收到这个响应时,它将根据 text/plain MIME 类型来决定如何处理该响应内容——通常是直接显示或以文本形式处理。

其他常见的 MIME 类型

这里有一些其他常用的 MIME 类型的例子:

  • text/html: HTML 格式的文本。

  • application/json: JSON 格式的数据。

  • application/xml: XML 格式的数据。

  • image/jpeg: JPEG 格式的图片。

  • application/pdf: PDF 文档 

flask_restful+OpenAI调用千问

 调用大模型参考:如何通过OpenAI接口调用通义千问模型_大模型服务平台百炼(Model Studio)-阿里云帮助中心

配置api_key: 通义千问模型的completions接口_大模型服务平台百炼(Model Studio)-阿里云帮助中心

from flask import Flask, Response
from flask_restful import Api, Resource, fields, reqparse

app = Flask(__name__)

api = Api(app)
from openai import OpenAI

client = OpenAI(
    # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
    api_key='',
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  # 填写DashScope SDK的base_url
)


def wq_completion(user_input):
    completions = client.completions.create(
        model="qwen2.5-coder-32b-instruct",
        prompt=user_input,
        stream=True
    )
    for completion in completions:
        yield completion.choices[0].text


class HelloWorld(Resource):
    resource_fields = {
        'name': fields.String,
        'age': fields.String,
        'height': fields.Float,
    }

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('input', type=str, required=True, help="Please enter your input", location='json')
        args = parser.parse_args()
        input = args['input']
        return Response(wq_completion(input), mimetype='text/plain')


api.add_resource(HelloWorld, '/helloworld')
if __name__ == '__main__':
    app.run()

前面有个问号,很奇怪,应该是返回的问题,查看一下原始数据

第一句有一个?号加换号,那我们就进行处理一下 

def wq_completion(user_input):
    completions = client.completions.create(
        model="qwen2.5-coder-32b-instruct",
        prompt=user_input,
        stream=True
    )
    for completion in completions:
        text = completion.choices[0].text
        if '?' in text:
            text = text.replace('?', '').replace('?', '').strip('\n')
        if text:
            yield text

# 这个问题挺奇怪的,可能是这个模型特别的问题,这样就正常了


http://www.kler.cn/a/596531.html

相关文章:

  • golang压力测试工具如hey或wrk使用介绍
  • 可编辑52页PPT | 智慧园区安全生产顶层设计方案
  • [项目]基于FreeRTOS的STM32四轴飞行器: 十一.MPU6050配置与读取
  • 数据结构之双向链表-初始化链表-头插法-遍历链表-获取尾部结点-尾插法-指定位置插入-删除节点-释放链表——完整代码
  • 【Golang】go语言操作redis数据库--go-redis
  • pikachu,phpstudy启动遇到的问题,本地安装过mysql
  • 构建自定义MCP天气服务器:集成Claude for Desktop与实时天气数据
  • 智慧港口新未来:大数据赋能应急消防,筑牢安全防线
  • 置信度是什么?
  • Collectors.toList / list 转 list
  • 清晰易懂的 Python 彻底卸载与清理教程
  • Java面试高频问题深度解析:JVM、锁机制、SQL优化与并发处理
  • 《鸿蒙原生应用开发:掌控Ability生命周期的艺术》
  • AWS 日本东京 EC2 VPS 性能、线路评测
  • ESPNOW收发测试 基于esp-idf
  • 1、正点原子ZYNQ最小系统--GPIO之MIO控制LED笔记
  • HCIP交换机hybrid接口实验
  • Minine源码设计逻辑解析
  • STM32F103C8T6 -MINI核心板
  • MyBatis参数赋值技巧:#{} 和 ${} 的区别与实践