Skip to main content

一种新的方式来生成结构化的 JSON 响应,支持开发模式、自定义数据和消息,提升前后端交互的效率与一致性。

Project description

Flask Responser

flask_responser 是一个简单的 Python 库,旨在帮助开发者快速构建标准化的 HTTP JSON 响应,尤其适用于 Flask 框架的后端开发。


安装

pip install flask_responser

功能概述

flask_responser 提供了一个灵活的 Response 类,用于生成符合 RESTful 风格的 JSON 响应,包含以下功能:

  • 支持外部消息 (ext_mes):用于显示给最终用户的消息。
  • 支持内部消息 (int_mes):用于前端开发者调试的消息。
  • 内部状态码 (int_code):用于前端开发者区分不同错误类型。
  • 自定义数据字段 (data):返回给前端的实际数据。
  • 开发模式支持:在开发模式下,未设置的 int_mesint_code 会自动与 ext_messtatus_code 同步。

使用方法

导入模块

from flask_responser import Response

示例代码

示例 1:基本用法

from flask import Flask
from flask_responser import Response

app = Flask(__name__)

@app.route('/')
def home():
    res = Response(
        status_code=200,
        ext_mes="请求成功",
        data={"key": "value"}
    )
    return res.make()

if __name__ == '__main__':
    app.run(debug=True)

示例 2:启用开发模式

res = Response(
    status_code=400,
    ext_mes="无效的请求",
    dev=True
)
response = res.make()

在开发模式下,若未指定 int_mesint_code,则:

  • int_mes 自动设置为 ext_mes
  • int_code 自动设置为 status_code

示例 3:自定义响应头

res = Response(
    status_code=200,
    ext_mes="操作成功",
    data={"result": "ok"},
    custom_header="HeaderValue"
)
response = res.make()

参数说明

初始化参数

参数名 类型 默认值 描述
status_code int 200 HTTP 状态码,返回给浏览器。
ext_mes str "" 显示给最终用户的外部消息。
int_mes str "" 内部消息,用于前端开发者调试。
int_code int 0 内部状态码,用于区分不同错误类型。
data dict None 返回给前端的数据。
dev bool True 是否启用开发模式。启用后,未设置的 int_mesint_code 会被自动填充。
**kwargs dict {} 其他自定义的响应头。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_responser-0.0.1a0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_responser-0.0.1a0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file flask_responser-0.0.1a0.tar.gz.

File metadata

  • Download URL: flask_responser-0.0.1a0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for flask_responser-0.0.1a0.tar.gz
Algorithm Hash digest
SHA256 4a73faa1f707e2635b8c422d465e986d702b72535feface5710670096b888007
MD5 1e82daf1fed17b58a4704e843f5362ce
BLAKE2b-256 265ef97c9ccc6fbe52f4e0d4ba25ad17f7e4f0e10078bb2e7303d741a72fb196

See more details on using hashes here.

File details

Details for the file flask_responser-0.0.1a0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_responser-0.0.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 d51ffdfaf66b8dceaa4171627cf51319c7bc7f863db7e9bea4dc6b3401ea60df
MD5 50f633f7d1c5a18730c17637157b93c8
BLAKE2b-256 e81864119c6f030d619ea9715b79b9467d51f93c9f95dc8391e53a9fa9c38a84

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page