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

load env at runtime #113

Merged
merged 3 commits into from
Dec 4, 2023
Merged
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
23 changes: 17 additions & 6 deletions livekit-api/livekit/api/access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import datetime
import os
import jwt
from typing import Optional

DEFAULT_TTL = datetime.timedelta(hours=6)
DEFAULT_LEEWAY = datetime.timedelta(minutes=1)
Expand Down Expand Up @@ -74,14 +75,18 @@ class Claims:
class AccessToken:
def __init__(
self,
api_key: str = os.getenv("LIVEKIT_API_KEY", ""),
api_secret: str = os.getenv("LIVEKIT_API_SECRET", ""),
api_key: Optional[str] = None,
api_secret: Optional[str] = None,
) -> None:
api_key = api_key or os.getenv("LIVEKIT_API_KEY")
api_secret = api_secret or os.getenv("LIVEKIT_API_SECRET")

if not api_key or not api_secret:
raise ValueError("api_key and api_secret must be set")

self.api_key = api_key # iss
self.api_secret = api_secret
self.claims = Claims()
if not api_key or not api_secret:
raise ValueError("api_key and api_secret must be set")

# default jwt claims
self.identity = "" # sub
Expand Down Expand Up @@ -137,11 +142,17 @@ def to_jwt(self) -> str:
class TokenVerifier:
def __init__(
self,
api_key: str = os.getenv("LIVEKIT_API_KEY", ""),
api_secret: str = os.getenv("LIVEKIT_API_SECRET", ""),
api_key: Optional[str] = None,
api_secret: Optional[str] = None,
*,
leeway: datetime.timedelta = DEFAULT_LEEWAY,
) -> None:
api_key = api_key or os.getenv("LIVEKIT_API_KEY")
api_secret = api_secret or os.getenv("LIVEKIT_API_SECRET")

if not api_key or not api_secret:
raise ValueError("api_key and api_secret must be set")

self.api_key = api_key
self.api_secret = api_secret
self._leeway = leeway
Expand Down
17 changes: 14 additions & 3 deletions livekit-api/livekit/api/livekit_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
from .room_service import RoomService
from .egress_service import EgressService
from .ingress_service import IngressService
from typing import Optional


class LiveKitAPI:
def __init__(
self,
url: str = os.getenv("LIVEKIT_URL", "http://localhost:7880"),
api_key: str = os.getenv("LIVEKIT_API_KEY", ""),
api_secret: str = os.getenv("LIVEKIT_API_SECRET", ""),
url: Optional[str] = None,
api_key: Optional[str] = None,
api_secret: Optional[str] = None,
*,
timeout: float = 60, # 1 minutes by default
):
url = url or os.getenv("LIVEKIT_URL")
api_key = api_key or os.getenv("LIVEKIT_API_KEY")
api_secret = api_secret or os.getenv("LIVEKIT_API_SECRET")

if not url:
raise ValueError("url must be set")

if not api_key or not api_secret:
raise ValueError("api_key and api_secret must be set")

self._session = aiohttp.ClientSession(timeout=timeout)
self._room = RoomService(url, api_key, api_secret, self._session)
self._ingress = IngressService(url, api_key, api_secret, self._session)
Expand Down
Loading