Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pslacerda/apistar_pydantic
Better parameters for APIStar (and pydantic support)
https://github.com/pslacerda/apistar_pydantic
Last synced: 2 months ago
JSON representation
Better parameters for APIStar (and pydantic support)
- Host: GitHub
- URL: https://github.com/pslacerda/apistar_pydantic
- Owner: pslacerda
- Created: 2018-01-23T18:22:00.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-21T21:51:40.000Z (over 6 years ago)
- Last Synced: 2024-10-29T00:48:53.504Z (3 months ago)
- Language: Python
- Homepage:
- Size: 22.5 KB
- Stars: 19
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
- starred-awesome - apistar_pydantic - Better parameters for APIStar (and pydantic support) (Python)
README
apistar-pydantic
################Better handler parameters for `APIStar `_
(and `pydantic `_ support).General usage is working as expected but documentation and OpenAPI schema aren't done yet.
.. code-block:: sh
$ pip install apistar-pydantic
Usage
=====.. code-block:: python
from apistar import App
from pydantic import BaseModel
from apistar_pydantic import (
QueryParam, PathParam, DictBodyData, DictQueryData,
PydanticBodyData as BodyData,
PydanticQueryData as QueryData,
Route, components,
)#
# Declare models
#class City(BaseModel):
"""City info"""
name: str
population: intclass Computer(BaseModel):
"""Computer info"""
model: str
price: floatclass ComputerCity(City, Computer):
"""A computer in a city"""#
# Create views
#def resource_complete(param1: QueryParam[str],
param2: QueryParam[int],
param3: DictBodyData[dict],
param4: DictQueryData[dict]):
return repr(locals())def resource_query(city: QueryData[City]):
return "%s has %d citizens." % (city.name, city.population)def resource_body(computer: BodyData[Computer]):
return "%s costs R$ %.2f" % (computer.model, computer.price)def resource_mixed(city: QueryData[City],
computer: BodyData[Computer]):
return ComputerCity(**city.dict(), **computer.dict())#
# Start the app
#app = App(
routes=[
Route('/resource', 'GET', resource_query),
Route('/resource_query', 'GET', resource_query),
Route('/resource_body', 'POST', resource_body),
Route('/resource_mixed', 'POST', resource_mixed),
],
components=[
*components
]
)if __name__ == '__main__':
app.serve('127.0.0.1', 3000, debug=True)