craftbeerpi4-pione/cbpi/http_endpoints/http_plugin.py

107 lines
3.2 KiB
Python
Raw Normal View History

2019-07-31 07:58:54 +02:00
from aiohttp import web
2019-08-05 20:51:20 +02:00
from cbpi.api import request_mapping
from cbpi.utils import json_dumps
2021-10-16 14:22:04 +02:00
import logging
2019-07-31 07:58:54 +02:00
class PluginHttpEndpoints:
def __init__(self,cbpi):
self.cbpi = cbpi
self.cbpi.register(self, url_prefix="/plugin")
@request_mapping(path="/install/", method="POST", auth_required=False, json_schema={"package_name": str})
async def install(self, request):
"""
---
description: Install Plugin
tags:
- Plugin
parameters:
- in: body
name: body
description: Install a plugin
required: true
schema:
type: object
properties:
package_name:
type: string
produces:
- application/json
responses:
"204":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
data = await request.json()
return web.Response(status=204) if await self.cbpi.plugin.install(data["package_name"]) is True else web.Response(status=500)
@request_mapping(path="/uninstall", method="POST", auth_required=False, json_schema={"package_name": str})
async def uninstall(self, request):
"""
---
description: Uninstall Plugin
tags:
- Plugin
parameters:
- in: body
name: body
description: Uninstall a plugin
required: true
schema:
type: object
properties:
package_name:
type: string
produces:
- application/json
responses:
"204":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
data = await request.json()
return web.Response(status=204) if await self.cbpi.plugin.uninstall(data["package_name"]) is True else web.Response(status=500)
@request_mapping(path="/list", method="GET", auth_required=False)
async def list(self, request):
"""
---
description: Get a list of avialable plugins
tags:
- Plugin
produces:
- application/json
responses:
"200":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
2021-10-16 14:22:04 +02:00
plugin_list = await self.cbpi.plugin.load_plugin_list()
return web.json_response(plugin_list, dumps=json_dumps)
@request_mapping(path="/names", method="GET", auth_required=False)
2023-04-02 18:27:15 +02:00
async def names(self, request):
"""
---
description: Get a list of avialable plugin names
tags:
- Plugin
produces:
- application/json
responses:
"200":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
plugin_names = await self.cbpi.plugin.load_plugin_names()
return web.json_response(plugin_names, dumps=json_dumps)