flasgger-marshmallow


Nameflasgger-marshmallow JSON
Version 0.0.10 PyPI version JSON
download
home_pagehttps://github.com/flask-rabmq/Flasgger-Marshmallow
Summarycode to swagger document.
upload_time2023-01-16 06:32:05
maintainer
docs_urlNone
authorchenxiaolong
requires_python>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*
licenseMIT
keywords python flask swagger flasgger marshmallow restfull
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # flasgger marshmallow

code to swagger document.


## Installing

Install and update using `pip`:

```
$ pip install -U Flasgger-Marshmallow
```


## A Simple Example

```python
import logging

from flasgger import Swagger
# use basePath   from flasgger_marshmallow import Swagger
from flask import Flask, request
from flask_restful import Api, Resource
from marshmallow import Schema, fields
from flasgger_marshmallow import swagger_decorator

logging.basicConfig(
    filemode="w",
    format="%(asctime)s-%(name)s-%(levelname)s--%(process)d-%(thread)d-%(threadName)s-%(message)s",
    level=logging.INFO
)

logger = logging.getLogger(__name__)

app = Flask(__name__)
api = Api(app)
swagger = Swagger(app)


class MobileSchema(Schema):
    model = fields.String(required=True, doc='手机型号')
    no = fields.String(required=True, doc='手机编号')

    class Meta:
        strict = True


class UserJsonSchema(Schema):
    username = fields.Str(required=True, doc='用户名')
    age = fields.Integer(required=False, default=0, doc='年龄')
    qq = fields.List(fields.String, required=False, doc='用户QQ号')
    email = fields.Email(required=False, doc='邮箱')
    image = fields.URL(required=False, doc='用户头像')
    mobile = fields.Nested(MobileSchema, many=False)

    class Meta:
        strict = True
        unknown = 'EXCLUDE'  # 参数中对多余字段处理 EXCLUDE`-排除, `INCLUDE`-不处理 or `RAISE`-抛异常.


class CreateUserJsonSchema(UserJsonSchema):
    pass

    class Meta:
        strict = True


class CreateUserSuccessResponse(Schema):
    id = fields.Number(required=True)

    class Meta:
        strict = True


class QueryUserSchema(Schema):
    id = fields.Int(required=False, doc='用户ID')
    username = fields.String(required=False, doc='用户名')

    class Meta:
        strict = True


class GetUserResponseSchema(Schema):
    """
    返回符合条件的用户列表
    """
    users = fields.Nested(UserJsonSchema, many=True, doc='用户列表')
    count = fields.Integer(required=True, default=0, doc='用户数量')
    page = fields.Integer(required=True, default=1, doc='当前页码')

    class Meta:
        strict = True


class UserDetailResponseSchema(UserJsonSchema):
    """用户详情"""
    pass

    class Meta:
        strict = True


class responseHeadersSchema(Schema):
    Location = fields.String(required=True, default=1, doc='跳转地址')
    X_RateLimit_Limit = fields.Integer(
        required=True, default=1, doc='Request limit per hour',
        data_key='X-RateLimit-Limit'
    )  # marshmallow 3
    # X_RateLimit_Limit = fields.Integer(
    #     required=True, default=1, doc='Request limit per hour',
    #     load_from='X-RateLimit-Limit', dump_to='X-RateLimit-Limit'
    # ) # marshmallow 2

    class Meta:
        strict = True


class HeadersSchema(Schema):
    Login_Credential = fields.String(
        required=True, doc='登录凭证',
        data_key='Login-Credential'
    )

    class Meta:
        unknown = True


class RedirectResponseSchema(Schema):
    """
    重定向实例
    """

    class Meta:
        headers = responseHeadersSchema
        strict = True


class User(Resource):

    @swagger_decorator(json_schema=CreateUserJsonSchema, response_schema={200: CreateUserSuccessResponse})
    def post(self):
        """
        创建一个用户
        """

        # 获取校验后的数据
        logger.info('%s, %s', type(request.json_schema), request.json_schema)
        return {'id': 1}

    @swagger_decorator(query_schema=QueryUserSchema, response_schema={200: GetUserResponseSchema},
                       headers_schema=HeadersSchema)
    def get(self):
        """
        查询用户
        """

        # 获取校验后的数据
        logger.info('%s, %s', type(request.query_schema), request.query_schema)
        return {"count": 1, "page": 1, "users": [{'username': '陈小龙'}]}

    @swagger_decorator(query_schema=QueryUserSchema, response_schema={302: RedirectResponseSchema})
    def put(self):
        """重定向实例"""
        return None, 302, {'Location': 'http://www.baidu.com', 'X-RateLimit-Limit': 2000}


class UsernamePathSchema(Schema):
    username = fields.String(required=False, doc='用户名')

    class Meta:
        strict = True


class UpdateUserSchema(Schema):
    email = fields.Email(required=False, doc='用户邮箱')
    image = fields.Url(required=False, doc='用户头像')

    class Meta:
        strict = True


class Username(Resource):

    @swagger_decorator(
        path_schema=UsernamePathSchema,
        response_schema={200: UserDetailResponseSchema}
    )
    def get(self, username):
        """
        This examples uses FlaskRESTful Resource    # 这里是简介
        It works also with swag_from, schemas and spec_dict  # 这里是详情
        """

        # 获取校验后的数据
        logger.info('%s, %s', type(request.path_schema), request.path_schema)
        return {'username': username}, 200

    @swagger_decorator(path_schema=UsernamePathSchema,
                       form_schema=UpdateUserSchema,
                       response_schema={200: UserDetailResponseSchema},
                       tags=["AAA"]
                       )
    def put(self, username):
        """
        更新用户信息
        """
        return {'username': username}, 200


api.add_resource(Username, '/username/<username>')
api.add_resource(User, '/users')

app.run(debug=True)
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/flask-rabmq/Flasgger-Marshmallow",
    "name": "flasgger-marshmallow",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*",
    "maintainer_email": "",
    "keywords": "python flask swagger flasgger marshmallow restfull",
    "author": "chenxiaolong",
    "author_email": "cxiaolong6@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4d/8d/d0119fb83bb931dfbd3a55ccf36aa8de3cdd0145cd248073bcae1f059381/flasgger_marshmallow-0.0.10.tar.gz",
    "platform": null,
    "description": "# flasgger marshmallow\n\ncode to swagger document.\n\n\n## Installing\n\nInstall and update using `pip`:\n\n```\n$ pip install -U Flasgger-Marshmallow\n```\n\n\n## A Simple Example\n\n```python\nimport logging\n\nfrom flasgger import Swagger\n# use basePath   from flasgger_marshmallow import Swagger\nfrom flask import Flask, request\nfrom flask_restful import Api, Resource\nfrom marshmallow import Schema, fields\nfrom flasgger_marshmallow import swagger_decorator\n\nlogging.basicConfig(\n    filemode=\"w\",\n    format=\"%(asctime)s-%(name)s-%(levelname)s--%(process)d-%(thread)d-%(threadName)s-%(message)s\",\n    level=logging.INFO\n)\n\nlogger = logging.getLogger(__name__)\n\napp = Flask(__name__)\napi = Api(app)\nswagger = Swagger(app)\n\n\nclass MobileSchema(Schema):\n    model = fields.String(required=True, doc='\u624b\u673a\u578b\u53f7')\n    no = fields.String(required=True, doc='\u624b\u673a\u7f16\u53f7')\n\n    class Meta:\n        strict = True\n\n\nclass UserJsonSchema(Schema):\n    username = fields.Str(required=True, doc='\u7528\u6237\u540d')\n    age = fields.Integer(required=False, default=0, doc='\u5e74\u9f84')\n    qq = fields.List(fields.String, required=False, doc='\u7528\u6237QQ\u53f7')\n    email = fields.Email(required=False, doc='\u90ae\u7bb1')\n    image = fields.URL(required=False, doc='\u7528\u6237\u5934\u50cf')\n    mobile = fields.Nested(MobileSchema, many=False)\n\n    class Meta:\n        strict = True\n        unknown = 'EXCLUDE'  # \u53c2\u6570\u4e2d\u5bf9\u591a\u4f59\u5b57\u6bb5\u5904\u7406 EXCLUDE`-\u6392\u9664, `INCLUDE`-\u4e0d\u5904\u7406 or `RAISE`-\u629b\u5f02\u5e38.\n\n\nclass CreateUserJsonSchema(UserJsonSchema):\n    pass\n\n    class Meta:\n        strict = True\n\n\nclass CreateUserSuccessResponse(Schema):\n    id = fields.Number(required=True)\n\n    class Meta:\n        strict = True\n\n\nclass QueryUserSchema(Schema):\n    id = fields.Int(required=False, doc='\u7528\u6237ID')\n    username = fields.String(required=False, doc='\u7528\u6237\u540d')\n\n    class Meta:\n        strict = True\n\n\nclass GetUserResponseSchema(Schema):\n    \"\"\"\n    \u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u7528\u6237\u5217\u8868\n    \"\"\"\n    users = fields.Nested(UserJsonSchema, many=True, doc='\u7528\u6237\u5217\u8868')\n    count = fields.Integer(required=True, default=0, doc='\u7528\u6237\u6570\u91cf')\n    page = fields.Integer(required=True, default=1, doc='\u5f53\u524d\u9875\u7801')\n\n    class Meta:\n        strict = True\n\n\nclass UserDetailResponseSchema(UserJsonSchema):\n    \"\"\"\u7528\u6237\u8be6\u60c5\"\"\"\n    pass\n\n    class Meta:\n        strict = True\n\n\nclass responseHeadersSchema(Schema):\n    Location = fields.String(required=True, default=1, doc='\u8df3\u8f6c\u5730\u5740')\n    X_RateLimit_Limit = fields.Integer(\n        required=True, default=1, doc='Request limit per hour',\n        data_key='X-RateLimit-Limit'\n    )  # marshmallow 3\n    # X_RateLimit_Limit = fields.Integer(\n    #     required=True, default=1, doc='Request limit per hour',\n    #     load_from='X-RateLimit-Limit', dump_to='X-RateLimit-Limit'\n    # ) # marshmallow 2\n\n    class Meta:\n        strict = True\n\n\nclass HeadersSchema(Schema):\n    Login_Credential = fields.String(\n        required=True, doc='\u767b\u5f55\u51ed\u8bc1',\n        data_key='Login-Credential'\n    )\n\n    class Meta:\n        unknown = True\n\n\nclass RedirectResponseSchema(Schema):\n    \"\"\"\n    \u91cd\u5b9a\u5411\u5b9e\u4f8b\n    \"\"\"\n\n    class Meta:\n        headers = responseHeadersSchema\n        strict = True\n\n\nclass User(Resource):\n\n    @swagger_decorator(json_schema=CreateUserJsonSchema, response_schema={200: CreateUserSuccessResponse})\n    def post(self):\n        \"\"\"\n        \u521b\u5efa\u4e00\u4e2a\u7528\u6237\n        \"\"\"\n\n        # \u83b7\u53d6\u6821\u9a8c\u540e\u7684\u6570\u636e\n        logger.info('%s, %s', type(request.json_schema), request.json_schema)\n        return {'id': 1}\n\n    @swagger_decorator(query_schema=QueryUserSchema, response_schema={200: GetUserResponseSchema},\n                       headers_schema=HeadersSchema)\n    def get(self):\n        \"\"\"\n        \u67e5\u8be2\u7528\u6237\n        \"\"\"\n\n        # \u83b7\u53d6\u6821\u9a8c\u540e\u7684\u6570\u636e\n        logger.info('%s, %s', type(request.query_schema), request.query_schema)\n        return {\"count\": 1, \"page\": 1, \"users\": [{'username': '\u9648\u5c0f\u9f99'}]}\n\n    @swagger_decorator(query_schema=QueryUserSchema, response_schema={302: RedirectResponseSchema})\n    def put(self):\n        \"\"\"\u91cd\u5b9a\u5411\u5b9e\u4f8b\"\"\"\n        return None, 302, {'Location': 'http://www.baidu.com', 'X-RateLimit-Limit': 2000}\n\n\nclass UsernamePathSchema(Schema):\n    username = fields.String(required=False, doc='\u7528\u6237\u540d')\n\n    class Meta:\n        strict = True\n\n\nclass UpdateUserSchema(Schema):\n    email = fields.Email(required=False, doc='\u7528\u6237\u90ae\u7bb1')\n    image = fields.Url(required=False, doc='\u7528\u6237\u5934\u50cf')\n\n    class Meta:\n        strict = True\n\n\nclass Username(Resource):\n\n    @swagger_decorator(\n        path_schema=UsernamePathSchema,\n        response_schema={200: UserDetailResponseSchema}\n    )\n    def get(self, username):\n        \"\"\"\n        This examples uses FlaskRESTful Resource    # \u8fd9\u91cc\u662f\u7b80\u4ecb\n        It works also with swag_from, schemas and spec_dict  # \u8fd9\u91cc\u662f\u8be6\u60c5\n        \"\"\"\n\n        # \u83b7\u53d6\u6821\u9a8c\u540e\u7684\u6570\u636e\n        logger.info('%s, %s', type(request.path_schema), request.path_schema)\n        return {'username': username}, 200\n\n    @swagger_decorator(path_schema=UsernamePathSchema,\n                       form_schema=UpdateUserSchema,\n                       response_schema={200: UserDetailResponseSchema},\n                       tags=[\"AAA\"]\n                       )\n    def put(self, username):\n        \"\"\"\n        \u66f4\u65b0\u7528\u6237\u4fe1\u606f\n        \"\"\"\n        return {'username': username}, 200\n\n\napi.add_resource(Username, '/username/<username>')\napi.add_resource(User, '/users')\n\napp.run(debug=True)\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "code to swagger document.",
    "version": "0.0.10",
    "split_keywords": [
        "python",
        "flask",
        "swagger",
        "flasgger",
        "marshmallow",
        "restfull"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4d8dd0119fb83bb931dfbd3a55ccf36aa8de3cdd0145cd248073bcae1f059381",
                "md5": "9824e281d51e1801056a25c33522bd8c",
                "sha256": "a47c60d21fe6e72af2db98ef9dcfe9f48829e336b995b911dae51606301239c5"
            },
            "downloads": -1,
            "filename": "flasgger_marshmallow-0.0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "9824e281d51e1801056a25c33522bd8c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*",
            "size": 8621,
            "upload_time": "2023-01-16T06:32:05",
            "upload_time_iso_8601": "2023-01-16T06:32:05.175590Z",
            "url": "https://files.pythonhosted.org/packages/4d/8d/d0119fb83bb931dfbd3a55ccf36aa8de3cdd0145cd248073bcae1f059381/flasgger_marshmallow-0.0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-16 06:32:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "flask-rabmq",
    "github_project": "Flasgger-Marshmallow",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "flasgger-marshmallow"
}
        
Elapsed time: 0.06670s