Skip to main content

Make it easy to serialize flask api request and response

Project description

flask-rest-serializer

Make flask request parse and response serialize easier.

Ability

  • define request and response schema with Marshmallow.
  • request serialize and validation (validation is supported by Marshmallow Schema load).
  • field can specify request location to get value , location is one of headers, body(form or json), args). If not set location will be args for http GET, DELETE and body for http POST, PUT, PATCH.
  • return Python object(Sqlalchemy Model instance or customer Data Object) directly in your view functions.
  • swagger generation according to request and response schema.

Configurations

flask_config_key type description
REST_SERIALIZER_BLUEPRINT_SEPARATE_DOCS boolean if set True, will generate separated swagger doc file for blueprint. if set False only one doc file will be generated.
REST_SERIALIZER_VALIDATION_ERROR_HANDLER string abort, string re_raise or a callable object behaviour when serialize request occurred a ValidationError. if set abort, will raise a HttpException. if set re_raise will reraise ValidationError. if set a callable object, will call it with ValidationError

Usage

# schemas.py
from marshmallow import fields
from marshmallow.schema import Schema


class UserSchema(Schema):
    id = fields.Integer()
    username = fields.String()


class QueryUserSchema(Schema):
    username = fields.String()


class CreateUserSchema(Schema):
    username = fields.String(required=True, allow_none=False,
                             metadata={"location": "body"})  # if location not set, 

# app.py
from flask import Flask
from flask_restful import Api, Resource

from example.schemas import CreateUserSchema, QueryUserSchema, UserSchema
from flask_rest_serializer import generate_swagger, serialize_with_schemas

app = Flask("example")
api = Api(app, prefix="/rest")


class User:
    def __init__(self, id, username):
        self.id = id
        self.username = username


user_one = User(id=1, username="one")
user_two = User(id=2, username="two")

users = [user_one, user_two]


@app.route("/users", methods=["GET"])
@serialize_with_schemas(request_schema=QueryUserSchema,
                        response_schema=UserSchema(many=True))
def get_users(username):
    return [user for user in users if username in user.username]


@app.route("/users/<int:user_id>")
@serialize_with_schemas(response_schema=UserSchema)
def get_user_by_id(user_id):
    for user in users:
        if user.id == user_id:
            return user

    return None


@api.resource("/users")
class UserResource(Resource):
    @serialize_with_schemas(request_schema=CreateUserSchema,
                            response_schema=UserSchema)
    def post(self, username):
        new_user = User(id=3, username=username)
        return new_user


generate_swagger(app, "1.0", "./", "yaml")

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-rest-serializer-0.0.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

flask_rest_serializer-0.0.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file flask-rest-serializer-0.0.2.tar.gz.

File metadata

  • Download URL: flask-rest-serializer-0.0.2.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.6.5

File hashes

Hashes for flask-rest-serializer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7b1295e72fa3442e04d017cfaf6c96a4a534ee040a971ac3dddc31a54a6bc4f1
MD5 4587fffccea638679ca8eedf7cdc8c2c
BLAKE2b-256 cb81f5e4de351ca7ced285325b338b817286474328f456d3836d1a86d54c3ecb

See more details on using hashes here.

File details

Details for the file flask_rest_serializer-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: flask_rest_serializer-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.6.5

File hashes

Hashes for flask_rest_serializer-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 07089840ceab873039a767417e493cd16ce2f2e56bd8c6421ba09a8617c91b45
MD5 0c1dd1618ec00351b20b7d027a38faa8
BLAKE2b-256 d839ca737f81aad2ca6b66674657e81882c6a03cfc01a96dcabe5761bdc983e2

See more details on using hashes here.

Supported by

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