commit 5f61728536517989277381122bc61f74b07edabe Author: Thomas L Date: Thu Jun 18 04:55:56 2020 +0200 simple REST API to manage ingredients diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9065e65 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d9a83e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv +pizzatool.sqlite +*.swp diff --git a/pizzatool/__init__.py b/pizzatool/__init__.py new file mode 100644 index 0000000..ce2c4ec --- /dev/null +++ b/pizzatool/__init__.py @@ -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())) diff --git a/pizzatool/api.py b/pizzatool/api.py new file mode 100644 index 0000000..9aa6b2f --- /dev/null +++ b/pizzatool/api.py @@ -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() diff --git a/pizzatool/model.py b/pizzatool/model.py new file mode 100644 index 0000000..225d839 --- /dev/null +++ b/pizzatool/model.py @@ -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) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..2ef6401 --- /dev/null +++ b/setup.py @@ -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', + ] +)