mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-12-01 13:44:16 +00:00
lots of linting
This commit is contained in:
@@ -10,7 +10,7 @@ import uvicorn
|
||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||
from starlette.websockets import WebSocketState
|
||||
|
||||
from openbb_websockets.helpers import get_logger, parse_kwargs
|
||||
from openbb_websockets.helpers import get_logger, parse_kwargs, setup_database
|
||||
|
||||
connected_clients: set = set()
|
||||
|
||||
@@ -65,7 +65,10 @@ async def websocket_endpoint( # noqa: PLR0915
|
||||
|
||||
if (
|
||||
broadcast_server.auth_token is not None
|
||||
and auth_token != broadcast_server._decrypt_value(broadcast_server.auth_token)
|
||||
and auth_token
|
||||
!= broadcast_server._decrypt_value( # pylint: disable=protected-access
|
||||
broadcast_server.auth_token
|
||||
)
|
||||
):
|
||||
await websocket.accept()
|
||||
await websocket.send_text(
|
||||
@@ -92,10 +95,9 @@ async def websocket_endpoint( # noqa: PLR0915
|
||||
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
msg = f"Unexpected error: {e.__class__.__name__} -> {e}"
|
||||
broadcast_server.logger.error(msg)
|
||||
pass
|
||||
finally:
|
||||
if broadcast_server in connected_clients:
|
||||
connected_clients.remove(broadcast_server)
|
||||
@@ -106,7 +108,7 @@ async def websocket_endpoint( # noqa: PLR0915
|
||||
await stdin_task
|
||||
except asyncio.CancelledError:
|
||||
broadcast_server.logger.info("INFO: A listener task was cancelled.")
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
msg = f"Unexpected error while cancelling stream task: {e.__class__.__name__} -> {e}"
|
||||
broadcast_server.logger.error(msg)
|
||||
if websocket.client_state != WebSocketState.DISCONNECTED:
|
||||
@@ -117,7 +119,7 @@ async def websocket_endpoint( # noqa: PLR0915
|
||||
broadcast_server.logger.error(msg)
|
||||
|
||||
|
||||
class BroadcastServer:
|
||||
class BroadcastServer: # pylint: disable=too-many-instance-attributes
|
||||
"""Stream new results from a continuously written SQLite database.
|
||||
|
||||
Not intended to be used directly, it is initialized by the server app when it accepts a new connection.
|
||||
@@ -144,6 +146,7 @@ class BroadcastServer:
|
||||
self._iv = os.urandom(16)
|
||||
self.auth_token = self._encrypt_value(auth_token) if auth_token else None
|
||||
self.websocket = None
|
||||
self.kwargs = kwargs
|
||||
|
||||
def _encrypt_value(self, value: str) -> str:
|
||||
"""Encrypt the value for storage."""
|
||||
@@ -159,8 +162,11 @@ class BroadcastServer:
|
||||
|
||||
return decrypt_value(self._key, self._iv, value)
|
||||
|
||||
async def stream_results(self): # noqa: PLR0915
|
||||
async def stream_results( # noqa: PLR0915 # pylint: disable=too-many-branches
|
||||
self,
|
||||
):
|
||||
"""Continuously read the database and send new messages as JSON via WebSocket."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
import sqlite3 # noqa
|
||||
from openbb_core.app.model.abstract.error import OpenBBError
|
||||
|
||||
@@ -168,16 +174,15 @@ class BroadcastServer:
|
||||
last_id = 0
|
||||
|
||||
if not file_path.exists():
|
||||
self.logger.error(f"Results file not found: {file_path}")
|
||||
self.logger.error("Results file not found: %s", str(file_path))
|
||||
return
|
||||
else:
|
||||
conn = sqlite3.connect(self.results_file)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"SELECT MAX(id) FROM {self.table_name}") # noqa:S608
|
||||
last_id = cursor.fetchone()[0] or 0
|
||||
conn.close()
|
||||
conn = sqlite3.connect(self.results_file)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"SELECT MAX(id) FROM {self.table_name}") # noqa:S608
|
||||
last_id = cursor.fetchone()[0] or 0
|
||||
conn.close()
|
||||
|
||||
try:
|
||||
try: # pylint: disable=too-many-nested-blocks
|
||||
while True:
|
||||
try:
|
||||
if file_path.exists():
|
||||
@@ -192,7 +197,7 @@ class BroadcastServer:
|
||||
|
||||
if rows:
|
||||
for row in rows:
|
||||
index, message = row
|
||||
_, message = row
|
||||
await self.broadcast(json.dumps(json.loads(message)))
|
||||
last_id = max(row[0] for row in rows)
|
||||
else:
|
||||
@@ -209,17 +214,15 @@ class BroadcastServer:
|
||||
"Results file was removed by the parent process."
|
||||
)
|
||||
break
|
||||
else:
|
||||
raise OpenBBError(e) from e
|
||||
raise OpenBBError(e) from e
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
msg = f"Unexpected error: {e.__class__.__name__} -> {e}"
|
||||
self.logger.error(msg)
|
||||
finally:
|
||||
return
|
||||
return
|
||||
|
||||
async def broadcast(self, message: str):
|
||||
"""Broadcast a message to all connected connected clients."""
|
||||
@@ -229,14 +232,15 @@ class BroadcastServer:
|
||||
await client.websocket.send_text(message)
|
||||
except WebSocketDisconnect:
|
||||
disconnected_clients.add(client)
|
||||
except Exception as e:
|
||||
self.logger.error(f"Unexpected error: {e}")
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
msg = f"Unexpected error: {e.__class__.__name__} -> {e}"
|
||||
self.logger.error(msg)
|
||||
disconnected_clients.add(client)
|
||||
# Remove disconnected connected clients
|
||||
for client in disconnected_clients:
|
||||
connected_clients.remove(client)
|
||||
|
||||
def start_app(self, host: str = "127.0.0.1", port: int = 6666, **kwargs):
|
||||
def start_app(self, host: str = "127.0.0.1", port: int = 6666):
|
||||
"""Start the FastAPI app with Uvicorn."""
|
||||
uvicorn.run(
|
||||
self._app,
|
||||
@@ -251,7 +255,6 @@ def create_broadcast_server(
|
||||
table_name: str,
|
||||
sleep_time: float = 0.25,
|
||||
auth_token: Optional[str] = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""Create a new BroadcastServer instance."""
|
||||
return BroadcastServer(results_file, table_name, sleep_time, auth_token)
|
||||
@@ -288,5 +291,8 @@ if __name__ == "__main__":
|
||||
raise ValueError("Results file path is required for Broadcast server.")
|
||||
|
||||
if not Path(RESULTS_FILE).absolute().exists():
|
||||
raise FileNotFoundError(f"Results file not found: {RESULTS_FILE}")
|
||||
# pylint: disable=import-outside-toplevel
|
||||
from openbb_core.provider.utils.helpers import run_async
|
||||
|
||||
run_async(setup_database, RESULTS_FILE, TABLE_NAME)
|
||||
main()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""WebSocket Client module for interacting with a provider websocket in a non-blocking pattern."""
|
||||
|
||||
# pylint: disable=too-many-statements
|
||||
# pylint: disable=too-many-statements,protected-access
|
||||
# flake8: noqa: PLR0915
|
||||
|
||||
import logging
|
||||
@@ -10,7 +10,7 @@ if TYPE_CHECKING:
|
||||
from openbb_core.provider.abstract.data import Data
|
||||
|
||||
|
||||
class WebSocketClient:
|
||||
class WebSocketClient: # pylint: disable=too-many-instance-attributes
|
||||
"""Client for interacting with a websocket server in a non-blocking pattern.
|
||||
|
||||
Parameters
|
||||
@@ -79,7 +79,7 @@ class WebSocketClient:
|
||||
Send a message to the WebSocket process. Messages can be sent to "provider" or "broadcast" targets.
|
||||
"""
|
||||
|
||||
def __init__( # noqa: PLR0913
|
||||
def __init__( # noqa: PLR0913 # pylint: disable=too-many-arguments,too-many-positional-arguments,too-many-locals
|
||||
self,
|
||||
name: str,
|
||||
module: str,
|
||||
@@ -95,8 +95,7 @@ class WebSocketClient:
|
||||
) -> None:
|
||||
"""Initialize the WebSocketClient class."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
import asyncio # noqa
|
||||
import atexit
|
||||
import atexit # noqa
|
||||
import os
|
||||
import tempfile
|
||||
import threading
|
||||
@@ -105,6 +104,7 @@ class WebSocketClient:
|
||||
from pathlib import Path
|
||||
from openbb_core.app.model.abstract.error import OpenBBError
|
||||
from openbb_websockets.helpers import get_logger
|
||||
from openbb_websockets.helpers import encrypt_value
|
||||
|
||||
self.name = name
|
||||
self.module = module.replace(".py", "") # type: ignore
|
||||
@@ -115,13 +115,14 @@ class WebSocketClient:
|
||||
self._symbol = symbol
|
||||
self._key = os.urandom(32)
|
||||
self._iv = os.urandom(16)
|
||||
self._auth_token = self._encrypt_value(auth_token) if auth_token else None
|
||||
self._auth_token = (
|
||||
encrypt_value(self._key, self._iv, auth_token) if auth_token else None
|
||||
)
|
||||
# strings in kwargs are encrypted before storing in the class but unencrypted when passed to the provider module.
|
||||
if kwargs:
|
||||
for k, v in kwargs.items():
|
||||
if isinstance(v, str):
|
||||
encrypted_value = self._encrypt_value(v)
|
||||
kwargs[k] = encrypted_value
|
||||
kwargs[k] = encrypt_value(self._key, self._iv, v)
|
||||
else:
|
||||
kwargs[k] = v
|
||||
|
||||
@@ -144,7 +145,6 @@ class WebSocketClient:
|
||||
|
||||
if not results_file:
|
||||
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
||||
pass
|
||||
temp_file_path = temp_file.name
|
||||
self.results_path = Path(temp_file_path).absolute()
|
||||
self.results_file = temp_file_path
|
||||
@@ -162,20 +162,6 @@ class WebSocketClient:
|
||||
self.logger.error(msg)
|
||||
self._exception = OpenBBError(msg)
|
||||
|
||||
def _encrypt_value(self, value):
|
||||
"""Encrypt a value before storing."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
from openbb_websockets.helpers import encrypt_value
|
||||
|
||||
return encrypt_value(self._key, self._iv, value)
|
||||
|
||||
def _decrypt_value(self, encrypted_value):
|
||||
"""Decrypt the value for use."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
from openbb_websockets.helpers import decrypt_value
|
||||
|
||||
return decrypt_value(self._key, self._iv, encrypted_value)
|
||||
|
||||
def _atexit(self) -> None:
|
||||
"""Clean up the WebSocket client processes at exit."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
@@ -195,34 +181,10 @@ class WebSocketClient:
|
||||
def _setup_database(self) -> None:
|
||||
"""Set up the SQLite database and table."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
import asyncio # noqa
|
||||
import threading
|
||||
import time
|
||||
from openbb_core.provider.utils.helpers import run_async # noqa
|
||||
from openbb_websockets.helpers import setup_database
|
||||
|
||||
def run_in_new_loop():
|
||||
"""Run setup in new event loop."""
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
try:
|
||||
loop.run_until_complete(
|
||||
setup_database(self.results_path, self.table_name)
|
||||
)
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
def run_in_thread():
|
||||
"""Run setup in separate thread."""
|
||||
thread = threading.Thread(target=run_in_new_loop)
|
||||
thread.start()
|
||||
time.sleep(0.01)
|
||||
thread.join()
|
||||
|
||||
try:
|
||||
loop = asyncio.get_running_loop() # noqa
|
||||
run_in_thread()
|
||||
except RuntimeError:
|
||||
run_in_new_loop()
|
||||
run_async(setup_database, self.results_path, self.table_name)
|
||||
|
||||
def _log_provider_output(self, output_queue) -> None:
|
||||
"""Log output from the provider logger, handling exceptions, errors, and messages that are not data."""
|
||||
@@ -346,17 +308,17 @@ class WebSocketClient:
|
||||
except queue.Empty:
|
||||
continue
|
||||
|
||||
def connect(self) -> None:
|
||||
def connect(self) -> None: # pylint: disable=too-many-locals
|
||||
"""Connect to the provider WebSocket."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
import json # noqa
|
||||
import os
|
||||
import os # noqa
|
||||
import psutil
|
||||
import queue
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from openbb_core.app.model.abstract.error import OpenBBError
|
||||
from openbb_websockets.helpers import decrypt_value
|
||||
|
||||
if self.is_running:
|
||||
self.logger.info("Provider connection already running.")
|
||||
@@ -381,7 +343,9 @@ class WebSocketClient:
|
||||
if kwargs:
|
||||
for k, v in kwargs.items():
|
||||
if isinstance(v, str):
|
||||
unencrypted_value = self._decrypt_value(v)
|
||||
unencrypted_value = decrypt_value(
|
||||
self._key, self._iv, v # pylint: disable=protected-access
|
||||
)
|
||||
kwargs[k] = unencrypted_value
|
||||
else:
|
||||
kwargs[k] = v
|
||||
@@ -396,7 +360,7 @@ class WebSocketClient:
|
||||
if kwarg not in command:
|
||||
command.extend([kwarg])
|
||||
|
||||
self._process = subprocess.Popen( # noqa
|
||||
self._process = subprocess.Popen( # noqa # pylint: disable=consider-using-with
|
||||
command,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
@@ -430,9 +394,9 @@ class WebSocketClient:
|
||||
time.sleep(2)
|
||||
|
||||
if self._exception is not None:
|
||||
exc = getattr(self, "_exception", None)
|
||||
self._exception = None
|
||||
raise OpenBBError(exc)
|
||||
with self._exception as exc:
|
||||
self._exception = None
|
||||
raise OpenBBError(exc)
|
||||
|
||||
if not self.is_running:
|
||||
self.logger.error("The provider server failed to start.")
|
||||
@@ -542,7 +506,7 @@ class WebSocketClient:
|
||||
try:
|
||||
cursor = conn.execute(f"SELECT * FROM {self.table_name}") # noqa
|
||||
for row in cursor:
|
||||
index, message = row
|
||||
_, message = row
|
||||
if self.data_model:
|
||||
message = json.loads(message)
|
||||
if isinstance(message, (str, bytes)):
|
||||
@@ -554,7 +518,7 @@ class WebSocketClient:
|
||||
else:
|
||||
output.append(json.loads(json.loads(message)))
|
||||
except Exception as e:
|
||||
raise OpenBBError(f"Error retrieving results: {e}")
|
||||
raise OpenBBError(f"Error retrieving results: {e}") from e
|
||||
if output:
|
||||
return output
|
||||
|
||||
@@ -580,8 +544,9 @@ class WebSocketClient:
|
||||
self.table_name,
|
||||
self.results_file,
|
||||
)
|
||||
except Exception as e:
|
||||
self.logger.error("Error clearing results: %s", e)
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
msg = f"Error clearing results: {e.__class__.__name__}: {e}"
|
||||
self.logger.error(msg)
|
||||
|
||||
@property
|
||||
def module(self) -> list:
|
||||
@@ -624,7 +589,7 @@ class WebSocketClient:
|
||||
else None
|
||||
)
|
||||
|
||||
def start_broadcasting(
|
||||
def start_broadcasting( # pylint: disable=too-many-locals
|
||||
self,
|
||||
host: str = "127.0.0.1",
|
||||
port: int = 6666,
|
||||
@@ -639,14 +604,14 @@ class WebSocketClient:
|
||||
import psutil
|
||||
import queue
|
||||
from openbb_platform_api.utils.api import check_port
|
||||
from openbb_websockets.helpers import decrypt_value
|
||||
|
||||
if (
|
||||
self._broadcast_process is not None
|
||||
and self._broadcast_process.poll() is None
|
||||
):
|
||||
self.logger.info(
|
||||
f"WebSocket broadcast already running on: {self._broadcast_address}"
|
||||
)
|
||||
msg = f"WebSocket broadcast already running on: {self._broadcast_address}"
|
||||
self.logger.info(msg)
|
||||
return
|
||||
|
||||
open_port = check_port(host, port)
|
||||
@@ -663,20 +628,22 @@ class WebSocketClient:
|
||||
f"port={open_port}",
|
||||
f"results_file={self.results_file}",
|
||||
f"table_name={self.table_name}",
|
||||
f"auth_token={self._decrypt_value(self._auth_token) if self._auth_token else None}",
|
||||
f"auth_token={decrypt_value(self._key, self._iv, self._auth_token) if self._auth_token else None}",
|
||||
]
|
||||
if kwargs:
|
||||
for kwarg in kwargs:
|
||||
command.extend([f"{kwarg}={kwargs[kwarg]}"])
|
||||
for k, v in kwargs.items():
|
||||
command.extend([f"{k}={v}"])
|
||||
|
||||
self._broadcast_process = subprocess.Popen( # noqa
|
||||
command,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
stdin=subprocess.PIPE,
|
||||
env=os.environ,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
self._broadcast_process = (
|
||||
subprocess.Popen( # noqa # pylint: disable=consider-using-with
|
||||
command,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
stdin=subprocess.PIPE,
|
||||
env=os.environ,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
)
|
||||
)
|
||||
self._psutil_broadcast_process = psutil.Process(self._broadcast_process.pid)
|
||||
output_queue: queue.Queue = queue.Queue()
|
||||
@@ -786,7 +753,7 @@ def send_message(
|
||||
client._broadcast_process.stdin.flush()
|
||||
else:
|
||||
client.logger.error("Broadcast process is not running.")
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
msg = f"Error sending message to the {target} process: {e.__class__.__name__} -> {e}"
|
||||
client.logger.error(msg)
|
||||
|
||||
@@ -796,28 +763,22 @@ def read_message_queue(
|
||||
):
|
||||
"""Read messages from the queue and send them to the WebSocketConnection process."""
|
||||
while not message_queue.empty():
|
||||
try:
|
||||
message = message_queue.get(timeout=1)
|
||||
if message:
|
||||
try:
|
||||
if (
|
||||
target == "provider"
|
||||
and not client._stop_log_thread_event.is_set()
|
||||
):
|
||||
send_message(client, message, target="provider")
|
||||
elif (
|
||||
target == "broadcast"
|
||||
and not client._stop_broadcasting_event.is_set()
|
||||
):
|
||||
send_message(client, message, target="broadcast")
|
||||
except Exception as e:
|
||||
err = (
|
||||
f"Error while attempting to transmit from the outgoing message queue: {e.__class__.__name__} "
|
||||
f"-> {e} -> {message}"
|
||||
)
|
||||
client.logger.error(err)
|
||||
finally:
|
||||
break
|
||||
message = message_queue.get(timeout=1)
|
||||
if message:
|
||||
try:
|
||||
if target == "provider" and not client._stop_log_thread_event.is_set():
|
||||
send_message(client, message, target="provider")
|
||||
elif (
|
||||
target == "broadcast"
|
||||
and not client._stop_broadcasting_event.is_set()
|
||||
):
|
||||
send_message(client, message, target="broadcast")
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
err = (
|
||||
f"Error while attempting to transmit from the outgoing message queue: {e.__class__.__name__} "
|
||||
f"-> {e} -> {message}"
|
||||
)
|
||||
client.logger.error(err)
|
||||
|
||||
|
||||
def non_blocking_broadcast(client, output_queue, broadcast_message_queue) -> None:
|
||||
@@ -832,7 +793,7 @@ def non_blocking_broadcast(client, output_queue, broadcast_message_queue) -> Non
|
||||
break
|
||||
if output:
|
||||
output_queue.put(output.strip())
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
client.logger.error(
|
||||
f"Unexpected error in non_blocking_broadcast: {e.__class__.__name__} -> {e}"
|
||||
)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
"""WebSockets helpers."""
|
||||
|
||||
# pylint: disable=protected-access
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Any, Optional
|
||||
@@ -24,7 +26,6 @@ def clean_message(message: str) -> str:
|
||||
def get_logger(name, level=logging.INFO):
|
||||
"""Get a logger instance."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
logger = logging.getLogger(f"{name}-{uuid.uuid4()}")
|
||||
|
||||
@@ -10,11 +10,14 @@ class Listener:
|
||||
self.loop = None
|
||||
self.websocket = None
|
||||
self.current_task = None
|
||||
self.logger = None
|
||||
self.kwargs: dict = {}
|
||||
if kwargs:
|
||||
self.kwargs = kwargs
|
||||
|
||||
async def listen(self, url, **kwargs): # noqa: PLR0915
|
||||
async def listen( # noqa: PLR0915 # pylint: disable=too-many-branches,too-many-statements,too-many-locals
|
||||
self, url, **kwargs
|
||||
):
|
||||
"""Listen for WebSocket messages."""
|
||||
# pylint: disable=import-outside-toplevel
|
||||
import asyncio # noqa
|
||||
@@ -47,9 +50,8 @@ class Listener:
|
||||
async with websockets.connect(url, **kwargs) as websocket:
|
||||
self.websocket = websocket
|
||||
url = clean_message(url)
|
||||
self.logger.info(
|
||||
f"\nListening for messages from {clean_message(url)}"
|
||||
)
|
||||
msg = f"\nListening for messages from {clean_message(url)}"
|
||||
self.logger.info(msg)
|
||||
for handler in self.logger.handlers:
|
||||
handler.flush()
|
||||
async for message in websocket:
|
||||
@@ -71,30 +73,30 @@ class Listener:
|
||||
websockets.ConnectionClosedError,
|
||||
asyncio.IncompleteReadError,
|
||||
):
|
||||
self.logger.error(
|
||||
f"The process hosting {clean_message(url)} was terminated."
|
||||
)
|
||||
msg = f"The process hosting {clean_message(url)} was terminated."
|
||||
self.logger.error(msg)
|
||||
break
|
||||
except websockets.exceptions.InvalidURI as error:
|
||||
self.logger.error(f"Invalid URI -> {error}")
|
||||
msg = f"Invalid URI -> {error}"
|
||||
self.logger.error(msg)
|
||||
break
|
||||
except InvalidStatusCode as error:
|
||||
self.logger.error(f"Invalid status code -> {error}")
|
||||
msg = f"Invalid status code -> {error}"
|
||||
self.logger.error(msg)
|
||||
break
|
||||
except OSError as error:
|
||||
if "Multiple exceptions" in str(error):
|
||||
err = str(error).split("Multiple exceptions:")[1].strip()
|
||||
err = err.split("[")[-1].strip().replace("]", ":")
|
||||
self.logger.error(
|
||||
f"An error occurred while attempting to connect to: {clean_message(url)} -> {err}"
|
||||
)
|
||||
msg = f"An error occurred while attempting to connect to: {clean_message(url)} -> {err}"
|
||||
self.logger.error(msg)
|
||||
else:
|
||||
self.logger.error(
|
||||
f"An error occurred while attempting to connect to: {clean_message(url)} -> {error}"
|
||||
)
|
||||
msg = f"An error occurred while attempting to connect to: {clean_message(url)} -> {error}"
|
||||
self.logger.error(msg)
|
||||
break
|
||||
except Exception as error:
|
||||
self.logger.error(f"An unexpected error occurred: {error}")
|
||||
except Exception as error: # pylint: disable=broad-except
|
||||
msg = f"Unexpected error -> {error.__class__.__name__}: {error}"
|
||||
self.logger.error(msg)
|
||||
raise OpenBBError(error) from error
|
||||
finally:
|
||||
if self.websocket:
|
||||
@@ -154,7 +156,5 @@ def listen(url, **kwargs):
|
||||
try:
|
||||
listener = Listener(**kwargs)
|
||||
listener.run(url, **kwargs)
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
raise OpenBBError(e) from e
|
||||
finally:
|
||||
return
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Websockets Router."""
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
# pylint: disable=unused-argument,protected-access,unused-import
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
@@ -34,6 +34,17 @@ sys.stdout = StdOutSink()
|
||||
|
||||
@router.command(
|
||||
model="WebSocketConnection",
|
||||
examples=[
|
||||
APIEx(
|
||||
parameters={
|
||||
"name": "client1",
|
||||
"provider": "fmp",
|
||||
"asset_type": "crypto",
|
||||
"symbol": "btcusd,ethusd,solusd",
|
||||
"start_broadcast": True,
|
||||
}
|
||||
)
|
||||
],
|
||||
)
|
||||
async def create_connection(
|
||||
cc: CommandContext,
|
||||
|
||||
Reference in New Issue
Block a user