From 7c780e66505f93d31459adc93cad3e299626f5b1 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 4 Nov 2023 16:22:27 +0100 Subject: [PATCH] Refactored `utils.bus.send_request`. It should return the response output and raise an error if the response has errors. --- .../backend/http/app/routes/plugins/camera/pi.py | 15 ++++++++------- .../backend/http/app/streaming/plugins/sound.py | 6 +----- platypush/backend/http/app/utils/bus.py | 7 ++++++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/platypush/backend/http/app/routes/plugins/camera/pi.py b/platypush/backend/http/app/routes/plugins/camera/pi.py index 0e3c2234d..539ed1ef9 100644 --- a/platypush/backend/http/app/routes/plugins/camera/pi.py +++ b/platypush/backend/http/app/routes/plugins/camera/pi.py @@ -29,8 +29,9 @@ def video_feed(): frame = output.frame if frame and len(frame): - yield (b'--frame\r\n' - b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') + yield ( + b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n' + ) @camera_pi.route('/camera/pi/frame', methods=['GET']) @@ -38,18 +39,18 @@ def video_feed(): def get_frame_img(): filename = os.path.join(tempfile.gettempdir(), 'camera_pi.jpg') response = send_request('camera.pi.take_picture', image_file=filename) - frame_file = (response.output or {}).get('image_file') + frame_file = (response or {}).get('image_file') assert frame_file is not None - return send_from_directory(os.path.dirname(frame_file), - os.path.basename(frame_file)) + return send_from_directory( + os.path.dirname(frame_file), os.path.basename(frame_file) + ) @camera_pi.route('/camera/pi/stream', methods=['GET']) @authenticate() def get_stream_feed(): - return Response(video_feed(), - mimetype='multipart/x-mixed-replace; boundary=frame') + return Response(video_feed(), mimetype='multipart/x-mixed-replace; boundary=frame') # vim:sw=4:ts=4:et: diff --git a/platypush/backend/http/app/streaming/plugins/sound.py b/platypush/backend/http/app/streaming/plugins/sound.py index 4101da841..14f94a49e 100644 --- a/platypush/backend/http/app/streaming/plugins/sound.py +++ b/platypush/backend/http/app/streaming/plugins/sound.py @@ -29,16 +29,12 @@ class SoundRoute(StreamingRoute): @contextmanager def _audio_stream(self, **kwargs) -> Generator[None, None, None]: - response = send_request( + send_request( 'sound.record', dtype='int16', **kwargs, ) - assert response and not response.is_error(), ( - 'Streaming error: ' + str(response.errors) if response else '(unknown)' - ) - yield send_request('sound.stop_recording') diff --git a/platypush/backend/http/app/utils/bus.py b/platypush/backend/http/app/utils/bus.py index 72f85658f..168e44fb8 100644 --- a/platypush/backend/http/app/utils/bus.py +++ b/platypush/backend/http/app/utils/bus.py @@ -64,4 +64,9 @@ def send_request(action, wait_for_response=True, **kwargs): if kwargs: msg['args'] = kwargs - return send_message(msg, wait_for_response=wait_for_response) + rs = send_message(msg, wait_for_response=wait_for_response) + assert rs, 'Got an empty response from the server' + if rs: + assert not rs.errors, '\n'.join(rs.errors) + + return rs.output