13
13
from jose import jwt # _create_jwt_auth_header
14
14
import random # _create_jwt_auth_header
15
15
import logging # logging
16
+ import warnings # Native. Used for notifying deprecations
17
+
16
18
17
19
# compat
18
20
from six .moves .urllib .parse import urlencode
@@ -102,6 +104,8 @@ def __init__(
102
104
self ._proxies = None
103
105
self .endpoints = Endpoints (api_url , self .api_key )
104
106
self ._app_version = __version__ if app_version == None else app_version
107
+ # JWT custom claims - Default values
108
+ self ._jwt_livetime = 3 # In minutes
105
109
106
110
@property
107
111
def proxies (self ):
@@ -119,6 +123,14 @@ def app_version(self):
119
123
def app_version (self , value ):
120
124
self ._app_version = value
121
125
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
+
122
134
def generate_token (
123
135
self ,
124
136
session_id ,
@@ -382,9 +394,9 @@ def create_session(
382
394
self .proxies ,
383
395
)
384
396
response = requests .post (
385
- self .endpoints .session_url (),
397
+ self .endpoints .get_session_url (),
386
398
data = options ,
387
- headers = self .headers (),
399
+ headers = self .get_headers (),
388
400
proxies = self .proxies ,
389
401
timeout = self .timeout ,
390
402
)
@@ -423,7 +435,7 @@ def create_session(
423
435
except Exception as e :
424
436
raise OpenTokException ("Failed to generate session: %s" % str (e ))
425
437
426
- def headers (self ):
438
+ def get_headers (self ):
427
439
"""For internal use."""
428
440
return {
429
441
"User-Agent" : "OpenTok-Python-SDK/"
@@ -433,12 +445,28 @@ def headers(self):
433
445
"X-OPENTOK-AUTH" : self ._create_jwt_auth_header (),
434
446
}
435
447
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 ):
437
457
"""For internal use."""
438
- result = self .headers ()
458
+ result = self .get_headers ()
439
459
result ["Content-Type" ] = "application/json"
440
460
return result
441
461
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
+
442
470
def start_archive (
443
471
self ,
444
472
session_id ,
@@ -514,9 +542,9 @@ def start_archive(
514
542
)
515
543
516
544
response = requests .post (
517
- self .endpoints .archive_url (),
545
+ self .endpoints .get_archive_url (),
518
546
data = json .dumps (payload ),
519
- headers = self .json_headers (),
547
+ headers = self .get_json_headers (),
520
548
proxies = self .proxies ,
521
549
timeout = self .timeout ,
522
550
)
@@ -560,8 +588,8 @@ def stop_archive(self, archive_id):
560
588
)
561
589
562
590
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 (),
565
593
proxies = self .proxies ,
566
594
timeout = self .timeout ,
567
595
)
@@ -595,8 +623,8 @@ def delete_archive(self, archive_id):
595
623
)
596
624
597
625
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 (),
600
628
proxies = self .proxies ,
601
629
timeout = self .timeout ,
602
630
)
@@ -625,8 +653,8 @@ def get_archive(self, archive_id):
625
653
)
626
654
627
655
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 (),
630
658
proxies = self .proxies ,
631
659
timeout = self .timeout ,
632
660
)
@@ -661,7 +689,7 @@ def get_archives(self, offset=None, count=None, session_id=None):
661
689
if session_id is not None :
662
690
params ["sessionId" ] = session_id
663
691
664
- endpoint = self .endpoints .archive_url () + "?" + urlencode (params )
692
+ endpoint = self .endpoints .get_archive_url () + "?" + urlencode (params )
665
693
666
694
logger .debug (
667
695
"GET to %r with headers %r, proxies %r" ,
@@ -672,7 +700,7 @@ def get_archives(self, offset=None, count=None, session_id=None):
672
700
673
701
response = requests .get (
674
702
endpoint ,
675
- headers = self .json_headers (),
703
+ headers = self .get_json_headers (),
676
704
proxies = self .proxies ,
677
705
timeout = self .timeout ,
678
706
)
@@ -693,7 +721,7 @@ def list_archives(self, offset=None, count=None, session_id=None):
693
721
"""
694
722
return self .get_archives (offset , count , session_id )
695
723
696
- def signal (self , session_id , payload , connection_id = None ):
724
+ def send_signal (self , session_id , payload , connection_id = None ):
697
725
"""
698
726
Send signals to all participants in an active OpenTok session or to a specific client
699
727
connected to that session.
@@ -717,9 +745,9 @@ def signal(self, session_id, payload, connection_id=None):
717
745
)
718
746
719
747
response = requests .post (
720
- self .endpoints .signaling_url (session_id , connection_id ),
748
+ self .endpoints .get_signaling_url (session_id , connection_id ),
721
749
data = json .dumps (payload ),
722
- headers = self .json_headers (),
750
+ headers = self .get_json_headers (),
723
751
proxies = self .proxies ,
724
752
timeout = self .timeout ,
725
753
)
@@ -745,6 +773,14 @@ def signal(self, session_id, payload, connection_id=None):
745
773
else :
746
774
raise RequestError ("An unexpected error occurred" , response .status_code )
747
775
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
+
748
784
def get_stream (self , session_id , stream_id ):
749
785
"""
750
786
Returns an Stream object that contains information of an OpenTok stream:
@@ -765,7 +801,7 @@ def get_stream(self, session_id, stream_id):
765
801
766
802
response = requests .get (
767
803
endpoint ,
768
- headers = self .json_headers (),
804
+ headers = self .get_json_headers (),
769
805
proxies = self .proxies ,
770
806
timeout = self .timeout ,
771
807
)
@@ -802,7 +838,7 @@ def list_streams(self, session_id):
802
838
803
839
response = requests .get (
804
840
endpoint ,
805
- headers = self .json_headers (),
841
+ headers = self .get_json_headers (),
806
842
proxies = self .proxies ,
807
843
timeout = self .timeout ,
808
844
)
@@ -838,7 +874,7 @@ def force_disconnect(self, session_id, connection_id):
838
874
839
875
response = requests .delete (
840
876
endpoint ,
841
- headers = self .json_headers (),
877
+ headers = self .get_json_headers (),
842
878
proxies = self .proxies ,
843
879
timeout = self .timeout ,
844
880
)
@@ -893,7 +929,7 @@ def set_archive_layout(self, archive_id, layout_type, stylesheet=None):
893
929
response = requests .put (
894
930
endpoint ,
895
931
data = json .dumps (payload ),
896
- headers = self .json_headers (),
932
+ headers = self .get_json_headers (),
897
933
proxies = self .proxies ,
898
934
timeout = self .timeout ,
899
935
)
@@ -973,7 +1009,7 @@ def dial(self, session_id, token, sip_uri, options=[]):
973
1009
response = requests .post (
974
1010
endpoint ,
975
1011
data = json .dumps (payload ),
976
- headers = self .json_headers (),
1012
+ headers = self .get_json_headers (),
977
1013
proxies = self .proxies ,
978
1014
timeout = self .timeout ,
979
1015
)
@@ -1027,7 +1063,7 @@ class names (Strings) to apply to the stream. For example:
1027
1063
response = requests .put (
1028
1064
endpoint ,
1029
1065
data = json .dumps (items_payload ),
1030
- headers = self .json_headers (),
1066
+ headers = self .get_json_headers (),
1031
1067
proxies = self .proxies ,
1032
1068
timeout = self .timeout ,
1033
1069
)
@@ -1099,7 +1135,7 @@ def start_broadcast(self, session_id, options):
1099
1135
response = requests .post (
1100
1136
endpoint ,
1101
1137
data = json .dumps (payload ),
1102
- headers = self .json_headers (),
1138
+ headers = self .get_json_headers (),
1103
1139
proxies = self .proxies ,
1104
1140
timeout = self .timeout ,
1105
1141
)
@@ -1129,6 +1165,7 @@ def stop_broadcast(self, broadcast_id):
1129
1165
:rtype A Broadcast object, which contains information of the broadcast: id, sessionId
1130
1166
projectId, createdAt, updatedAt and resolution
1131
1167
"""
1168
+
1132
1169
endpoint = self .endpoints .broadcast_url (broadcast_id , stop = True )
1133
1170
1134
1171
logger .debug (
@@ -1137,10 +1174,10 @@ def stop_broadcast(self, broadcast_id):
1137
1174
self .json_headers (),
1138
1175
self .proxies ,
1139
1176
)
1140
-
1177
+
1141
1178
response = requests .post (
1142
1179
endpoint ,
1143
- headers = self .json_headers (),
1180
+ headers = self .get_json_headers (),
1144
1181
proxies = self .proxies ,
1145
1182
timeout = self .timeout ,
1146
1183
)
@@ -1171,6 +1208,7 @@ def get_broadcast(self, broadcast_id):
1171
1208
:rtype A Broadcast object, which contains information of the broadcast: id, sessionId
1172
1209
projectId, createdAt, updatedAt, resolution, broadcastUrls and status
1173
1210
"""
1211
+
1174
1212
endpoint = self .endpoints .broadcast_url (broadcast_id )
1175
1213
1176
1214
logger .debug (
@@ -1182,7 +1220,7 @@ def get_broadcast(self, broadcast_id):
1182
1220
1183
1221
response = requests .get (
1184
1222
endpoint ,
1185
- headers = self .json_headers (),
1223
+ headers = self .get_json_headers (),
1186
1224
proxies = self .proxies ,
1187
1225
timeout = self .timeout ,
1188
1226
)
@@ -1234,7 +1272,7 @@ def set_broadcast_layout(self, broadcast_id, layout_type, stylesheet=None):
1234
1272
response = requests .put (
1235
1273
endpoint ,
1236
1274
data = json .dumps (payload ),
1237
- headers = self .json_headers (),
1275
+ headers = self .get_json_headers (),
1238
1276
proxies = self .proxies ,
1239
1277
timeout = self .timeout ,
1240
1278
)
@@ -1261,7 +1299,8 @@ def _create_jwt_auth_header(self):
1261
1299
"ist" : "project" ,
1262
1300
"iss" : self .api_key ,
1263
1301
"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)
1265
1304
"jti" : "{0}" .format (0 , random .random ()),
1266
1305
}
1267
1306
0 commit comments