Compare commits

..

2 Commits

Author SHA1 Message Date
Vincent Maillol
b9519bb868 Add code coverage 2020-11-15 09:21:39 +01:00
Vincent Maillol
913f50298c Use docstring of handler in the OAS description 2020-11-14 20:33:55 +01:00
10 changed files with 60 additions and 11 deletions

3
.gitignore vendored
View File

@@ -1,6 +1,9 @@
.coverage
.idea/ .idea/
.pytest_cache .pytest_cache
__pycache__ __pycache__
aiohttp_pydantic.egg-info/ aiohttp_pydantic.egg-info/
build/ build/
coverage.xml
dist/ dist/

View File

@@ -2,11 +2,14 @@ language: python
python: python:
- '3.8' - '3.8'
script: script:
- pytest tests/ - pytest --cov-report=xml --cov=aiohttp_pydantic tests/
install: install:
- pip install -U setuptools wheel pip - pip install -U setuptools wheel pip
- pip install -r test_requirements.txt - pip install -r requirements/test.txt
- pip install -r requirements/ci.txt
- pip install . - pip install .
after_success:
- codecov
deploy: deploy:
provider: pypi provider: pypi
username: __token__ username: __token__
@@ -16,4 +19,4 @@ deploy:
on: on:
tags: true tags: true
branch: main branch: main
python: '3.8' python: '3.8'

View File

@@ -1,6 +1,16 @@
Aiohttp pydantic - Aiohttp View to validate and parse request Aiohttp pydantic - Aiohttp View to validate and parse request
============================================================= =============================================================
.. image:: https://travis-ci.org/Maillol/aiohttp-pydantic.svg?branch=main
:target: https://travis-ci.org/Maillol/aiohttp-pydantic
.. image:: https://img.shields.io/pypi/v/aiohttp-pydantic
:target: https://img.shields.io/pypi/v/aiohttp-pydantic
:alt: Latest PyPI package version
.. image:: https://codecov.io/gh/Maillol/aiohttp-pydantic/branch/add_code_coverage/graph/badge.svg
:target: https://codecov.io/gh/Maillol/aiohttp-pydantic
:alt: codecov.io status for master branch
Aiohttp pydantic is an `aiohttp view`_ to easily parse and validate request. Aiohttp pydantic is an `aiohttp view`_ to easily parse and validate request.
You define using the function annotations what your methods for handling HTTP verbs expects and Aiohttp pydantic parses the HTTP request You define using the function annotations what your methods for handling HTTP verbs expects and Aiohttp pydantic parses the HTTP request

View File

@@ -1,3 +1,4 @@
from inspect import getdoc
import typing import typing
from typing import List, Type from typing import List, Type
@@ -67,6 +68,9 @@ def _add_http_method_to_oas(
oas_operation: OperationObject = getattr(oas_path, http_method) oas_operation: OperationObject = getattr(oas_path, http_method)
handler = getattr(view, http_method) handler = getattr(view, http_method)
path_args, body_args, qs_args, header_args = _parse_func_signature(handler) path_args, body_args, qs_args, header_args = _parse_func_signature(handler)
description = getdoc(handler)
if description:
oas_operation.description = description
if body_args: if body_args:
oas_operation.request_body.content = { oas_operation.request_body.content = {

View File

@@ -9,8 +9,14 @@ from aiohttp.web_exceptions import HTTPMethodNotAllowed
from aiohttp.web_response import StreamResponse from aiohttp.web_response import StreamResponse
from pydantic import ValidationError from pydantic import ValidationError
from .injectors import (AbstractInjector, BodyGetter, HeadersGetter, from .injectors import (
MatchInfoGetter, QueryGetter, _parse_func_signature) AbstractInjector,
BodyGetter,
HeadersGetter,
MatchInfoGetter,
QueryGetter,
_parse_func_signature,
)
class PydanticView(AbstractView): class PydanticView(AbstractView):

7
requirements/ci.txt Normal file
View File

@@ -0,0 +1,7 @@
certifi==2020.11.8
chardet==3.0.4
codecov==2.1.10
coverage==5.3
idna==2.10
requests==2.25.0
urllib3==1.26.2

13
requirements/test.txt Normal file
View File

@@ -0,0 +1,13 @@
attrs==20.3.0
coverage==5.3
iniconfig==1.1.1
packaging==20.4
pluggy==0.13.1
py==1.9.0
pyparsing==2.4.7
pytest==6.1.2
pytest-aiohttp==0.3.0
pytest-cov==2.10.1
six==1.15.0
toml==0.10.2
typing-extensions==3.7.4.3

View File

@@ -35,8 +35,8 @@ install_requires =
swagger-ui-bundle swagger-ui-bundle
[options.extras_require] [options.extras_require]
test = pytest; pytest-aiohttp test = pytest==6.1.2; pytest-aiohttp==0.3.0; pytest-cov==2.10.1
ci = pytest==6.1.2; pytest-aiohttp==0.3.0; pytest-cov==2.10.1; codecov==2.1.10
[options.packages.find] [options.packages.find]
exclude = exclude =

View File

@@ -1,3 +0,0 @@
pytest==6.1.1
pytest-aiohttp==0.3.0
typing_extensions>=3.6.5

View File

@@ -15,9 +15,13 @@ class Pet(BaseModel):
class PetCollectionView(PydanticView): class PetCollectionView(PydanticView):
async def get(self) -> r200[List[Pet]]: async def get(self) -> r200[List[Pet]]:
"""
Get a list of pets
"""
return web.json_response() return web.json_response()
async def post(self, pet: Pet) -> r201[Pet]: async def post(self, pet: Pet) -> r201[Pet]:
"""Create a Pet"""
return web.json_response() return web.json_response()
@@ -52,6 +56,7 @@ async def test_generated_oas_should_have_pets_paths(generated_oas):
async def test_pets_route_should_have_get_method(generated_oas): async def test_pets_route_should_have_get_method(generated_oas):
assert generated_oas["paths"]["/pets"]["get"] == { assert generated_oas["paths"]["/pets"]["get"] == {
"description": "Get a list of pets",
"responses": { "responses": {
"200": { "200": {
"content": { "content": {
@@ -71,12 +76,13 @@ async def test_pets_route_should_have_get_method(generated_oas):
} }
} }
} }
} },
} }
async def test_pets_route_should_have_post_method(generated_oas): async def test_pets_route_should_have_post_method(generated_oas):
assert generated_oas["paths"]["/pets"]["post"] == { assert generated_oas["paths"]["/pets"]["post"] == {
"description": "Create a Pet",
"requestBody": { "requestBody": {
"content": { "content": {
"application/json": { "application/json": {