simple REST API to manage ingredients
This commit is contained in:
commit
5f61728536
10
.editorconfig
Normal file
10
.editorconfig
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
max_line_length = 79
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
venv
|
||||||
|
pizzatool.sqlite
|
||||||
|
*.swp
|
10
pizzatool/__init__.py
Normal file
10
pizzatool/__init__.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
from asyncio import run as asyncio_run
|
||||||
|
from hypercorn.config import Config
|
||||||
|
from hypercorn.asyncio import serve
|
||||||
|
from logging import basicConfig, DEBUG
|
||||||
|
from pizzatool.api import app
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
basicConfig(level=DEBUG, format="%(levelname)s %(name)s %(message)s")
|
||||||
|
asyncio_run(serve(app, Config()))
|
42
pizzatool/api.py
Normal file
42
pizzatool/api.py
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
from fastapi import Depends, FastAPI, HTTPException, Response, status
|
||||||
|
from logging import getLogger
|
||||||
|
from ormantic.exceptions import NoMatch
|
||||||
|
from pizzatool.model import Ingredient
|
||||||
|
|
||||||
|
|
||||||
|
logger = getLogger(__name__)
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get('/ingredients')
|
||||||
|
async def read_ingredients():
|
||||||
|
return await Ingredient.objects.all()
|
||||||
|
|
||||||
|
|
||||||
|
@app.post('/ingredients', status_code=status.HTTP_201_CREATED)
|
||||||
|
async def create_ingredient(name: str, response: Response):
|
||||||
|
ingredient = await Ingredient.objects.create(name=name)
|
||||||
|
response.headers['Location'] = 'ingredients/%d' % ingredient.id
|
||||||
|
return ingredient
|
||||||
|
|
||||||
|
|
||||||
|
async def get_ingredient(id: int):
|
||||||
|
try:
|
||||||
|
return await Ingredient.objects.get(id=id)
|
||||||
|
except NoMatch:
|
||||||
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get('/ingredients/{id}')
|
||||||
|
async def read_ingredient(ingredient: Ingredient = Depends(get_ingredient)):
|
||||||
|
return ingredient
|
||||||
|
|
||||||
|
|
||||||
|
@app.put('/ingredients/{id}', status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
async def update_ingredient(name: str, ingredient: Ingredient = Depends(get_ingredient)):
|
||||||
|
await ingredient.update(name=name)
|
||||||
|
|
||||||
|
|
||||||
|
@app.delete('/ingredients/{id}', status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
async def delete_ingredient(ingredient: Ingredient = Depends(get_ingredient)):
|
||||||
|
await ingredient.delete()
|
21
pizzatool/model.py
Normal file
21
pizzatool/model.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
from databases import Database
|
||||||
|
from ormantic import Integer, Model, Text
|
||||||
|
from sqlalchemy import create_engine, MetaData
|
||||||
|
|
||||||
|
|
||||||
|
database = Database('sqlite:///pizzatool.sqlite')
|
||||||
|
metadata = MetaData()
|
||||||
|
|
||||||
|
|
||||||
|
class Ingredient(Model):
|
||||||
|
id: Integer(primary_key=True) = None
|
||||||
|
name: Text()
|
||||||
|
|
||||||
|
class Mapping:
|
||||||
|
table_name = 'ingridients'
|
||||||
|
metadata = metadata
|
||||||
|
database = database
|
||||||
|
|
||||||
|
|
||||||
|
engine = create_engine(str(database.url), echo=True)
|
||||||
|
metadata.create_all(engine)
|
21
setup.py
Normal file
21
setup.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
from setuptools import find_packages, setup
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name='pizzatool',
|
||||||
|
version='0.0.0',
|
||||||
|
packages=find_packages(),
|
||||||
|
url='https://git.0x90.space/0x90/pizzatool',
|
||||||
|
author='0x90.space',
|
||||||
|
author_email='people@schleuder.0x90.space',
|
||||||
|
entry_points={
|
||||||
|
'console_scripts': [
|
||||||
|
'pizzatool=pizzatool:main',
|
||||||
|
]
|
||||||
|
},
|
||||||
|
install_requires=[
|
||||||
|
'aiosqlite',
|
||||||
|
'fastapi',
|
||||||
|
'hypercorn',
|
||||||
|
'ormantic @ https://github.com/MushroomMaula/ormantic/tarball/master#egg=ormantic-0.0.32',
|
||||||
|
]
|
||||||
|
)
|
Loading…
Reference in a new issue