Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://nf1s.github.io/sanic-pydantic/
A library for parsing and validating http requests for sanic web-framework using pydantic library
https://nf1s.github.io/sanic-pydantic/
json json-parser pydantic sanic sanic-framework web webargs
Last synced: about 1 month ago
JSON representation
A library for parsing and validating http requests for sanic web-framework using pydantic library
- Host: GitHub
- URL: https://nf1s.github.io/sanic-pydantic/
- Owner: nf1s
- License: mit
- Archived: true
- Created: 2020-04-03T08:24:55.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-02-11T01:45:39.000Z (almost 2 years ago)
- Last Synced: 2024-10-02T23:14:42.281Z (2 months ago)
- Topics: json, json-parser, pydantic, sanic, sanic-framework, web, webargs
- Language: Python
- Homepage: https://ahmednafies.github.io/sanic-pydantic/
- Size: 1.8 MB
- Stars: 7
- Watchers: 0
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-sanic - Sanic-Pydantic - framework using pydantic library. (Extensions / Utils)
README
# Sanic Pyndatic
[![CircleCI](https://circleci.com/gh/nf1s/sanic-pydantic.svg?style=shield)](https://circleci.com/gh/nf1s/sanic-pydantic) [![codecov](https://codecov.io/gh/nf1s/sanic-pydantic/branch/master/graph/badge.svg)](https://codecov.io/gh/nf1s/sanic-pydantic) ![GitHub Pipenv locked Python version](https://img.shields.io/github/pipenv/locked/python-version/nf1s/sanic-pydantic) [![Downloads](https://pepy.tech/badge/sanic-pydantic)](https://pepy.tech/project/sanic-pydantic) ![license](https://img.shields.io/badge/license-MIT-green)
## Description
A library for parsing and validating http requests for sanic web-framework using pydantic libraryFull documentation [here](https://nf1s.github.io/sanic-pydantic/)
## Requirements
python >= 3.7
## How to install
```bash
pip install sanic-pydantic
```## Dependencies
pydantic
## Example
```python
from sanic_pydantic import webargs
from sanic import Sanic
from sanic.response import json
from pydantic import BaseModelapp = Sanic("new app")
class PathModel(BaseModel):
id: intclass QueryModel(BaseModel):
name: strclass BodyModel(BaseModel):
age: intclass HeadersModel(BaseModel):
api_key: str = Field(alias="x-api-key")@app.route("/get/", 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 responseif __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
```