From 8c2d54e9877a4912363ef5af9b206c890ea54089 Mon Sep 17 00:00:00 2001 From: quentin on chickenita Date: Tue, 17 Sep 2024 15:56:34 +0200 Subject: [PATCH] remoteadmin: try to add push --- pypeman/plugins/remoteadmin/urls.py | 1 + pypeman/plugins/remoteadmin/views.py | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/pypeman/plugins/remoteadmin/urls.py b/pypeman/plugins/remoteadmin/urls.py index 436cfaa..fb63b00 100644 --- a/pypeman/plugins/remoteadmin/urls.py +++ b/pypeman/plugins/remoteadmin/urls.py @@ -20,6 +20,7 @@ def init_urls(app, prefix=""): web.get(prefix + '/channels/{channelname}/messages/{message_id}/replay', views.replay_msg), web.get(prefix + '/channels/{channelname}/messages/{message_id}/view', views.view_msg), web.get(prefix + '/channels/{channelname}/messages/{message_id}/preview', views.preview_msg), + web.get(prefix + '/channels/{channelname}/messages/push', views.push_msg), # WEBSOCKETS : web.get(prefix + '/', views.backport_old_client), ]) diff --git a/pypeman/plugins/remoteadmin/views.py b/pypeman/plugins/remoteadmin/views.py index 52dd831..b69277c 100644 --- a/pypeman/plugins/remoteadmin/views.py +++ b/pypeman/plugins/remoteadmin/views.py @@ -5,6 +5,7 @@ from jsonrpcserver.response import SuccessResponse from pypeman import channels +from pypeman.message import Message logger = logging.getLogger(__name__) @@ -159,6 +160,34 @@ async def replay_msg(request, ws=None): return web.json_response(result) +async def push_msg(request, ws=None): + """ + Push a new message. + + :params channel: The channel name. + + :queryparams: + encode_payload (Bool, default=False): Force pickling and encoding the payload or not + """ + args = request.rel_url.query + channelname = request.match_info['channelname'] + post_data = await request.post() + encode_payload = args.get("encode_payload", False) + chan = get_channel(channelname) + try: + msg = Message(payload=post_data) + msg_res = await chan.handle(msg) + result = msg_res.to_dict(encode_payload=encode_payload) + except Exception as exc: + logger.exception(f"Cannot play msg : {post_data}") + result = {'error': str(exc)} + + if ws is not None: + await ws.send_jsonrpcresp(result) + return ws + return web.json_response(result) + + async def view_msg(request, ws=None): """ Permit to get the content of a message @@ -296,5 +325,8 @@ async def backport_old_client(request): await start_channel(request=request, ws=ws) elif cmd_method == "stop_channel": await stop_channel(request=request, ws=ws) + elif cmd_method == "push_msg": + msg = params[1] + await push_msg(request=request, ws=ws) else: await ws.send_str(f"{cmd_method} is not a valid method")