Skip to content

Commit

Permalink
remoteadmin: try to add push
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin on chickenita committed Sep 17, 2024
1 parent 1e88bdd commit 8c2d54e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions pypeman/plugins/remoteadmin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
])
32 changes: 32 additions & 0 deletions pypeman/plugins/remoteadmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from jsonrpcserver.response import SuccessResponse

from pypeman import channels
from pypeman.message import Message

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

0 comments on commit 8c2d54e

Please sign in to comment.