Skip to content
Merged
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
15 changes: 12 additions & 3 deletions livekit-api/livekit/api/livekit_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def __init__(
api_key: Optional[str] = None,
api_secret: Optional[str] = None,
*,
timeout: aiohttp.ClientTimeout = aiohttp.ClientTimeout(total=60), # 60 seconds
timeout: Optional[aiohttp.ClientTimeout] = None,
session: Optional[aiohttp.ClientSession] = None,
):
"""Create a new LiveKitAPI instance.

Expand All @@ -37,6 +38,7 @@ def __init__(
api_key: API key (read from `LIVEKIT_API_KEY` environment variable if not provided)
api_secret: API secret (read from `LIVEKIT_API_SECRET` environment variable if not provided)
timeout: Request timeout (default: 60 seconds)
session: aiohttp.ClientSession instance to use for requests, if not provided, a new one will be created
"""
url = url or os.getenv("LIVEKIT_URL")
api_key = api_key or os.getenv("LIVEKIT_API_KEY")
Expand All @@ -48,7 +50,12 @@ def __init__(
if not api_key or not api_secret:
raise ValueError("api_key and api_secret must be set")

self._session = aiohttp.ClientSession(timeout=timeout)
if not timeout:
timeout = aiohttp.ClientTimeout(total=60)

self._custom_session = True if session is None else False
self._session = session or aiohttp.ClientSession(timeout=timeout)

self._room = RoomService(self._session, url, api_key, api_secret)
self._ingress = IngressService(self._session, url, api_key, api_secret)
self._egress = EgressService(self._session, url, api_key, api_secret)
Expand Down Expand Up @@ -84,7 +91,9 @@ async def aclose(self):
"""Close the API client

Call this before your application exits or when the API client is no longer needed."""
await self._session.close()
# we do not close custom sessions, that's up to the caller
if not self._custom_session:
await self._session.close()

async def __aenter__(self):
"""@private
Expand Down
Loading