craftbeerpi4-pione/core/http_endpoints/http_api.py

46 lines
1.7 KiB
Python
Raw Normal View History

2018-11-01 19:50:04 +01:00
import logging
from aiohttp import web
2019-01-02 00:48:36 +01:00
from aiohttp_swagger import swagger_path
2018-12-29 00:27:19 +01:00
from cbpi_api import *
2018-11-01 21:27:37 +01:00
from core.utils.utils import json_dumps
2018-11-01 19:50:04 +01:00
class HttpAPI():
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
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):
if self.types is not None:
return web.json_response(data=self.types, dumps=json_dumps)
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):
return web.json_response(await self.get_all(force_db_update=True), dumps=json_dumps)
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'])
return web.json_response(await self.get_one(id), dumps=json_dumps)
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()
obj = await self.add(**data)
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()
obj = await self.update(id, data)
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 00:48:36 +01:00
await self.delete(int(id))
2018-12-13 21:45:33 +01:00
return web.Response(status=204)