GithubHelp home page GithubHelp logo

notsobot / nsb.api.sanic-dantic Goto Github PK

View Code? Open in Web Editor NEW

This project forked from miss85246/sanic-dantic

0.0 0.0 0.0 122 KB

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

Home Page: https://miss85246.github.io/sanic-dantic/

License: GNU General Public License v3.0

Python 100.00%

nsb.api.sanic-dantic's Introduction

sanic-dantic

Build Status Downloads PyPI - Python Version PyPI PyPI - License Docs

当前为 sanic-dantic 的英文简介,如果您想阅读中文版本,请点击这里

Introduction

sanic-dantic is a request parameter check and parsing library based on the sanic framework. It is based on pydantic and can help developers quickly check and get request parameters.

Why sanic-dantic

In many cases, we need to check and parse request parameters, but the sanic framework itself does not provide it, so we need to implement it ourselves. In most cases, you may need to do this:

from sanic import Sanic
from sanic.response import json

app = Sanic("SanicDanticExample")


@app.route('/example')
async def path_param_examples(request):
    name = request.get("name")
    age = request.get("age")
    if not isinstance(name, str) or not isinstance(age, int):
        return json({"error": "parameter type error"})
    return json({"message": f"hello {name} are you {age} years old ?"})

This code looks simple, but when your parameters become more and more, you will find that you need to write a lot. This is a bad implementation because it is not maintainable. So we need a better way to implement it.

sanic-dantic can help you quickly implement parameter checking and parsing.

Installation

You can install sanic-dantic through pip:

pip install sanic-dantic

You can also install it through the source code:

pip install git+https://github.com/miss85246/sanic-dantic.git

Requirements

sanic-dantic depends on sanic and pydantic, their specific versions are:

sanic >= 20.3.0
pydantic >= 1.7.3

Simple Usage

First, define the parameter check model through pydantic, then define the view function, and check and parse the parameters through the parse_params decorator.

from sanic import Sanic
from sanic.response import json

from sanic_dantic import parse_params, BaseModel, Field


# define params check model by pydantic.
class Person(BaseModel):
    name: str = Field(description="name of person")
    age: int = Field(default=18, description="age of person")


app = Sanic("SanicDanticExample")


# view function and check and parse params by `parse_params` decorator.
@app.route('/example')
@parse_params(path=Person)
async def path_param_examples(request, params):
    # request.ctx and params are parsed parameters
    print("ctx:", request.ctx.params)
    print("params:", params)
    print("name:", params.name)
    print("age:", params.age)

    return json({"ctx": request.ctx.params, "params": params})


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

now you can test with curl:

curl -X GET "http://localhost:8000/example?name=Connor&age=18"

the response is:

{
  "ctx": {
    "name": "Connor",
    "age": 18
  },
  "params": {
    "name": "Connor",
    "age": 18
  }
}

and the console output is:

ctx: {'name': 'Connor', 'age': 18}
params: {'name': 'Connor', 'age': 18}
name: Connor
age: 18

more pydantic usage can see pydantic documentation

more sanic-dantic advanced usage can see sanic-dantic documentation

License

sanic-dantic is licensed under the GPL-3.0 License. If you want to use sanic-dantic for secondary development or commercial use, please follow the GPL-3.0 License.

nsb.api.sanic-dantic's People

Contributors

cakedan avatar miss85246 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.