From 4f7bab7de61d136f08f485c131227bf10d751227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Baamonde=20Lozano?= Date: Sun, 27 Feb 2022 16:02:36 +0100 Subject: [PATCH] InvocationResult, send test (#78) --- README.md | 2 ++ setup.py | 2 +- signalrcore/hub/base_hub_connection.py | 18 ++++++++++++++++-- test/send_test.py | 11 ++++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index aec6d7e..6f17874 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,7 @@ username, message - parameters of signalrmethod ```python hub_connection.send("SendMessage", [username, message]) ``` + ## Sending messages with callback SendMessage - signalr method username, message - parameters of signalrmethod @@ -238,6 +239,7 @@ username, message - parameters of signalrmethod if not send_callback_received.acquire(timeout=1): raise ValueError("CALLBACK NOT RECEIVED") ``` + ## Requesting streaming (Server to client) ```python hub_connection.stream( diff --git a/setup.py b/setup.py index 626cb99..eda8795 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="signalrcore", - version="0.9.2", + version="0.9.3", author="mandrewcito", author_email="anbaalo@gmail.com", description="A Python SignalR Core client(json and messagepack), with invocation auth and two way streaming. Compatible with azure / serverless functions. Also with automatic reconnect and manually reconnect.", diff --git a/signalrcore/hub/base_hub_connection.py b/signalrcore/hub/base_hub_connection.py index 6ba2097..f01fb6e 100644 --- a/signalrcore/hub/base_hub_connection.py +++ b/signalrcore/hub/base_hub_connection.py @@ -1,3 +1,4 @@ +from operator import inv import websocket import threading import requests @@ -19,6 +20,11 @@ from ..subject import Subject from ..messages.invocation_message import InvocationMessage +class InvocationResult(object): + def __init__(self, invocation_id) -> None: + self.invocation_id = invocation_id + self.message = None + class BaseHubConnection(object): def __init__( self, @@ -90,7 +96,7 @@ def on(self, event, callback_function: Callable): self.logger.debug("Handler registered started {0}".format(event)) self.handlers.append((event, callback_function)) - def send(self, method, arguments, on_invocation=None, invocation_id=str(uuid.uuid4())): + def send(self, method, arguments, on_invocation=None, invocation_id=str(uuid.uuid4())) -> InvocationResult: """Sends a message Args: @@ -113,6 +119,8 @@ def send(self, method, arguments, on_invocation=None, invocation_id=str(uuid.uui if type(arguments) is not list and type(arguments) is not Subject: raise TypeError("Arguments of a message must be a list or subject") + result = InvocationResult(invocation_id) + if type(arguments) is list: message = InvocationMessage( invocation_id, @@ -127,11 +135,17 @@ def send(self, method, arguments, on_invocation=None, invocation_id=str(uuid.uui on_invocation)) self.transport.send(message) - + result.message = message + if type(arguments) is Subject: arguments.connection = self arguments.target = method arguments.start() + result.invocation_id = arguments.invocation_id + result.message = arguments + + + return result def on_message(self, messages): diff --git a/test/send_test.py b/test/send_test.py index 63d9e7b..08cdf99 100644 --- a/test/send_test.py +++ b/test/send_test.py @@ -1,4 +1,5 @@ import os +import re import unittest import logging import time @@ -103,10 +104,18 @@ def test_send_with_callback(self): self.received = False _lock = threading.Lock() _lock.acquire() + uid = str(uuid.uuid4()) + + def release(m): + self.assertTrue(m.invocation_id, uid) + _lock.release() + self.connection.send( "SendMessage", [self.username, self.message], - lambda m: _lock.release()) + release, + invocation_id=uid) + self.assertTrue(_lock.acquire(timeout=10)) del _lock