Skip to content

Commit

Permalink
feat: Check if url is a WS URL (#227)
Browse files Browse the repository at this point in the history
* feat: Check if url is an WSS URL

* feat: Check if url is an WSS URL

* feat: Check if url is an WSS URL

* feat: Check if url is an WSS URL

* feat: Check if url is an WSS URL
  • Loading branch information
juancarlospaco authored Dec 23, 2024
1 parent 093f84d commit f82597e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions realtime/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
T_ParamSpec,
T_Retval,
)
from ..utils import is_ws_url
from .channel import AsyncRealtimeChannel, RealtimeChannelOptions

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -60,6 +61,8 @@ def __init__(
:param max_retries: Maximum number of reconnection attempts. Defaults to 5.
:param initial_backoff: Initial backoff time (in seconds) for reconnection attempts. Defaults to 1.0.
"""
if not is_ws_url(url):
ValueError("url must be a valid WebSocket URL or HTTP URL string")
self.url = f"{re.sub(r'https://', 'wss://', re.sub(r'http://', 'ws://', url, flags=re.IGNORECASE), flags=re.IGNORECASE)}/websocket?apikey={token}"
self.http_endpoint = http_endpoint_url(url)
self.is_connected = False
Expand Down
5 changes: 5 additions & 0 deletions realtime/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from urllib.parse import urlparse


def is_ws_url(url: str) -> bool:
return urlparse(url).scheme in {"wss", "ws", "http", "https"}

0 comments on commit f82597e

Please sign in to comment.