Giter Club home page Giter Club logo

sanic-pydantic's Introduction

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)

sanic-pydantic's People

Contributors

dependabot[bot] avatar frailleon avatar nf1s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

sanic-pydantic's Issues

An Exception when installed

Hello there,
I tried to install sanic-pydantic today, but there were some mistakes.
I run this command:

pip install sanic-pydantic -i  https://pypi.douban.com/simple

and it's raise error:

ERROR: Exception:
Traceback (most recent call last):
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 216, in _main
    status = self.run(options, args)
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 182, in wrapper
    return func(self, options, args)
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 324, in run
    requirement_set = resolver.resolve(
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/resolution/legacy/resolver.py", line 183, in resolve
    discovered_reqs.extend(self._resolve_one(requirement_set, req))
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/resolution/legacy/resolver.py", line 388, in _resolve_one
    abstract_dist = self._get_abstract_dist_for(req_to_install)
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/resolution/legacy/resolver.py", line 340, in _get_abstract_dist_for
    abstract_dist = self.preparer.prepare_linked_requirement(req)
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 482, in prepare_linked_requirement
    abstract_dist = _get_prepared_distribution(
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 91, in _get_prepared_distribution
    abstract_dist.prepare_distribution_metadata(finder, build_isolation)
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/distributions/sdist.py", line 33, in prepare_distribution_metadata
    self.req.load_pyproject_toml()
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/req/req_install.py", line 503, in load_pyproject_toml
    pyproject_toml_data = load_pyproject_toml(
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_internal/pyproject.py", line 75, in load_pyproject_toml
    pp_toml = toml.load(f)
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_vendor/toml/decoder.py", line 156, in load
    return loads(f.read(), _dict, decoder)
  File "/home/connor/.virtualenvs/test/lib/python3.8/site-packages/pip/_vendor/toml/decoder.py", line 362, in loads
    raise TomlDecodeError("Unterminated string found."
pip._vendor.toml.decoder.TomlDecodeError: Unterminated string found. Reached end of file. (line 23 column 1 char 434)

my python version is Python 3.8.5
my linux version is Linux version 4.18.0-147.5.1.el8_1.x86_64 ([email protected]) (gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)) #1 SMP Wed Feb 5 02:00:39 UTC 2020
can you give me some help? thanks

pip install error on python3.8

$ pip install sanic-pydantic
Collecting sanic-pydantic
Using cached sanic-pydantic-1.2.0.tar.gz (3.4 kB)
ERROR: Exception:
Traceback (most recent call last):
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 180, in _main
status = self.run(options, args)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/cli/req_command.py", line 205, in wrapper
return func(self, options, args)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 318, in run
requirement_set = resolver.resolve(
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 127, in resolve
result = self._result = resolver.resolve(
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 473, in resolve
state = resolution.resolve(requirements, max_rounds=max_rounds)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 341, in resolve
name, crit = self._merge_into_criterion(r, parent=None)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/resolvers.py", line 172, in _merge_into_criterion
if not criterion.candidates:
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_vendor/resolvelib/structs.py", line 139, in bool
return bool(self._sequence)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 143, in bool
return any(self)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 129, in
return (c for c in iterator if id(c) not in self._incompatible_ids)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 33, in _iter_built
candidate = func()
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/factory.py", line 200, in _make_candidate_from_link
self._link_candidate_cache[link] = LinkCandidate(
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 306, in init
super().init(
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 151, in init
self.dist = self._prepare()
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 234, in _prepare
dist = self._prepare_distribution()
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 317, in _prepare_distribution
return self._factory.preparer.prepare_linked_requirement(
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 508, in prepare_linked_requirement
return self._prepare_linked_requirement(req, parallel_builds)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 570, in _prepare_linked_requirement
dist = _get_prepared_distribution(
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 60, in _get_prepared_distribution
abstract_dist.prepare_distribution_metadata(finder, build_isolation)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/distributions/sdist.py", line 29, in prepare_distribution_metadata
self.req.load_pyproject_toml()
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/req/req_install.py", line 493, in load_pyproject_toml
pyproject_toml_data = load_pyproject_toml(
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_internal/pyproject.py", line 62, in load_pyproject_toml
pp_toml = toml.load(f)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_vendor/toml/decoder.py", line 156, in load
return loads(f.read(), _dict, decoder)
File "/Users/jenuce/my_cms/venv/lib/python3.8/site-packages/pip/_vendor/toml/decoder.py", line 362, in loads
raise TomlDecodeError("Unterminated string found."
pip._vendor.toml.decoder.TomlDecodeError: Unterminated string found. Reached end of file. (line 23 column 1 char 434)

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.