craftbeerpi4-pione/core/http_endpoints/http_api.py

134 lines
4.6 KiB
Python
Raw Normal View History

2018-11-01 19:50:04 +01:00
import logging
2018-11-04 00:47:26 +01:00
2018-11-01 19:50:04 +01:00
from aiohttp import web
from core.api.decorator import request_mapping
2018-11-01 21:27:37 +01:00
from core.utils.utils import json_dumps
2018-11-01 19:50:04 +01:00
2018-11-04 00:47:26 +01:00
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
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):
2018-11-16 20:35:59 +01:00
"""
2018-11-18 15:40:10 +01:00
---
description: This end-point allow to test that service is up.
tags:
- REST API
produces:
- application/json
parameters:
- name: "id"
in: "path"
description: "ID of object to return"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Met
"""
2018-11-01 19:50:04 +01:00
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):
2018-11-16 20:35:59 +01:00
"""
---
description: This end-point allow to test that service is up.
tags:
- REST API
produces:
- application/json
parameters:
- name: "id"
in: "path"
description: "ID of object to return"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
2018-11-01 19:50:04 +01:00
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):
"""
---
description: This end-point allow to test that service is up.
tags:
- REST API
produces:
- application/json
responses:
"200":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
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):
"""
---
description: This end-point allow to test that service is up.
tags:
- REST API
produces:
- application/json
parameters:
- in: body
name: body
description: Created user object
required: false
schema:
type: object
properties:
id:
type: integer
format: int64
responses:
"200":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
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):
"""
---
description: This end-point allow to test that service is up.
tags:
- REST API
produces:
- text/plain
responses:
"200":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
id = request.match_info['id']
await self.delete(id)
return web.Response(str=204)