Skip to main content

A request parameter checking and parsing library based on pydantic under the sanic framework

Project description

sanic-dantic

Travis (.com) Codecov PyPI - Python Version PyPI PyPI - Downloads PyPI - License

sanic-dantic is a request parameter checking and parsing library based on pydantic under the sanic framework

sanic-dantic is a request parameter checking and parsing library based on pydantic under the sanic framework

It is based on pydantic, which can facilitate developers to quickly check and obtain request parameters

Installation

pip install sanic-dantic

Usage

Before writing all the examples, let's write a file server.py first. In other examples, we will focus on the use of views and no longer write extraneous peripheral code.

from sanic import Sanic
from sanic.response import json

from sanic_dantic import parse_params, BaseModel, validator

app = Sanic("sanic-dantic-example")


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

Before using, we need to use pydantic to create a class for format checking of request parameters

class Person(BaseModel):
    name: str
    age: int

Use in function-based views

In the function view, you can pass the pydantic model to different formal parameters in parse_params to check and parse the values of different types of request parameters

You can get all the parsed parameters by appending the formal parameter params, and get the value of the parameter through the attribute

@app.route('/path_example/<name>/<age>/')
@parse_params(path=Person)
async def path_param_examples(request, name, age, params):
    print(params.name, params.age)
    return json({"message": f"hello {params.name} are you {params.age} years old ?"})

If you do not want to pass additional parameters in the form of params way to get the parameters of the resolution, you can also get them by request.ctx.params

@app.route('/get_example/')
@parse_params(query=Person)
async def path_param_examples(request, params):
    print(params.name, params.age)
    print(request.ctx.params.name, request.ctx.params.age)
    return json({"message": f"hello {params.name} are you {params.age} years old ?"})
@app.route('/post_example/')
@parse_params(body=Person)
async def path_param_examples(request, params):
    print(params.name, params.age)
    print(request.ctx.params.name, request.ctx.params.age)
    return json({"message": f"hello {params.name} are you {params.age} years old ?"})

Use in class-based views

Of course, sanic-dantic not only supports function-based views, it also supports class-based views

class SanicDanticExampleView(HTTPMethodView):
    @parse_params(query=Person)
    def get(self, request):
        name, age = request.ctx.params.values()
        return json({"message": f"hello {name} are you {age} years old ?"})

    @parse_params(body=Person)
    def put(self, request):
        name, age = request.ctx.params.values()
        return json({"message": f"hello {name} are you {age} years old ?"})

    @parse_params(body=Person)
    def put(self, request):
        name, age = request.ctx.params.values()
        return json({"message": f"hello {name} are you {age} years old ?"})

Notice

If you need, you can even add inspection classes for path, query, and body at the same time

@app.route('/get_example/')
@parse_params(query=Person, body=Person)
async def path_param_examples(request, params):
    print(params.name, params.age)
    print(request.ctx.params.name, request.ctx.params.age)
    return json({"message": f"hello {params.name} are you {params.age} years old ?"})

However, if the parameter names in different types of parameters are consistent, the original parameters may be overwritten

> curl 'http://localhost:8000/get_example/?name=Connor&age=10' -x POST -d '{"name":"Calvin", "age":20}'
{"message": "hello Calvin are you 20 years old ?"}

The priority of different parameters is body > query > path

Other

sanic-dantic integrates most of the usage of pydantic, other more usage methods can refer to pydantic

Thanks

Thanks to the author Eric Jolibois of pydantic and the author Ahmed Nafies of sanic-pydantic.

Because of their open source, I have the inspiration to write sanic-dantic. Thank them very much

License

I'm pleased to support the open source community by making sanic-dantic available. Copyright (C) 2020, Connor Zhang. All rights reserved. If you have downloaded a copy of the sanic-dantic source code from here, please note that sanic-dantic source code is licensed under the MIT License. Your integration of sanic-dantic into your own projects may require compliance with the MIT License. A copy of the MIT License is included in this file.

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

sanic-dantic-1.1.1.tar.gz (7.3 kB view details)

Uploaded Source

File details

Details for the file sanic-dantic-1.1.1.tar.gz.

File metadata

  • Download URL: sanic-dantic-1.1.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.2

File hashes

Hashes for sanic-dantic-1.1.1.tar.gz
Algorithm Hash digest
SHA256 282d9fd8e204119a3c6fa919b0fb70cd67ffd7e591911ddd7c8419f12f806338
MD5 0f848732917644889ae96ef13a27414d
BLAKE2b-256 c107311a9c1171c39bc67012762be4c457ea5be90aaf27362431cd734bdf7363

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