diff --git a/assemblyai/streaming/v3/_base.py b/assemblyai/streaming/v3/_base.py index b0f71d5..579da63 100644 --- a/assemblyai/streaming/v3/_base.py +++ b/assemblyai/streaming/v3/_base.py @@ -174,6 +174,17 @@ def _build_uri(host: str, params: RealTimeParameters) -> str: return f"wss://{host}/v3/ws?{params_encoded}" +def _build_http_base_url(api_host: str) -> str: + """Derive an HTTP(S) base URL from ``api_host`` (which may include a ws:// or wss:// scheme).""" + if api_host.startswith("wss://"): + return "https://" + api_host[len("wss://") :] + if api_host.startswith("ws://"): + return "http://" + api_host[len("ws://") :] + if api_host.startswith(("https://", "http://")): + return api_host + return "https://" + api_host + + def _build_headers(options: RealTimeTranscriberOptions) -> Dict[str, Optional[str]]: # Matches the pre-refactor sync behavior: ``Authorization`` is left as the # raw value (may be ``None`` when neither ``token`` nor ``api_key`` is set, diff --git a/assemblyai/streaming/v3/async_client.py b/assemblyai/streaming/v3/async_client.py index 5e089f0..748d3d8 100644 --- a/assemblyai/streaming/v3/async_client.py +++ b/assemblyai/streaming/v3/async_client.py @@ -27,6 +27,7 @@ _as_bytes_chunk, _BaseStreamingClient, _build_headers, + _build_http_base_url, _build_uri, _dump_model, _dump_model_json, @@ -564,7 +565,7 @@ def _get_or_create_client(self) -> httpx.AsyncClient: if self._api_key: headers["Authorization"] = self._api_key self._http_client = httpx.AsyncClient( - base_url="https://" + self._api_host, + base_url=_build_http_base_url(self._api_host), headers=headers, ) return self._http_client diff --git a/assemblyai/streaming/v3/client.py b/assemblyai/streaming/v3/client.py index 1b47f0c..ebc2a9c 100644 --- a/assemblyai/streaming/v3/client.py +++ b/assemblyai/streaming/v3/client.py @@ -17,6 +17,7 @@ _as_bytes_chunk, _BaseStreamingClient, _build_headers, + _build_http_base_url, _build_uri, _dump_model, _dump_model_json, @@ -439,7 +440,7 @@ def __init__(self, api_host: str, api_key: Optional[str] = None): headers["Authorization"] = api_key self._http_client = httpx.Client( - base_url="https://" + api_host, + base_url=_build_http_base_url(api_host), headers=headers, ) diff --git a/tests/unit/test_streaming.py b/tests/unit/test_streaming.py index 6e3bc0b..bc5a1aa 100644 --- a/tests/unit/test_streaming.py +++ b/tests/unit/test_streaming.py @@ -3014,3 +3014,44 @@ def test_update_configuration_unknown_field_raises_and_sends_nothing( # Then: nothing is enqueued assert client._write_queue.qsize() == 0 + + +@pytest.mark.parametrize( + ("api_host", "expected_base_url"), + [ + ("streaming.assemblyai.com", "https://streaming.assemblyai.com"), + ("wss://streaming.assemblyai.com", "https://streaming.assemblyai.com"), + ("ws://127.0.0.1:8080", "http://127.0.0.1:8080"), + ], +) +def test_create_temporary_token_derives_http_base_url( + api_host: str, expected_base_url: str +): + client = StreamingClient( + StreamingClientOptions(api_key="test", api_host=api_host) + ) + assert str(client._client._http_client.base_url).rstrip("/") == expected_base_url + + +@pytest.mark.parametrize( + ("api_host", "expected_base_url"), + [ + ("streaming.assemblyai.com", "https://streaming.assemblyai.com"), + ("wss://streaming.assemblyai.com", "https://streaming.assemblyai.com"), + ("ws://127.0.0.1:8080", "http://127.0.0.1:8080"), + ], +) +@pytest.mark.asyncio +async def test_async_create_temporary_token_derives_http_base_url( + api_host: str, expected_base_url: str +): + from assemblyai.streaming.v3 import AsyncStreamingClient + + client = AsyncStreamingClient( + StreamingClientOptions(api_key="test", api_host=api_host) + ) + http_client = client._client._get_or_create_client() + try: + assert str(http_client.base_url).rstrip("/") == expected_base_url + finally: + await client.disconnect()