1313from jose import jwt # _create_jwt_auth_header
1414import random # _create_jwt_auth_header
1515import logging # logging
16+ import warnings # Native. Used for notifying deprecations
17+
1618
1719# compat
1820from six .moves .urllib .parse import urlencode
@@ -102,6 +104,8 @@ def __init__(
102104 self ._proxies = None
103105 self .endpoints = Endpoints (api_url , self .api_key )
104106 self ._app_version = __version__ if app_version == None else app_version
107+ # JWT custom claims - Default values
108+ self ._jwt_livetime = 3 # In minutes
105109
106110 @property
107111 def proxies (self ):
@@ -119,6 +123,14 @@ def app_version(self):
119123 def app_version (self , value ):
120124 self ._app_version = value
121125
126+ @property
127+ def jwt_livetime (self ):
128+ return self ._jwt_livetime
129+
130+ @jwt_livetime .setter
131+ def jwt_livetime (self , minutes ):
132+ self ._jwt_livetime = minutes
133+
122134 def generate_token (
123135 self ,
124136 session_id ,
@@ -382,9 +394,9 @@ def create_session(
382394 self .proxies ,
383395 )
384396 response = requests .post (
385- self .endpoints .session_url (),
397+ self .endpoints .get_session_url (),
386398 data = options ,
387- headers = self .headers (),
399+ headers = self .get_headers (),
388400 proxies = self .proxies ,
389401 timeout = self .timeout ,
390402 )
@@ -423,7 +435,7 @@ def create_session(
423435 except Exception as e :
424436 raise OpenTokException ("Failed to generate session: %s" % str (e ))
425437
426- def headers (self ):
438+ def get_headers (self ):
427439 """For internal use."""
428440 return {
429441 "User-Agent" : "OpenTok-Python-SDK/"
@@ -433,12 +445,28 @@ def headers(self):
433445 "X-OPENTOK-AUTH" : self ._create_jwt_auth_header (),
434446 }
435447
436- def json_headers (self ):
448+ def headers (self ):
449+ warnings .warn (
450+ "opentok.headers is deprecated (use opentok.get_headers instead)." ,
451+ DeprecationWarning ,
452+ stacklevel = 2 ,
453+ )
454+ return self .get_headers ()
455+
456+ def get_json_headers (self ):
437457 """For internal use."""
438- result = self .headers ()
458+ result = self .get_headers ()
439459 result ["Content-Type" ] = "application/json"
440460 return result
441461
462+ def json_headers (self ):
463+ warnings .warn (
464+ "opentok.json_headers is deprecated (use opentok.get_json_headers instead)." ,
465+ DeprecationWarning ,
466+ stacklevel = 2 ,
467+ )
468+ return self .get_json_headers ()
469+
442470 def start_archive (
443471 self ,
444472 session_id ,
@@ -514,9 +542,9 @@ def start_archive(
514542 )
515543
516544 response = requests .post (
517- self .endpoints .archive_url (),
545+ self .endpoints .get_archive_url (),
518546 data = json .dumps (payload ),
519- headers = self .json_headers (),
547+ headers = self .get_json_headers (),
520548 proxies = self .proxies ,
521549 timeout = self .timeout ,
522550 )
@@ -560,8 +588,8 @@ def stop_archive(self, archive_id):
560588 )
561589
562590 response = requests .post (
563- self .endpoints .archive_url (archive_id ) + "/stop" ,
564- headers = self .json_headers (),
591+ self .endpoints .get_archive_url (archive_id ) + "/stop" ,
592+ headers = self .get_json_headers (),
565593 proxies = self .proxies ,
566594 timeout = self .timeout ,
567595 )
@@ -595,8 +623,8 @@ def delete_archive(self, archive_id):
595623 )
596624
597625 response = requests .delete (
598- self .endpoints .archive_url (archive_id ),
599- headers = self .json_headers (),
626+ self .endpoints .get_archive_url (archive_id ),
627+ headers = self .get_json_headers (),
600628 proxies = self .proxies ,
601629 timeout = self .timeout ,
602630 )
@@ -625,8 +653,8 @@ def get_archive(self, archive_id):
625653 )
626654
627655 response = requests .get (
628- self .endpoints .archive_url (archive_id ),
629- headers = self .json_headers (),
656+ self .endpoints .get_archive_url (archive_id ),
657+ headers = self .get_json_headers (),
630658 proxies = self .proxies ,
631659 timeout = self .timeout ,
632660 )
@@ -661,7 +689,7 @@ def get_archives(self, offset=None, count=None, session_id=None):
661689 if session_id is not None :
662690 params ["sessionId" ] = session_id
663691
664- endpoint = self .endpoints .archive_url () + "?" + urlencode (params )
692+ endpoint = self .endpoints .get_archive_url () + "?" + urlencode (params )
665693
666694 logger .debug (
667695 "GET to %r with headers %r, proxies %r" ,
@@ -672,7 +700,7 @@ def get_archives(self, offset=None, count=None, session_id=None):
672700
673701 response = requests .get (
674702 endpoint ,
675- headers = self .json_headers (),
703+ headers = self .get_json_headers (),
676704 proxies = self .proxies ,
677705 timeout = self .timeout ,
678706 )
@@ -693,7 +721,7 @@ def list_archives(self, offset=None, count=None, session_id=None):
693721 """
694722 return self .get_archives (offset , count , session_id )
695723
696- def signal (self , session_id , payload , connection_id = None ):
724+ def send_signal (self , session_id , payload , connection_id = None ):
697725 """
698726 Send signals to all participants in an active OpenTok session or to a specific client
699727 connected to that session.
@@ -717,9 +745,9 @@ def signal(self, session_id, payload, connection_id=None):
717745 )
718746
719747 response = requests .post (
720- self .endpoints .signaling_url (session_id , connection_id ),
748+ self .endpoints .get_signaling_url (session_id , connection_id ),
721749 data = json .dumps (payload ),
722- headers = self .json_headers (),
750+ headers = self .get_json_headers (),
723751 proxies = self .proxies ,
724752 timeout = self .timeout ,
725753 )
@@ -745,6 +773,14 @@ def signal(self, session_id, payload, connection_id=None):
745773 else :
746774 raise RequestError ("An unexpected error occurred" , response .status_code )
747775
776+ def signal (self , session_id , payload , connection_id = None ):
777+ warnings .warn (
778+ "opentok.signal is deprecated (use opentok.send_signal instead)." ,
779+ DeprecationWarning ,
780+ stacklevel = 2 ,
781+ )
782+ self .send_signal (session_id , payload , connection_id )
783+
748784 def get_stream (self , session_id , stream_id ):
749785 """
750786 Returns an Stream object that contains information of an OpenTok stream:
@@ -765,7 +801,7 @@ def get_stream(self, session_id, stream_id):
765801
766802 response = requests .get (
767803 endpoint ,
768- headers = self .json_headers (),
804+ headers = self .get_json_headers (),
769805 proxies = self .proxies ,
770806 timeout = self .timeout ,
771807 )
@@ -802,7 +838,7 @@ def list_streams(self, session_id):
802838
803839 response = requests .get (
804840 endpoint ,
805- headers = self .json_headers (),
841+ headers = self .get_json_headers (),
806842 proxies = self .proxies ,
807843 timeout = self .timeout ,
808844 )
@@ -838,7 +874,7 @@ def force_disconnect(self, session_id, connection_id):
838874
839875 response = requests .delete (
840876 endpoint ,
841- headers = self .json_headers (),
877+ headers = self .get_json_headers (),
842878 proxies = self .proxies ,
843879 timeout = self .timeout ,
844880 )
@@ -893,7 +929,7 @@ def set_archive_layout(self, archive_id, layout_type, stylesheet=None):
893929 response = requests .put (
894930 endpoint ,
895931 data = json .dumps (payload ),
896- headers = self .json_headers (),
932+ headers = self .get_json_headers (),
897933 proxies = self .proxies ,
898934 timeout = self .timeout ,
899935 )
@@ -973,7 +1009,7 @@ def dial(self, session_id, token, sip_uri, options=[]):
9731009 response = requests .post (
9741010 endpoint ,
9751011 data = json .dumps (payload ),
976- headers = self .json_headers (),
1012+ headers = self .get_json_headers (),
9771013 proxies = self .proxies ,
9781014 timeout = self .timeout ,
9791015 )
@@ -1027,7 +1063,7 @@ class names (Strings) to apply to the stream. For example:
10271063 response = requests .put (
10281064 endpoint ,
10291065 data = json .dumps (items_payload ),
1030- headers = self .json_headers (),
1066+ headers = self .get_json_headers (),
10311067 proxies = self .proxies ,
10321068 timeout = self .timeout ,
10331069 )
@@ -1099,7 +1135,7 @@ def start_broadcast(self, session_id, options):
10991135 response = requests .post (
11001136 endpoint ,
11011137 data = json .dumps (payload ),
1102- headers = self .json_headers (),
1138+ headers = self .get_json_headers (),
11031139 proxies = self .proxies ,
11041140 timeout = self .timeout ,
11051141 )
@@ -1129,6 +1165,7 @@ def stop_broadcast(self, broadcast_id):
11291165 :rtype A Broadcast object, which contains information of the broadcast: id, sessionId
11301166 projectId, createdAt, updatedAt and resolution
11311167 """
1168+
11321169 endpoint = self .endpoints .broadcast_url (broadcast_id , stop = True )
11331170
11341171 logger .debug (
@@ -1137,10 +1174,10 @@ def stop_broadcast(self, broadcast_id):
11371174 self .json_headers (),
11381175 self .proxies ,
11391176 )
1140-
1177+
11411178 response = requests .post (
11421179 endpoint ,
1143- headers = self .json_headers (),
1180+ headers = self .get_json_headers (),
11441181 proxies = self .proxies ,
11451182 timeout = self .timeout ,
11461183 )
@@ -1171,6 +1208,7 @@ def get_broadcast(self, broadcast_id):
11711208 :rtype A Broadcast object, which contains information of the broadcast: id, sessionId
11721209 projectId, createdAt, updatedAt, resolution, broadcastUrls and status
11731210 """
1211+
11741212 endpoint = self .endpoints .broadcast_url (broadcast_id )
11751213
11761214 logger .debug (
@@ -1182,7 +1220,7 @@ def get_broadcast(self, broadcast_id):
11821220
11831221 response = requests .get (
11841222 endpoint ,
1185- headers = self .json_headers (),
1223+ headers = self .get_json_headers (),
11861224 proxies = self .proxies ,
11871225 timeout = self .timeout ,
11881226 )
@@ -1234,7 +1272,7 @@ def set_broadcast_layout(self, broadcast_id, layout_type, stylesheet=None):
12341272 response = requests .put (
12351273 endpoint ,
12361274 data = json .dumps (payload ),
1237- headers = self .json_headers (),
1275+ headers = self .get_json_headers (),
12381276 proxies = self .proxies ,
12391277 timeout = self .timeout ,
12401278 )
@@ -1261,7 +1299,8 @@ def _create_jwt_auth_header(self):
12611299 "ist" : "project" ,
12621300 "iss" : self .api_key ,
12631301 "iat" : int (time .time ()), # current time in unix time (seconds)
1264- "exp" : int (time .time ()) + (60 * 3 ), # 3 minutes in the future (seconds)
1302+ "exp" : int (time .time ())
1303+ + (60 * self ._jwt_livetime ), # 3 minutes in the future (seconds)
12651304 "jti" : "{0}" .format (0 , random .random ()),
12661305 }
12671306
0 commit comments