craftbeerpi4-pione/core/http_endpoints/http_curd_endpoints.py

52 lines
1.8 KiB
Python
Raw Normal View History

2018-11-01 19:50:04 +01:00
import logging
2019-01-02 21:20:44 +01:00
2018-11-01 19:50:04 +01:00
from aiohttp import web
2018-12-29 00:27:19 +01:00
from cbpi_api import *
2019-01-02 21:20:44 +01:00
2018-11-01 21:27:37 +01:00
from core.utils.utils import json_dumps
2018-11-01 19:50:04 +01:00
2019-01-02 21:20:44 +01:00
class HttpCrudEndpoints():
2018-11-16 20:35:59 +01:00
def __init__(self, cbpi):
2018-11-01 19:50:04 +01:00
self.logger = logging.getLogger(__name__)
2018-11-16 20:35:59 +01:00
self.cbpi = cbpi
2019-01-02 21:20:44 +01:00
2018-11-01 19:50:04 +01:00
2019-01-02 00:48:36 +01:00
@request_mapping(path="/types", auth_required=False)
async def get_types(self, request):
2019-01-02 21:20:44 +01:00
if self.controller.types is not None:
return web.json_response(data=self.controller.types, dumps=json_dumps)
2019-01-02 00:48:36 +01:00
else:
return web.Response(status=404, text="Types not supported by endpoint")
2018-11-16 20:35:59 +01:00
@request_mapping(path="/", auth_required=False)
2018-11-01 19:50:04 +01:00
async def http_get_all(self, request):
2019-01-02 21:20:44 +01:00
return web.json_response(await self.controller.get_all(force_db_update=True), dumps=json_dumps)
2018-11-01 19:50:04 +01:00
2018-11-16 20:35:59 +01:00
@request_mapping(path="/{id:\d+}", auth_required=False)
2018-11-01 19:50:04 +01:00
async def http_get_one(self, request):
id = int(request.match_info['id'])
2019-01-02 21:20:44 +01:00
return web.json_response(await self.controller.get_one(id), dumps=json_dumps)
2018-11-01 19:50:04 +01:00
2018-11-16 20:35:59 +01:00
@request_mapping(path="/", method="POST", auth_required=False)
async def http_add(self, request):
data = await request.json()
2019-01-02 21:20:44 +01:00
print(data)
obj = await self.controller.add(**data)
2018-11-16 20:35:59 +01:00
return web.json_response(obj, dumps=json_dumps)
@request_mapping(path="/{id}", method="PUT", auth_required=False)
async def http_update(self, request):
2018-11-01 19:50:04 +01:00
id = request.match_info['id']
2018-11-16 20:35:59 +01:00
data = await request.json()
2019-01-02 21:20:44 +01:00
obj = await self.controller.update(id, data)
2018-11-16 20:35:59 +01:00
return web.json_response(obj, dumps=json_dumps)
2018-11-01 19:50:04 +01:00
2018-11-16 20:35:59 +01:00
@request_mapping(path="/{id}", method="DELETE", auth_required=False)
async def http_delete_one(self, request):
id = request.match_info['id']
2019-01-02 21:20:44 +01:00
await self.controller.delete(int(id))
2018-12-13 21:45:33 +01:00
return web.Response(status=204)