2019-01-02 21:20:44 +01:00
|
|
|
from aiohttp import web
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.api import *
|
2019-01-02 21:20:44 +01:00
|
|
|
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.utils import json_dumps
|
2021-06-28 18:50:14 +02:00
|
|
|
import logging
|
2019-01-02 21:20:44 +01:00
|
|
|
|
2021-02-16 20:37:51 +01:00
|
|
|
|
|
|
|
class ConfigHttpEndpoints:
|
2019-01-02 21:20:44 +01:00
|
|
|
|
|
|
|
def __init__(self, cbpi):
|
2021-02-16 20:37:51 +01:00
|
|
|
self.cbpi = cbpi
|
2019-01-02 21:20:44 +01:00
|
|
|
self.controller = cbpi.config
|
|
|
|
self.cbpi.register(self, "/config")
|
|
|
|
|
2019-01-24 21:27:55 +01:00
|
|
|
@request_mapping(path="/{name}/", method="PUT", auth_required=False)
|
2021-01-17 22:49:18 +01:00
|
|
|
async def http_put(self, request) -> web.Response:
|
2019-01-24 21:27:55 +01:00
|
|
|
|
2019-01-02 21:20:44 +01:00
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Set config parameter
|
|
|
|
tags:
|
|
|
|
- Config
|
|
|
|
parameters:
|
|
|
|
- name: "name"
|
|
|
|
in: "path"
|
|
|
|
description: "Parameter name"
|
|
|
|
required: true
|
|
|
|
type: "string"
|
2021-01-17 22:49:18 +01:00
|
|
|
- name: body
|
|
|
|
in: body
|
|
|
|
description: "Parameter Value"
|
|
|
|
required: true
|
|
|
|
schema:
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
value:
|
|
|
|
type: string
|
2019-01-02 21:20:44 +01:00
|
|
|
responses:
|
|
|
|
"204":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
2019-07-27 21:08:19 +02:00
|
|
|
|
2019-01-02 21:20:44 +01:00
|
|
|
name = request.match_info['name']
|
|
|
|
data = await request.json()
|
|
|
|
await self.controller.set(name=name, value=data.get("value"))
|
|
|
|
return web.Response(status=204)
|
|
|
|
|
|
|
|
@request_mapping(path="/", auth_required=False)
|
|
|
|
async def http_get_all(self, request) -> web.Response:
|
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Get all config parameters
|
|
|
|
tags:
|
|
|
|
- Config
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
|
|
|
return web.json_response(self.controller.cache, dumps=json_dumps)
|
|
|
|
|
2021-06-28 18:50:14 +02:00
|
|
|
@request_mapping(path="/{name}/", method="POST", auth_required=False)
|
2019-01-02 21:20:44 +01:00
|
|
|
async def http_paramter(self, request) -> web.Response:
|
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Get all config parameters
|
|
|
|
tags:
|
|
|
|
- Config
|
|
|
|
parameters:
|
|
|
|
- name: "name"
|
|
|
|
in: "path"
|
|
|
|
description: "Parameter name"
|
|
|
|
required: true
|
|
|
|
type: "string"
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
|
|
|
name = request.match_info['name']
|
2021-06-28 18:50:14 +02:00
|
|
|
# if name not in self.cache:
|
|
|
|
# raise CBPiException("Parameter %s not found" % name)
|
2023-04-06 12:40:49 +02:00
|
|
|
# data = self.controller.get(name)
|
2021-06-28 18:50:14 +02:00
|
|
|
return web.json_response(self.controller.get(name), dumps=json_dumps)
|
2023-04-06 12:40:49 +02:00
|
|
|
|
|
|
|
@request_mapping(path="/remove/{name}/", method="PUT", auth_required=False)
|
|
|
|
async def http_remove(self, request) -> web.Response:
|
|
|
|
|
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Remove config parameter
|
|
|
|
tags:
|
|
|
|
- Config
|
|
|
|
parameters:
|
|
|
|
- name: "name"
|
|
|
|
in: "path"
|
|
|
|
description: "Parameter name"
|
|
|
|
required: true
|
|
|
|
type: "string"
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = request.match_info['name']
|
|
|
|
await self.controller.remove(name=name)
|
2023-04-08 14:15:08 +02:00
|
|
|
return web.Response(status=200)
|
|
|
|
|
|
|
|
@request_mapping(path="/obsolete", auth_required=False)
|
|
|
|
async def http_remove_obsolete(self, request) -> web.Response:
|
|
|
|
"""
|
|
|
|
---
|
2023-04-08 15:12:26 +02:00
|
|
|
description: Remove obsolete config parameters
|
2023-04-08 14:15:08 +02:00
|
|
|
tags:
|
|
|
|
- Config
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
|
|
|
return web.json_response(await self.controller.remove_obsolete(), dumps=json_dumps)
|