Skip to content

Commit

Permalink
adding application_id back to client for video compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Aug 25, 2023
1 parent 4492ba6 commit 6e2eb47
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/vonage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def __init__(
self.signature_secret = signature_secret
self.signature_method = signature_method

self.application_id = application_id

if self.signature_method in {
"md5",
"sha1",
Expand Down Expand Up @@ -444,9 +446,9 @@ def _add_individual_errors(self, error_data):
return message

def _create_jwt_auth_string(self):
return b"Bearer " + self._generate_application_jwt()
return b"Bearer " + self.generate_application_jwt()

def _generate_application_jwt(self):
def generate_application_jwt(self):
try:
return self._jwt_client.generate_application_jwt(self._jwt_claims)
except AttributeError as err:
Expand Down
15 changes: 9 additions & 6 deletions src/vonage/video.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from vonage import Client

from .errors import (
InvalidRoleError,
TokenExpiryError,
Expand All @@ -18,7 +24,7 @@ class Video:
media_mode_values = {'routed', 'relayed'}
token_roles = {'subscriber', 'publisher', 'moderator'}

def __init__(self, client):
def __init__(self, client: Client):
self._client = client

def create_session(self, session_options: dict = None):
Expand Down Expand Up @@ -313,7 +319,6 @@ def remove_stream_from_broadcast(self, broadcast_id: str, stream_id: str):
def generate_client_token(self, session_id, token_options={}):
now = int(time())
claims = {
'application_id': self._client.application_id,
'scope': 'session.connect',
'session_id': session_id,
'role': 'publisher',
Expand All @@ -339,10 +344,8 @@ def generate_client_token(self, session_id, token_options={}):
claims['acl'] = token_options['acl']

self.validate_client_token_options(claims)
headers = {'typ': 'JWT', 'alg': 'RS256'}
return jwt.encode(
payload=claims, key=self._client._private_key, algorithm='RS256', headers=headers
)
self._client.auth(claims)
return self._client.generate_application_jwt()

def validate_client_token_options(self, claims):
now = int(time())
Expand Down
4 changes: 2 additions & 2 deletions tests/test_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def vonage_jwt_mock(self, claims):

def test_generate_application_jwt(client):
with patch('vonage.client.JwtClient.generate_application_jwt', vonage_jwt_mock):
jwt = client._generate_application_jwt()
jwt = client.generate_application_jwt()
assert jwt == test_jwt


Expand All @@ -47,7 +47,7 @@ def test_create_jwt_error_no_application_id_or_private_key():
empty_client = Client()

with raises(ClientError) as err:
empty_client._generate_application_jwt()
empty_client.generate_application_jwt()
assert (
str(err.value)
== 'JWT generation failed. Check that you passed in valid values for "application_id" and "private_key".'
Expand Down
52 changes: 28 additions & 24 deletions tests/test_video.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from util import *
from vonage import Client
from vonage.errors import (
ClientError,
InvalidRoleError,
Expand All @@ -20,7 +21,7 @@


@responses.activate
def test_create_default_session(client, dummy_data):
def test_create_default_session(client: Client, dummy_data):
stub(
responses.POST,
"https://video.api.vonage.com/session/create",
Expand All @@ -37,7 +38,7 @@ def test_create_default_session(client, dummy_data):


@responses.activate
def test_create_session_custom_archive_mode_and_location(client, dummy_data):
def test_create_session_custom_archive_mode_and_location(client: Client, dummy_data):
stub(
responses.POST,
"https://video.api.vonage.com/session/create",
Expand All @@ -55,7 +56,7 @@ def test_create_session_custom_archive_mode_and_location(client, dummy_data):


@responses.activate
def test_create_session_custom_media_mode(client, dummy_data):
def test_create_session_custom_media_mode(client: Client, dummy_data):
stub(
responses.POST,
"https://video.api.vonage.com/session/create",
Expand Down Expand Up @@ -135,7 +136,6 @@ def test_generate_client_token_custom_options(client):
def test_check_client_token_headers(client):
token = client.video.generate_client_token(session_id)
headers = jwt.get_unverified_header(token)
print(headers)
assert headers['alg'] == 'RS256'
assert headers['typ'] == 'JWT'

Expand All @@ -152,7 +152,7 @@ def test_generate_client_token_invalid_expire_time(client):


@responses.activate
def test_get_stream(client, dummy_data):
def test_get_stream(client: Client, dummy_data):
stub(
responses.GET,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/stream/{stream_id}",
Expand All @@ -166,7 +166,7 @@ def test_get_stream(client, dummy_data):


@responses.activate
def test_list_streams(client, dummy_data):
def test_list_streams(client: Client, dummy_data):
stub(
responses.GET,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/stream",
Expand All @@ -180,7 +180,7 @@ def test_list_streams(client, dummy_data):


@responses.activate
def test_change_stream_layout(client, dummy_data):
def test_change_stream_layout(client: Client, dummy_data):
stub(
responses.PUT,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/stream",
Expand All @@ -194,7 +194,7 @@ def test_change_stream_layout(client, dummy_data):


@responses.activate
def test_send_signal_to_all_participants(client, dummy_data):
def test_send_signal_to_all_participants(client: Client, dummy_data):
stub(
responses.POST,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/signal",
Expand All @@ -208,7 +208,7 @@ def test_send_signal_to_all_participants(client, dummy_data):


@responses.activate
def test_send_signal_to_single_participant(client, dummy_data):
def test_send_signal_to_single_participant(client: Client, dummy_data):
stub(
responses.POST,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/connection/{connection_id}/signal",
Expand All @@ -225,7 +225,7 @@ def test_send_signal_to_single_participant(client, dummy_data):


@responses.activate
def test_disconnect_client(client, dummy_data):
def test_disconnect_client(client: Client, dummy_data):
stub(
responses.DELETE,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/connection/{connection_id}",
Expand All @@ -236,7 +236,7 @@ def test_disconnect_client(client, dummy_data):


@responses.activate
def test_mute_specific_stream(client, dummy_data):
def test_mute_specific_stream(client: Client, dummy_data):
stub(
responses.POST,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/stream/{stream_id}/mute",
Expand All @@ -250,7 +250,7 @@ def test_mute_specific_stream(client, dummy_data):


@responses.activate
def test_mute_all_streams(client, dummy_data):
def test_mute_all_streams(client: Client, dummy_data):
stub(
responses.POST,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/mute",
Expand All @@ -264,7 +264,7 @@ def test_mute_all_streams(client, dummy_data):


@responses.activate
def test_mute_all_streams_except_excluded_list(client, dummy_data):
def test_mute_all_streams_except_excluded_list(client: Client, dummy_data):
stub(
responses.POST,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/mute",
Expand All @@ -280,7 +280,7 @@ def test_mute_all_streams_except_excluded_list(client, dummy_data):


@responses.activate
def test_disable_mute_all_streams(client, dummy_data):
def test_disable_mute_all_streams(client: Client, dummy_data):
stub(
responses.POST,
f"https://video.api.vonage.com/v2/project/{client.application_id}/session/{session_id}/mute",
Expand All @@ -300,7 +300,7 @@ def test_disable_mute_all_streams(client, dummy_data):


@responses.activate
def test_list_archives_with_filters_applied(client, dummy_data):
def test_list_archives_with_filters_applied(client: Client, dummy_data):
stub(
responses.GET,
f"https://video.api.vonage.com/v2/project/{client.application_id}/archive",
Expand All @@ -315,7 +315,7 @@ def test_list_archives_with_filters_applied(client, dummy_data):


@responses.activate
def test_create_new_archive(client, dummy_data):
def test_create_new_archive(client: Client, dummy_data):
stub(
responses.POST,
f"https://video.api.vonage.com/v2/project/{client.application_id}/archive",
Expand All @@ -332,7 +332,7 @@ def test_create_new_archive(client, dummy_data):


@responses.activate
def test_get_archive(client, dummy_data):
def test_get_archive(client: Client, dummy_data):
stub(
responses.GET,
f"https://video.api.vonage.com/v2/project/{client.application_id}/archive/{archive_id}",
Expand All @@ -348,23 +348,25 @@ def test_get_archive(client, dummy_data):


@responses.activate
def test_delete_archive(client, dummy_data):
def test_delete_archive(client: Client, dummy_data):
stub(
responses.GET,
f"https://video.api.vonage.com/v2/project/{client.application_id}/archive/{archive_id}",
status_code=204,
fixture_path='no_content.json',
)

assert client.video.delete_archive(archive_id=archive_id) == None
assert request_user_agent() == dummy_data.user_agent


@responses.activate
def test_add_stream_to_archive(client, dummy_data):
def test_add_stream_to_archive(client: Client, dummy_data):
stub(
responses.PATCH,
f"https://video.api.vonage.com/v2/project/{client.application_id}/archive/{archive_id}/streams",
status_code=204,
fixture_path='no_content.json',
)

assert (
Expand All @@ -377,19 +379,20 @@ def test_add_stream_to_archive(client, dummy_data):


@responses.activate
def test_remove_stream_from_archive(client, dummy_data):
def test_remove_stream_from_archive(client: Client, dummy_data):
stub(
responses.PATCH,
f"https://video.api.vonage.com/v2/project/{client.application_id}/archive/{archive_id}/streams",
status_code=204,
fixture_path='no_content.json',
)

assert client.video.remove_stream_from_archive(archive_id=archive_id, stream_id='1234') == None
assert request_user_agent() == dummy_data.user_agent


@responses.activate
def test_stop_archive(client, dummy_data):
def test_stop_archive(client: Client, dummy_data):
stub(
responses.POST,
f"https://video.api.vonage.com/v2/project/{client.application_id}/archive/{archive_id}/stop",
Expand All @@ -405,7 +408,7 @@ def test_stop_archive(client, dummy_data):


@responses.activate
def test_change_archive_layout(client, dummy_data):
def test_change_archive_layout(client: Client, dummy_data):
stub(
responses.PUT,
f"https://video.api.vonage.com/v2/project/{client.application_id}/archive/{archive_id}/layout",
Expand Down Expand Up @@ -665,11 +668,12 @@ def test_change_broadcast_layout(client):


@responses.activate
def test_add_stream_to_broadcast(client, dummy_data):
def test_add_stream_to_broadcast(client: Client, dummy_data):
stub(
responses.PATCH,
f"https://video.api.vonage.com/v2/project/{client.application_id}/broadcast/{broadcast_id}/streams",
status_code=204,
fixture_path='no_content.json',
)

assert (
Expand All @@ -682,7 +686,7 @@ def test_add_stream_to_broadcast(client, dummy_data):


@responses.activate
def test_remove_stream_from_archive(client, dummy_data):
def test_remove_stream_from_archive(client: Client, dummy_data):
stub(
responses.PATCH,
f"https://video.api.vonage.com/v2/project/{client.application_id}/broadcast/{broadcast_id}/streams",
Expand Down

0 comments on commit 6e2eb47

Please sign in to comment.