Skip to main content

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

Project description

sanic-parsedargs

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

sanic-parsedargs 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.0.4.tar.gz (4.5 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: sanic-dantic-1.0.4.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6rc1

File hashes

Hashes for sanic-dantic-1.0.4.tar.gz
Algorithm Hash digest
SHA256 b10e125dfa7faac0a92b7bcb1077d4df0b222be0c852ee87ab1ec13b4f13e6af
MD5 419cf890599df904a23e588d3745dcb1
BLAKE2b-256 4780b87c54f03a3caafb698859573e96a15d2072680ee1aad7792b87ea4cf768

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