Skip to main content

Sanic Pyndatic

CircleCI codecov GitHub Pipenv locked Python version Downloads license

Description

A library for parsing and validating http requests for sanic web-framework using pydantic library

Full documentation here

Requirements

python >= 3.7

How to install

pip install sanic-pydantic

Dependencies

pydantic

Example

from sanic_pydantic import webargs

from sanic import Sanic
from sanic.response import json
from pydantic import BaseModel

app = Sanic("new app")


class PathModel(BaseModel):
    id: int


class QueryModel(BaseModel):
    name: str


class BodyModel(BaseModel):
    age: int


class HeadersModel(BaseModel):
    api_key: str = Field(alias="x-api-key")


@app.route("/get/<id:int>", methods=["GET"])
@webargs(path=PathModel, headers=HeadersModel)
def example_get_endpoint_params(request, id, **kwargs):
    response = json({"id":id})
    return response

@app.route("/get-request", methods=["GET"])
@webargs(query=QueryModel)
def example_get_endpoint(request, **kwargs):
    print(kwargs)
    response = json(kwargs)
    return response


@app.route("/post-request", methods=["POST"])
@webargs(query=QueryModel, body=BodyModel)
def example_post_endpoint(request, **kwargs):
    print(kwargs)
    response = json(kwargs)
    return response


@app.route("/async-get-request", methods=["GET"])
@webargs(query=QueryModel)
async def async_example_get_endpoint(request, **kwargs):
    print(kwargs)
    response = json(kwargs)
    return response


@app.route("/async-post-request", methods=["POST"])
@webargs(query=QueryModel, body=BodyModel)
async def async_example_post_endpoint(request, **kwargs):
    print(kwargs)
    response = json(kwargs)
    return response

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

Metadata

Release files for sanic-pydantic 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-pydantic 1.3.1
File Size Uploaded
sanic-pydantic-1.3.1.tar.gz 4.1 kB Details

Release files / sanic-pydantic-1.3.1.tar.gz

Download URL sanic-pydantic-1.3.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ee6f8699faa805b735e4469dd7634111b4d34383ed7ce13cd984d4c9c12e0f9f
BLAKE2b-256 checksum
How to use checksums
8c5bbbfe08e70e702d4566ee462fced129e71e53a6708edbf3a214cb31c31f75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

1.3.1 This release

1 release file

1.3.0

1 release file

1.2.4

1 release file

1.2.3

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page