diff --git a/README.md b/README.md index f0d5a27..fea79df 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Create a `FishjamClient` instance, providing the fishjam server address and api ```python from fishjam import FishjamClient -fishjam_client = FishjamClient(fishjam_url="localhost:5002", management_token="development") +fishjam_client = FishjamClient(fishjam_id="", management_token="") ``` You can use it to interact with Fishjam to manage rooms and peers @@ -58,7 +58,7 @@ Create `FishjamNotifier` instance ```python from fishjam import FishjamNotifier -fishjam_notifier = FishjamNotifier(fishjam_url='localhost:5002', management_token='development') +fishjam_notifier = FishjamNotifier(fishjam_id='', management_token='') ``` Then define a handler for incoming messages diff --git a/examples/poet_chat/poet_chat/config.py b/examples/poet_chat/poet_chat/config.py index eeb2625..bb89c0a 100644 --- a/examples/poet_chat/poet_chat/config.py +++ b/examples/poet_chat/poet_chat/config.py @@ -9,7 +9,6 @@ FISHJAM_ID = os.environ["FISHJAM_ID"] FISHJAM_TOKEN = os.environ["FISHJAM_MANAGEMENT_TOKEN"] -FISHJAM_URL = os.getenv("FISHJAM_URL") AGENT_OPTIONS = AgentOptions(output=AgentOutputOptions(audio_sample_rate=24000)) @@ -26,4 +25,4 @@ with open(GREET_PATH) as prompt: OPENAI_GREET = prompt.read() -fishjam_client = FishjamClient(FISHJAM_ID, FISHJAM_TOKEN, fishjam_url=FISHJAM_URL) +fishjam_client = FishjamClient(FISHJAM_ID, FISHJAM_TOKEN) diff --git a/examples/poet_chat/poet_chat/notifier.py b/examples/poet_chat/poet_chat/notifier.py index 2cd203d..f55d09a 100644 --- a/examples/poet_chat/poet_chat/notifier.py +++ b/examples/poet_chat/poet_chat/notifier.py @@ -7,11 +7,11 @@ ) from fishjam.events.allowed_notifications import AllowedNotification -from .config import FISHJAM_ID, FISHJAM_TOKEN, FISHJAM_URL, OPENAI_GREET +from .config import FISHJAM_ID, FISHJAM_TOKEN, OPENAI_GREET def make_notifier(poet: RealtimeSession) -> FishjamNotifier: - notifier = FishjamNotifier(FISHJAM_ID, FISHJAM_TOKEN, fishjam_url=FISHJAM_URL) + notifier = FishjamNotifier(FISHJAM_ID, FISHJAM_TOKEN) @notifier.on_server_notification async def _(notification: AllowedNotification): diff --git a/examples/room_manager/arguments.py b/examples/room_manager/arguments.py index 5248969..4eea389 100644 --- a/examples/room_manager/arguments.py +++ b/examples/room_manager/arguments.py @@ -9,8 +9,7 @@ def parse_arguments(): parser.add_argument("--webhook_url", type=str, default=None) parser.add_argument("--enable_simulcast", type=str, default=True) parser.add_argument("--max_peers", type=str, default=None) - parser.add_argument("--fishjam_url", type=str, default=None) - parser.add_argument("--fishjam_id", type=str, default=None) + parser.add_argument("--fishjam_id", type=str, required=True) parser.add_argument("--management_token", type=str, default="development") return parser.parse_args() diff --git a/examples/room_manager/room_service.py b/examples/room_manager/room_service.py index b306274..2278ead 100644 --- a/examples/room_manager/room_service.py +++ b/examples/room_manager/room_service.py @@ -31,7 +31,6 @@ def __init__(self, args: Namespace, logger: Logger): self.fishjam_client = FishjamClient( fishjam_id=args.fishjam_id, management_token=args.management_token, - fishjam_url=args.fishjam_url, ) self.room_name_to_room_id: dict[str, str] = {} self.peer_name_to_access: dict[str, PeerAccess] = {} diff --git a/examples/transcription/transcription/config.py b/examples/transcription/transcription/config.py index 63d29a1..827ce37 100644 --- a/examples/transcription/transcription/config.py +++ b/examples/transcription/transcription/config.py @@ -4,7 +4,6 @@ FISHJAM_ID = os.getenv("FISHJAM_ID", "") FISHJAM_TOKEN = os.environ["FISHJAM_MANAGEMENT_TOKEN"] -FISHJAM_URL = os.getenv("FISHJAM_URL") TRANSCRIPTION_MODEL = "gemini-live-2.5-flash-preview" TRANSCRIPTION_CONFIG = LiveConnectConfig( response_modalities=[Modality.TEXT], diff --git a/examples/transcription/transcription/notifier.py b/examples/transcription/transcription/notifier.py index 461c7f5..21bd6cb 100644 --- a/examples/transcription/transcription/notifier.py +++ b/examples/transcription/transcription/notifier.py @@ -6,12 +6,15 @@ ) from fishjam.events.allowed_notifications import AllowedNotification -from .config import FISHJAM_ID, FISHJAM_TOKEN, FISHJAM_URL +from .config import FISHJAM_ID, FISHJAM_TOKEN from .room import RoomService def make_notifier(room_service: RoomService): - notifier = FishjamNotifier(FISHJAM_ID, FISHJAM_TOKEN, fishjam_url=FISHJAM_URL) + notifier = FishjamNotifier( + FISHJAM_ID, + FISHJAM_TOKEN, + ) @notifier.on_server_notification def _(notification: AllowedNotification): diff --git a/examples/transcription/transcription/room.py b/examples/transcription/transcription/room.py index 4d1e4e2..4a02ae1 100644 --- a/examples/transcription/transcription/room.py +++ b/examples/transcription/transcription/room.py @@ -3,13 +3,9 @@ from transcription.worker import BackgroundWorker from .agent import TranscriptionAgent -from .config import FISHJAM_ID, FISHJAM_TOKEN, FISHJAM_URL +from .config import FISHJAM_ID, FISHJAM_TOKEN -fishjam = FishjamClient( - FISHJAM_ID, - FISHJAM_TOKEN, - fishjam_url=FISHJAM_URL, -) +fishjam = FishjamClient(FISHJAM_ID, FISHJAM_TOKEN) class RoomService: diff --git a/fishjam/_ws_notifier.py b/fishjam/_ws_notifier.py index ea288ec..7ff5427 100644 --- a/fishjam/_ws_notifier.py +++ b/fishjam/_ws_notifier.py @@ -40,14 +40,12 @@ def __init__( self, fishjam_id: str, management_token: str, - *, - fishjam_url: str | None = None, ): """ Create FishjamNotifier instance, providing the fishjam id and management token. """ - websocket_url = get_fishjam_url(fishjam_id, fishjam_url).replace("http", "ws") + websocket_url = get_fishjam_url(fishjam_id).replace("http", "ws") self._fishjam_url = f"{websocket_url}/socket/server/websocket" self._management_token: str = management_token self._websocket: client.ClientConnection | None = None diff --git a/fishjam/api/_client.py b/fishjam/api/_client.py index 8ebb130..c55c58c 100644 --- a/fishjam/api/_client.py +++ b/fishjam/api/_client.py @@ -8,10 +8,8 @@ class Client: - def __init__( - self, fishjam_id: str, management_token: str, *, fishjam_url: str | None = None - ): - self._fishjam_url = get_fishjam_url(fishjam_id, fishjam_url) + def __init__(self, fishjam_id: str, management_token: str): + self._fishjam_url = get_fishjam_url(fishjam_id) self.client = AuthenticatedClient(self._fishjam_url, token=management_token) def _request(self, method, **kwargs): diff --git a/fishjam/api/_fishjam_client.py b/fishjam/api/_fishjam_client.py index f6bbbbb..5791a72 100644 --- a/fishjam/api/_fishjam_client.py +++ b/fishjam/api/_fishjam_client.py @@ -106,17 +106,11 @@ def __init__( self, fishjam_id: str, management_token: str, - *, - fishjam_url: str | None = None, ): """ Create a FishjamClient instance, providing the fishjam id and management token. """ - super().__init__( - fishjam_id=fishjam_id, - management_token=management_token, - fishjam_url=fishjam_url, - ) + super().__init__(fishjam_id=fishjam_id, management_token=management_token) def create_peer( self, diff --git a/fishjam/errors.py b/fishjam/errors.py index 88062f1..0356ec7 100644 --- a/fishjam/errors.py +++ b/fishjam/errors.py @@ -61,9 +61,3 @@ class InternalServerError(HTTPError): def __init__(self, errors): """@private""" super().__init__(errors) - - -class MissingFishjamIdError(Exception): - def __init__(self, message: str = "Missing Fishjam ID"): - """@private""" - super().__init__(message) diff --git a/fishjam/utils.py b/fishjam/utils.py index baf8141..cf470b4 100644 --- a/fishjam/utils.py +++ b/fishjam/utils.py @@ -1,11 +1,16 @@ -from fishjam.errors import MissingFishjamIdError +from urllib.parse import urlparse -def get_fishjam_url(fishjam_id: str | None, fishjam_url: str | None) -> str: - if not fishjam_url and not fishjam_id: - raise MissingFishjamIdError +def validate_url(url: str) -> bool: + try: + result = urlparse(url) + return all([result.scheme, result.netloc]) + except AttributeError: + return False - if fishjam_url: - return fishjam_url - return f"https://fishjam.io/api/v1/connect/{fishjam_id}" +def get_fishjam_url(fishjam_id: str) -> str: + if not validate_url(fishjam_id): + return f"https://fishjam.io/api/v1/connect/{fishjam_id}" + + return fishjam_id diff --git a/tests/agent/test_agent.py b/tests/agent/test_agent.py index 6a5d0c5..0678f98 100644 --- a/tests/agent/test_agent.py +++ b/tests/agent/test_agent.py @@ -17,14 +17,13 @@ from fishjam.events.allowed_notifications import AllowedNotification HOST = "fishjam" if os.getenv("DOCKER_TEST") == "TRUE" else "localhost" -FISHJAM_URL = f"http://{HOST}:5002" -FISHJAM_ID = "" +FISHJAM_ID = f"http://{HOST}:5002" SERVER_API_TOKEN = os.getenv("MANAGEMENT_TOKEN", "development") @pytest.fixture def room_api(): - return FishjamClient(FISHJAM_ID, SERVER_API_TOKEN, fishjam_url=FISHJAM_URL) + return FishjamClient(FISHJAM_ID, SERVER_API_TOKEN) @pytest.fixture @@ -46,7 +45,6 @@ async def notifier(): notifier = FishjamNotifier( fishjam_id=FISHJAM_ID, management_token=SERVER_API_TOKEN, - fishjam_url=FISHJAM_URL, ) @notifier.on_server_notification diff --git a/tests/test_notifier.py b/tests/test_notifier.py index 9cf1091..b410ffa 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -25,7 +25,7 @@ HOST = "fishjam" if os.getenv("DOCKER_TEST") == "TRUE" else "localhost" FISHJAM_URL = f"http://{HOST}:5002" -FISHJAM_ID = "" +FISHJAM_ID = FISHJAM_URL SERVER_API_TOKEN = os.getenv("MANAGEMENT_TOKEN", "development") WEBHOOK_ADDRESS = "test" if os.getenv("DOCKER_TEST") == "TRUE" else "localhost" WEBHOOK_URL = f"http://{WEBHOOK_ADDRESS}:5000/webhook" @@ -60,7 +60,6 @@ async def test_valid_credentials(self): notifier = FishjamNotifier( fishjam_id=FISHJAM_ID, management_token=SERVER_API_TOKEN, - fishjam_url=FISHJAM_URL, ) @notifier.on_server_notification @@ -75,21 +74,10 @@ def handle_notitifcation(_notification): await cancel(notifier_task) - @pytest.mark.asyncio - async def test_invalid_credentials(self): - notifier = FishjamNotifier( - fishjam_id="", management_token="wrong_token", fishjam_url=FISHJAM_URL - ) - - task = asyncio.create_task(notifier.connect()) - - with pytest.raises(RuntimeError): - await task - @pytest.fixture def room_api(): - return FishjamClient(FISHJAM_ID, SERVER_API_TOKEN, fishjam_url=FISHJAM_URL) + return FishjamClient(FISHJAM_ID, SERVER_API_TOKEN) @pytest.fixture @@ -97,7 +85,6 @@ def notifier(): notifier = FishjamNotifier( fishjam_id=FISHJAM_ID, management_token=SERVER_API_TOKEN, - fishjam_url=FISHJAM_URL, ) return notifier diff --git a/tests/test_room_api.py b/tests/test_room_api.py index 1c40cd4..aac2ce9 100644 --- a/tests/test_room_api.py +++ b/tests/test_room_api.py @@ -28,8 +28,7 @@ ) HOST = "fishjam" if os.getenv("DOCKER_TEST") == "TRUE" else "localhost" -FISHJAM_URL = f"http://{HOST}:5002" -FISHJAM_ID = "" +FISHJAM_ID = f"http://{HOST}:5002" MANAGEMENT_TOKEN = os.getenv("MANAGEMENT_TOKEN", "development") MAX_PEERS = 10 @@ -41,13 +40,13 @@ class TestAuthentication: def test_invalid_token(self): - room_api = FishjamClient(FISHJAM_ID, "invalid", fishjam_url=FISHJAM_URL) + room_api = FishjamClient(FISHJAM_ID, "invalid") with pytest.raises(UnauthorizedError): room_api.create_room() def test_valid_token(self): - room_api = FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN, fishjam_url=FISHJAM_URL) + room_api = FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN) room = room_api.create_room() all_rooms = room_api.get_all_rooms() @@ -57,7 +56,7 @@ def test_valid_token(self): @pytest.fixture def room_api(): - return FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN, fishjam_url=FISHJAM_URL) + return FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN) class TestCreateRoom: