Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Check if url is a WS URL #227

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions realtime/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
T_ParamSpec,
T_Retval,
)
from ..utils import is_ws_url
from .channel import AsyncRealtimeChannel, RealtimeChannelOptions

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -57,6 +58,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"}
Loading