Skip to content

Commit

Permalink
start adding video api
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Sep 23, 2024
1 parent 8b3bb19 commit c5a93da
Show file tree
Hide file tree
Showing 15 changed files with 714 additions and 652 deletions.
7 changes: 6 additions & 1 deletion http_client/src/vonage_http_client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(

self._api_key = api_key
self._api_secret = api_secret
self._application_id = application_id

if application_id is not None and private_key is not None:
self._jwt_client = JwtClient(application_id, private_key)
Expand All @@ -60,10 +61,14 @@ def api_key(self):
def api_secret(self):
return self._api_secret

@property
def application_id(self):
return self._application_id

def create_jwt_auth_string(self):
return b'Bearer ' + self.generate_application_jwt()

def generate_application_jwt(self, claims: dict = None):
def generate_application_jwt(self, claims: dict = {}) -> bytes:
try:
return self._jwt_client.generate_application_jwt(claims)
except AttributeError as err:
Expand Down
6 changes: 6 additions & 0 deletions http_client/src/vonage_http_client/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class HttpClient:
The http_client_options dict can have any of the following fields:
api_host (str, optional): The API host to use for HTTP requests. Defaults to 'api.nexmo.com'.
rest_host (str, optional): The REST host to use for HTTP requests. Defaults to 'rest.nexmo.com'.
video_host (str, optional): The Video host to use for HTTP requests. Defaults to 'video.api.vonage.com'.
timeout (int, optional): The timeout for HTTP requests in seconds. Defaults to None.
pool_connections (int, optional): The number of pool connections. Must be > 0. Default is 10.
pool_maxsize (int, optional): The maximum size of the connection pool. Must be > 0. Default is 10.
Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(

self._api_host = self._http_client_options.api_host
self._rest_host = self._http_client_options.rest_host
self._video_host = self._http_client_options.video_host

self._timeout = self._http_client_options.timeout
self._session = Session()
Expand Down Expand Up @@ -102,6 +104,10 @@ def api_host(self):
def rest_host(self):
return self._rest_host

@property
def video_host(self):
return self._video_host

@property
def user_agent(self):
return self._user_agent
Expand Down
4 changes: 2 additions & 2 deletions jwt/src/vonage_jwt/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, application_id: str, private_key: str):
'Both of "application_id" and "private_key" are required.'
)

def generate_application_jwt(self, jwt_options: dict = {}):
def generate_application_jwt(self, jwt_options: dict = {}) -> bytes:
"""Generates a JWT for the specified Vonage application.
You can override values for application_id and private_key on the JWTClient object by
Expand All @@ -44,7 +44,7 @@ def generate_application_jwt(self, jwt_options: dict = {}):
token = encode(payload, self._private_key, algorithm='RS256', headers=headers)
return bytes(token, 'utf-8')

def _set_private_key(self, key: Union[str, bytes]):
def _set_private_key(self, key: Union[str, bytes]) -> None:
if isinstance(key, (str, bytes)) and re.search("[.][a-zA-Z0-9_]+$", key):
with open(key, "rb") as key_file:
self._private_key = key_file.read()
Expand Down
10 changes: 9 additions & 1 deletion video/src/vonage_video/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
from .enums import ArchiveMode, MediaMode, TokenRole
from .session import SessionOptions, VideoSession
from .token import TokenOptions

__all__ = ['MediaMode', 'ArchiveMode', 'TokenRole', 'TokenOptions']
__all__ = [
'MediaMode',
'ArchiveMode',
'TokenRole',
'TokenOptions',
'SessionOptions',
'VideoSession',
]
5 changes: 5 additions & 0 deletions video/src/vonage_video/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ class ArchiveMode(str, Enum):
class MediaMode(str, Enum):
ROUTED = 'routed'
RELAYED = 'relayed'


class P2pPreference(str, Enum):
DISABLED = 'disabled'
ALWAYS = 'always'
19 changes: 0 additions & 19 deletions video/src/vonage_video/models/requests.py

This file was deleted.

47 changes: 47 additions & 0 deletions video/src/vonage_video/models/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Optional

from pydantic import BaseModel, Field, model_validator

from .enums import ArchiveMode, MediaMode, P2pPreference


class SessionOptions(BaseModel):
"""Options for creating a new session.
Args:
media_mode (MediaMode): The media mode for the session.
archive_mode (ArchiveMode): The archive mode for the session.
location (str): The location of the session.
e2ee (bool): Whether end-to-end encryption is enabled.
p2p_preference (str): The preference for peer-to-peer connections.
This is set automatically by selecting the `media_mode`.
"""

archive_mode: Optional[ArchiveMode] = Field(None, serialization_alias='archiveMode')
location: Optional[str] = None
media_mode: Optional[MediaMode] = None
e2ee: Optional[bool] = None
p2p_preference: Optional[str] = Field(
P2pPreference.DISABLED, serialization_alias='p2p.preference'
)

@model_validator(mode='after')
def set_p2p_preference(self):
if self.media_mode == MediaMode.ROUTED:
self.p2p_preference = P2pPreference.DISABLED
if self.media_mode == MediaMode.RELAYED:
self.p2p_preference = P2pPreference.ALWAYS
return self

@model_validator(mode='after')
def set_p2p_preference_if_archive_mode_set(self):
if self.archive_mode == ArchiveMode.ALWAYS:
self.p2p_preference = P2pPreference.DISABLED
return self


class VideoSession(BaseModel):
session_id: str
archive_mode: Optional[ArchiveMode] = None
media_mode: Optional[MediaMode] = None
location: Optional[str] = None
File renamed without changes.
21 changes: 21 additions & 0 deletions video/src/vonage_video/models/stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Optional

from pydantic import BaseModel, Field


class StreamInfo(BaseModel):
id: Optional[str] = Field(None, validation_alias='id')
video_type: Optional[str] = Field(None, validation_alias='videoType')
name: Optional[str] = Field(None, validation_alias='name')
layout_class_list: Optional[List[str]] = Field(
None, validation_alias='layoutClassList'
)


class StreamLayout(BaseModel):
id: str
layout_class_list: List[str] = Field(..., validation_alias='layoutClassList')


class StreamLayoutOptions(BaseModel):
items: List[StreamLayout]
6 changes: 3 additions & 3 deletions video/src/vonage_video/models/token.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from time import time
from typing import List, Literal, Optional, Union
from uuid import UUID, uuid4
from typing import List, Literal, Optional
from uuid import uuid4

from pydantic import BaseModel, field_validator, model_validator

Expand Down Expand Up @@ -29,7 +29,7 @@ class TokenOptions(BaseModel):
connection_data: Optional[str] = None
initial_layout_class_list: Optional[List[str]] = None
exp: Optional[int] = None
jti: Union[UUID, str] = uuid4()
jti: str = str(uuid4())
iat: int = int(time())
subject: Literal['video'] = 'video'
scope: Literal['session.connect'] = 'session.connect'
Expand Down
Loading

0 comments on commit c5a93da

Please sign in to comment.