def create_session(request): tbox = TokBox.objects.get(pk=1) opentok = OpenTok(tbox.api_key, tbox.api_secret) session = opentok.create_session(media_mode=MediaModes.routed) session_id = session.session_id return HttpResponse(session_id)
def create_interview_session(): if settings.ENVIRONMENT != "TEST": opentok = OpenTok(settings.OPENTOK_API_KEY, settings.OPENTOK_SECRET) session = opentok.create_session(media_mode=MediaModes.relayed) return session.session_id return "UNKNOWN"
def index(request): key = "45351832" secret = "29d4d704b3a28373202c230946bfe91e3e74c4fb" opentok = OpenTok(key, secret) if request.method == "POST": name = request.POST.get('name', '') role = request.POST.get('role', '') if role == "": role = "Publisher" ses_id = request.POST.get('session', '') try: if ses_id is "": ses = "New Session" session = opentok.create_session() session_id = session.session_id token = opentok.generate_token(session_id,role=Roles.publisher) else: ses = "Existing Session" session_id = ses_id if role == "moderator": token = opentok.generate_token(session_id,role=Roles.moderator) elif role == "subscriber": token = opentok.generate_token(session_id,role=Roles.subscriber) else: token = "Wrong Role requested for existing session id" data = {"user": name, "Session": ses, "role":role, "session:": {"Authentication": token, "Session ID": session_id}} except Exception as e: data = {"Exception": e.message, "Type": type(e)} return JsonResponse(data)
def create(): if request.method == 'POST': content = request.get_json(silent=True) fullName = cgi.escape(content['username']) className = cgi.escape(content['classname']) hashids = Hashids(salt=settings.HASHID_SALT,min_length=6) increment() count = get_count() hashid = hashids.encode(count) courseId = DEFAULT_COURSE_PREFIX + hashid userId = request.cookies.get('remote_userid') if 'remote_userid' in request.cookies else generate_user_id() userColor = request.cookies.get('remote_usercolor') if 'remote_usercolor' in request.cookies else generate_color() host = app.config.get('host') resp = make_response(hashid) # Add course to database key = courseId course = Course.get_or_insert(key, courseId=courseId, teacherName=fullName) course.put() # Add teacher to course # Create OpenTok session opentok_sdk = OpenTok(OPENTOK_API_KEY, OPENTOK_API_SECRET) # use tokbox server to route media streams; # if you want to use p2p - change media_mode to MediaModes.relayed opentok_session = opentok_sdk.create_session(media_mode = MediaModes.routed) opentok_token = opentok_sdk.generate_token(opentok_session.session_id) key = courseId + userId user = Student.get_or_insert(key, courseId = courseId, studentId = userId, fullName = fullName, color = userColor, role = 'TEACHER', opentokSessionId = opentok_session.session_id, opentokToken = opentok_token ) user.put() # Set user cookies (teacher role) auth = json.loads(request.cookies.get('remote_auth')) if 'remote_auth' in request.cookies else {} auth[hashid] = { 'role': 'Instructor', 'opentok_api_key': OPENTOK_API_KEY, 'opentok_session_id': user.opentokSessionId, 'opentok_token': user.opentokToken } resp.set_cookie('remote_userfullname', fullName) resp.set_cookie('remote_auth', json.dumps(auth)) resp.set_cookie('remote_userid', userId) resp.set_cookie('remote_usercolor', userColor) #resp.set_cookie('remote_userinitials', userInitials) return resp return redirect('/main#/create')
def post(self): caller = self.request.get('caller') callee = self.request.get('callee') registrations = Registration.query(Registration.uid == callee).order(-Registration.date).fetch(5) opentok = OpenTok(api_key, secret) session = opentok.create_session() token = session.generate_token() call = { 'caller': caller, 'callee': callee, 'apiKey': api_key, 'sessionId': session.session_id, 'token': token } if not registrations: self.abort(404) return logging.info(str(registrations)) for registration in registrations: self.signal(registration.session_id, call) self.response.write(json.dumps(call))
def post(self): caller = self.request.get('caller') callee = self.request.get('callee') registrations = Registration.query( Registration.uid == callee).order(-Registration.date).fetch(5) opentok = OpenTok(api_key, secret) session = opentok.create_session() token = session.generate_token() call = { 'caller': caller, 'callee': callee, 'apiKey': api_key, 'sessionId': session.session_id, 'token': token } if not registrations: self.abort(404) return logging.info(str(registrations)) for registration in registrations: self.signal(registration.session_id, call) self.response.write(json.dumps(call))
def save(self, *args, **kwargs): opentok = OpenTok('46764812', 'c6376c5a1c8c5c7c40d97235d852d6d50698e1a3') session = opentok.create_session() self.token = session.generate_token() self.session = session.session_id super(Room, self).save(*args, **kwargs)
def add_ot_session(api_key, api_session): from opentok import OpenTok opentok = OpenTok(api_key, api_session) otsession = opentok.create_session() otsessiondb.insert_one({ "_id": otsession.session_id, "timestamp": time.time(), "status": "open" }) return 200
def get(self, request, *args, **kwargs): opentok = OpenTok('46640082', '27254ec0aeb2fb70021007f55c9dfb26078b727d') session = opentok.create_session() session_id = session.session_id token = session.generate_token() return JsonResponse({ "ar_session_id": session_id, "ar_token": token }, safe=False)
def generate_session_id(): opentok = OpenTok(api_key, api_secret) # Create a session that attempts to send streams directly between clients (falling back # to use the OpenTok TURN server to relay streams if the clients cannot connect): session = opentok.create_session() # Store this session ID in the database session_id = session.session_id # print("generate session:" + session_id) return session_id
def get(self, request, *args, **kwargs): opentok = OpenTok( settings.API_KEY, settings.API_SECRET) session = opentok.create_session( media_mode=MediaModes.routed, archive_mode=ArchiveModes.always) response = { 'session_id': session.session_id, 'api_key': settings.API_KEY, 'token': opentok.generate_token(session.session_id) } return JsonResponse( response, content_type="application/json", safe=False)
def post(self, request, *args, **kwargs): api_key = '47058394' api_secret = '6a0bd6f70f9be22bde0678e0ae81f8d608e5aea1' opentok = OpenTok(api_key, api_secret) session = opentok.create_session() session_id = session.session_id token = opentok.generate_token(session_id) Event.objects.create_token(request.POST['id'], session_id) #logout(request) return HttpResponseRedirect(reverse('induccion_app:entry-lista'))
def join(): content = request.get_json(silent=True) hashid = cgi.escape(content['hashid']) fullName = cgi.escape(content['username']) userId = request.cookies.get('remote_userid') if 'remote_userid' in request.cookies else generate_user_id() userColor = request.cookies.get('remote_usercolor') if 'remote_usercolor' in request.cookies else generate_color() resp = make_response(hashid) # Ensure course exists courseId = DEFAULT_COURSE_PREFIX + hashid course = ndb.Key('Course', courseId).get() # Add user to course key = courseId + userId user = Student.get_or_insert(key, courseId=courseId, studentId=userId, fullName=fullName, color=userColor) userInitials = user.initials user.put() if not user.opentokSessionId: opentok_sdk = OpenTok(OPENTOK_API_KEY, OPENTOK_API_SECRET) # use tokbox server to route media streams; # if you want to use p2p - change media_mode to MediaModes.relayed opentok_session = opentok_sdk.create_session(media_mode = MediaModes.routed) opentok_token = opentok_sdk.generate_token(opentok_session.session_id) user.opentokSessionId = opentok_session.session_id user.opentokToken = opentok_token user.put() teacher = Student.get_teacher_by_course(courseId) # Set user cookies (student role) auth = json.loads(request.cookies.get('remote_auth')) if 'remote_auth' in request.cookies else {} auth[hashid] = { 'role': 'Student', 'opentok_api_key': OPENTOK_API_KEY, 'opentok_session_id': user.opentokSessionId, 'opentok_token': user.opentokToken, 'teacher_session_id': teacher.opentokSessionId, 'teacher_token': teacher.opentokToken } resp.set_cookie('remote_userfullname', fullName) resp.set_cookie('remote_auth', json.dumps(auth)) resp.set_cookie('remote_userid', userId) resp.set_cookie('remote_usercolor', userColor) resp.set_cookie('remote_userinitials', userInitials) configChanged(courseId, 'config', 'users') return resp
def index(): sid = '' sessions = Session.query.all() for s in sessions: if (s.player1 == None or s.player2 == None): sid = s.s_id if sid == '': opentok = OpenTok(api_key, api_secret) session = opentok.create_session() sid = session.session_id unicodedata.normalize('NFKD', sid).encode('ascii', 'ignore') s = Session(s_id=sid, player1=None, player2=None) db.session.add(s) db.session.commit() return redirect('/game/' + sid)
class OpenTokServer: """ OpenTok wrapper. For now only supports a single session """ def __init__(self): config = ConfigParser() config.read('config.ini') opentok_api_key = config.get('tokens', 'opentok_api_key') opentok_api_secret = config.get('tokens', 'opentok_api_secret') self.opentok = OpenTok(opentok_api_key, opentok_api_secret) self.session = None def create_session(self): """ Create an OpenTok session, get session ID and (moderator) token Returns: str, Session ID or None on error """ self.session = self.opentok.create_session( media_mode=MediaModes.routed) return self.session.session_id def get_session_id(self): """ Query session ID if session is active Returns: str, Session ID or None if no session is active """ return self.session.session_id def generate_token(self, username): """ Generate a token for session if active (username stored in metadata) Returns: str, Token or None if no active session """ if self.session is not None: return self.session.generate_token(role=Roles.publisher, data=f"username={username}") else: return None
def admin_class(): if profiledb.find_one({"_id": ObjectId(session["userId"])})["role"] == "admin": query = request.form api_key = '46838544' api_secret = 'c727ac0672a3ae6c1490b68e4032d8e982531450' opentok = OpenTok(api_key, api_secret) video_session = opentok.create_session() classdb.insert_one({ "title": query["title"], "body": query["body"], "session_id": video_session.session_id }) return redirect(url_for('profile', id=profiledb.find_one({"role": "admin"})["_id"])) """Only one endpoint for long polling"""
def get(self): template = JINJA_ENVIRONMENT.get_template('index.html') uid = self.request.get('uid') if uid: opentok = OpenTok(api_key, secret) session = opentok.create_session() registration = Registration(parent=ndb.Key('Registration', 'poc')) registration.uid = uid registration.session_id = str(session.session_id) registration.put() logging.info('Register uid: ' + uid + ' sessionId: ' + session.session_id) self.response.write(template.render({ 'uid': uid, 'apiKey': api_key, 'sessionId': session.session_id, 'token': session.generate_token() })) else: self.response.write(template.render({}))
def add_meeting(request): api_key = "47084204" # Replace with your OpenTok API key. api_secret = "3e487ed7f77e32f86f561244ee1571618a485458" # Replace with your OpenTok API secret. data = request.data opentok = OpenTok(api_key, api_secret) session = opentok.create_session() session_id = session.session_id token = opentok.generate_token(session_id) meeting = Meeting(name=data['name'], session_id=session_id, token=token, start_date=data['start_date'], end_date=data['end_date'], colour=data['colour'], summary=data['summary'], fullday=data['fullday']) meeting.save() return Response(meeting.values())
def get(self): template = JINJA_ENVIRONMENT.get_template('index.html') uid = self.request.get('uid') if uid: opentok = OpenTok(api_key, secret) session = opentok.create_session() registration = Registration(parent=ndb.Key('Registration', 'poc')) registration.uid = uid registration.session_id = str(session.session_id) registration.put() logging.info('Register uid: ' + uid + ' sessionId: ' + session.session_id) self.response.write( template.render({ 'uid': uid, 'apiKey': api_key, 'sessionId': session.session_id, 'token': session.generate_token() })) else: self.response.write(template.render({}))
class OpenTokSessionCreationTest(unittest.TestCase): def setUp(self): self.api_key = u('123456') self.api_secret = u('1234567890abcdef1234567890abcdef1234567890') self.opentok = OpenTok(self.api_key, self.api_secret) @httpretty.activate def test_create_default_session(self): httpretty.register_uri( httpretty.POST, u('https://api.opentok.com/session/create'), body= u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u('text/xml')) session = self.opentok.create_session() validate_jwt_header( self, httpretty.last_request().headers[u('x-opentok-auth')]) expect(httpretty.last_request().headers[u('user-agent')]).to( contain(u('OpenTok-Python-SDK/') + __version__)) body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b('p2p.preference'), [b('enabled')])) expect(body).to(have_key(b('archiveMode'), [b('manual')])) expect(session).to(be_a(Session)) expect(session).to( have_property( u('session_id'), u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg' ))) expect(session).to(have_property(u('media_mode'), MediaModes.relayed)) expect(session).to(have_property(u('location'), None)) @httpretty.activate def test_create_routed_session(self): httpretty.register_uri( httpretty.POST, u('https://api.opentok.com/session/create'), body= u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u('text/xml')) session = self.opentok.create_session(media_mode=MediaModes.routed) validate_jwt_header( self, httpretty.last_request().headers[u('x-opentok-auth')]) expect(httpretty.last_request().headers[u('user-agent')]).to( contain(u('OpenTok-Python-SDK/') + __version__)) body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b('p2p.preference'), [b('disabled')])) expect(body).to(have_key(b('archiveMode'), [b('manual')])) expect(session).to(be_a(Session)) expect(session).to( have_property( u('session_id'), u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg' ))) expect(session).to(have_property(u('media_mode'), MediaModes.routed)) expect(session).to(have_property(u('location'), None)) @httpretty.activate def test_create_session_with_location_hint(self): httpretty.register_uri( httpretty.POST, u('https://api.opentok.com/session/create'), body= u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u('text/xml')) session = self.opentok.create_session(location='12.34.56.78') validate_jwt_header( self, httpretty.last_request().headers[u('x-opentok-auth')]) expect(httpretty.last_request().headers[u('user-agent')]).to( contain(u('OpenTok-Python-SDK/') + __version__)) # ordering of keys is non-deterministic, must parse the body to see if it is correct body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b('location'), [b('12.34.56.78')])) expect(body).to(have_key(b('p2p.preference'), [b('enabled')])) expect(session).to(be_a(Session)) expect(session).to( have_property( u('session_id'), u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg' ))) expect(session).to(have_property(u('media_mode'), MediaModes.relayed)) expect(session).to(have_property(u('location'), u('12.34.56.78'))) @httpretty.activate def test_create_routed_session_with_location_hint(self): httpretty.register_uri( httpretty.POST, u('https://api.opentok.com/session/create'), body= u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u('text/xml')) session = self.opentok.create_session(location='12.34.56.78', media_mode=MediaModes.routed) validate_jwt_header( self, httpretty.last_request().headers[u('x-opentok-auth')]) expect(httpretty.last_request().headers[u('user-agent')]).to( contain(u('OpenTok-Python-SDK/') + __version__)) # ordering of keys is non-deterministic, must parse the body to see if it is correct body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b('location'), [b('12.34.56.78')])) expect(body).to(have_key(b('p2p.preference'), [b('disabled')])) expect(session).to(be_a(Session)) expect(session).to( have_property( u('session_id'), u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg' ))) expect(session).to(have_property(u('media_mode'), MediaModes.routed)) expect(session).to(have_property(u('location'), u('12.34.56.78'))) @httpretty.activate def test_create_manual_archive_mode_session(self): httpretty.register_uri( httpretty.POST, u('https://api.opentok.com/session/create'), body= u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u('text/xml')) session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.manual) validate_jwt_header( self, httpretty.last_request().headers[u('x-opentok-auth')]) expect(httpretty.last_request().headers[u('user-agent')]).to( contain(u('OpenTok-Python-SDK/') + __version__)) body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b('p2p.preference'), [b('disabled')])) expect(body).to(have_key(b('archiveMode'), [b('manual')])) expect(session).to(be_a(Session)) expect(session).to( have_property( u('session_id'), u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg' ))) expect(session).to(have_property(u('media_mode'), MediaModes.routed)) expect(session).to( have_property(u('archive_mode'), ArchiveModes.manual)) @httpretty.activate def test_create_always_archive_mode_session(self): httpretty.register_uri( httpretty.POST, u('https://api.opentok.com/session/create'), body= u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u('text/xml')) session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always) validate_jwt_header( self, httpretty.last_request().headers[u('x-opentok-auth')]) expect(httpretty.last_request().headers[u('user-agent')]).to( contain(u('OpenTok-Python-SDK/') + __version__)) body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b('p2p.preference'), [b('disabled')])) expect(body).to(have_key(b('archiveMode'), [b('always')])) expect(session).to(be_a(Session)) expect(session).to( have_property( u('session_id'), u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg' ))) expect(session).to(have_property(u('media_mode'), MediaModes.routed)) expect(session).to( have_property(u('archive_mode'), ArchiveModes.always)) @httpretty.activate def test_complains_about_always_archive_mode_and_relayed_session(self): httpretty.register_uri( httpretty.POST, u('https://api.opentok.com/session/create'), body= u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u('text/xml')) self.assertRaises(OpenTokException, self.opentok.create_session, media_mode=MediaModes.relayed, archive_mode=ArchiveModes.always)
from flask import Flask, render_template, request, redirect, url_for from opentok import OpenTok, MediaModes, OutputModes from email.utils import formatdate import os, time try: api_key = os.environ['API_KEY'] api_secret = os.environ['API_SECRET'] except Exception: raise Exception( 'You must define API_KEY and API_SECRET environment variables') app = Flask(__name__) opentok = OpenTok(api_key, api_secret) session = opentok.create_session(media_mode=MediaModes.routed) @app.template_filter('datefmt') def datefmt(dt): return formatdate(time.mktime(dt.timetuple())) @app.route("/") def index(): return render_template('index.html') @app.route("/host") def host(): key = api_key session_id = session.session_id
from flask import Flask, render_template from opentok import OpenTok, Roles import time API_KEY = 45961142 API_SECRET = "6e5a8ab70d86f22fec8d030342a193a8da05a1ad" opentok = OpenTok(API_KEY, API_SECRET) # create new session session = opentok.create_session() app=Flask(__name__) @app.route("/") def start(): key = API_KEY session_id = session.session_id token = opentok.generate_token(session_id) token = session.generate_token(role=Roles.publisher) print session_id return render_template('index.html', api_key=key, session_id=session_id, token=token) if __name__ == "__main__": app.debug = True app.run()
try: api_key = os.environ['API_KEY'] api_secret = os.environ['API_SECRET'] except Exception: raise Exception('You must define API_KEY and API_SECRET environment variables') monkey.patch_all() app = Flask(__name__) app.debug=True app.config['SECRET_KEY']='secret!' socketio=SocketIO(app) thread=None opentok = OpenTok(api_key, api_secret) toksession = opentok.create_session() rooms = {} @app.route("/") def display_index(): return render_template("index.html") @app.route("/login", methods=['POST']) def login(): """check if user in db, add to session and redirect to profile if not in db, redirect to index page""" email = request.form.get("email") password = request.form.get("password")
class Application(web.Application): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) try: api_key = os.environ['API_KEY'] api_secret = os.environ['API_SECRET'] except Exception: raise RuntimeError('You must define API_KEY and API_SECRET environment variables') self.opentok = OpenTok(api_key, api_secret) self.sessions = dict() self.users = dict() def user_enter(self, user_id): if user_id not in self.users: self.users[user_id] = [None, False] rv = {'state': self.users[user_id]} if user_id.lower().startswith('dr.'): rv['patients'] = dict((user_id, state) for user_id, state in self.users.items() if not user_id.lower().startswith('dr.')) return rv def _exit_from_session(self, user_id): session_id = self.users[user_id][0] self.users[user_id] = [None, False] if session_id is not None: if user_id in self.sessions[session_id]: self.sessions[session_id][1].pop(user_id) def user_exit(self, user_id): if user_id in self.users: self._exit_from_session(user_id) self.users.pop(user_id) def create_session(self, user_id): if user_id not in self.users: raise web.HTTPNotFound(reason='user_id not found') if not user_id.lower().startswith('dr.'): raise web.HTTPForbidden(reason='patient vcannot create session') self._exit_from_session(user_id) session = self.opentok.create_session() session_id = session.session_id token = self.opentok.generate_token(session_id) self.sessions[session_id] = [session, {}] self.sessions[session_id][1][user_id] = token self.users[user_id] = [session_id, True] return {'api_key': self.opentok.api_key, 'session_id': session_id, 'token': token} def join_to_session(self, user_id, session_id): if user_id not in self.users: raise web.HTTPNotFound(reason='user_id not found') if session_id not in self.sessions: raise web.HTTPNotFound(reason='session_id not found') if not user_id.lower().startswith('dr.'): if session_id != self.users[user_id][0]: raise web.HTTPForbidden(reason='patient cannot join to arbitrary session') self._exit_from_session(user_id) token = self.opentok.generate_token(session_id) self.sessions[session_id][1][user_id] = token self.users[user_id] = [session_id, True] return {'api_key': self.opentok.api_key, 'session_id': session_id, 'token': token} def call_from_session(self, user_id, session_id, addressee): if user_id not in self.users: raise web.HTTPNotFound(reason='user_id not found') if not user_id.lower().startswith('dr.'): raise web.HTTPForbidden(reason='patient cannot call from session') if addressee not in self.users: raise web.HTTPNotFound(reason='addressee not found') self.users[addressee] = [session_id, False]
def generate_opentok_session(): optok = Opentok.objects.filter(mode='development').first() opentok = OpenTok(optok.api_key, optok.api_secret) session = opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always) return session.session_id
def test_timeout(self): opentok = OpenTok(self.api_key, self.api_secret, timeout=1) opentok.create_session()
class OpenTokSessionCreationTest(unittest.TestCase): def setUp(self): self.api_key = u('123456') self.api_secret = u('1234567890abcdef1234567890abcdef1234567890') self.opentok = OpenTok(self.api_key, self.api_secret) @httpretty.activate def test_create_default_session(self): httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session() expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) body = parse_qs(httpretty.last_request().body) expect(body).to.have.key(b('p2p.preference')).being.equal([b('enabled')]) expect(body).to.have.key(b('archiveMode')).being.equal([b('manual')]) expect(session).to.be.a(Session) expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.relayed) expect(session).to.have.property(u('location')).being.equal(None) @httpretty.activate def test_create_routed_session(self): httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session(media_mode=MediaModes.routed) expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) body = parse_qs(httpretty.last_request().body) expect(body).to.have.key(b('p2p.preference')).being.equal([b('disabled')]) expect(body).to.have.key(b('archiveMode')).being.equal([b('manual')]) expect(session).to.be.a(Session) expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed) expect(session).to.have.property(u('location')).being.equal(None) @httpretty.activate def test_create_session_with_location_hint(self): httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session(location='12.34.56.78') expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) # ordering of keys is non-deterministic, must parse the body to see if it is correct body = parse_qs(httpretty.last_request().body) expect(body).to.have.key(b('location')).being.equal([b('12.34.56.78')]) expect(body).to.have.key(b('p2p.preference')).being.equal([b('enabled')]) expect(session).to.be.a(Session) expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.relayed) expect(session).to.have.property(u('location')).being.equal(u('12.34.56.78')) @httpretty.activate def test_create_routed_session_with_location_hint(self): httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session(location='12.34.56.78', media_mode=MediaModes.routed) expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) # ordering of keys is non-deterministic, must parse the body to see if it is correct body = parse_qs(httpretty.last_request().body) expect(body).to.have.key(b('location')).being.equal([b('12.34.56.78')]) expect(body).to.have.key(b('p2p.preference')).being.equal([b('disabled')]) expect(session).to.be.a(Session) expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed) expect(session).to.have.property(u('location')).being.equal(u('12.34.56.78')) @httpretty.activate def test_create_manual_archive_mode_session(self): httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.manual) expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) body = parse_qs(httpretty.last_request().body) expect(body).to.have.key(b('p2p.preference')).being.equal([b('disabled')]) expect(body).to.have.key(b('archiveMode')).being.equal([b('manual')]) expect(session).to.be.a(Session) expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed) expect(session).to.have.property(u('archive_mode')).being.equal(ArchiveModes.manual) @httpretty.activate def test_create_always_archive_mode_session(self): httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always) expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) body = parse_qs(httpretty.last_request().body) expect(body).to.have.key(b('p2p.preference')).being.equal([b('disabled')]) expect(body).to.have.key(b('archiveMode')).being.equal([b('always')]) expect(session).to.be.a(Session) expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed) expect(session).to.have.property(u('archive_mode')).being.equal(ArchiveModes.always) @httpretty.activate def test_complains_about_always_archive_mode_and_relayed_session(self): httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) self.assertRaises(OpenTokException, self.opentok.create_session, media_mode=MediaModes.relayed, archive_mode=ArchiveModes.always)
def test_timeout(self): with self.assertRaises(RequestError): opentok = OpenTok(self.api_key, self.api_secret, timeout=5) opentok.create_session()
class OpenTokSessionCreationTest(unittest.TestCase): def setUp(self): self.api_key = os.environ.get('API_KEY') or u('123456') self.api_secret = (os.environ.get('API_SECRET') or u('1234567890abcdef1234567890abcdef1234567890')) self.api_url = os.environ.get('API_URL') self.mock = not (os.environ.get('API_MOCK') == 'FALSE') if self.mock or self.api_url is None: self.opentok = OpenTok(self.api_key, self.api_secret) else: self.opentok = OpenTok(self.api_key, self.api_secret, api_url = self.api_url) def httpretty_enable(self): if self.mock: httpretty.enable() def httpretty_disable(self): if self.mock: httpretty.disable() def test_create_default_session(self): self.httpretty_enable() httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session() if self.mock: expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) expect(httpretty.last_request().body).to.equal(b('p2p.preference=enabled')) expect(session).to.be.a(Session) if self.mock: expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.relayed) expect(session).to.have.property(u('location')).being.equal(None) self.httpretty_disable() def test_create_routed_session(self): self.httpretty_enable() httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session(media_mode=MediaModes.routed) if self.mock: expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) expect(httpretty.last_request().body).to.equal(b('p2p.preference=disabled')) expect(session).to.be.a(Session) if self.mock: expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed) expect(session).to.have.property(u('location')).being.equal(None) self.httpretty_disable() def test_create_session_with_location_hint(self): self.httpretty_enable() httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session(location='12.34.56.78') if self.mock: expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) # ordering of keys is non-deterministic, must parse the body to see if it is correct body = parse_qs(httpretty.last_request().body) expect(body).to.have.key(b('location')).being.equal([b('12.34.56.78')]) expect(body).to.have.key(b('p2p.preference')).being.equal([b('enabled')]) expect(session).to.be.a(Session) if self.mock: expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.relayed) expect(session).to.have.property(u('location')).being.equal(u('12.34.56.78')) self.httpretty_disable() def test_create_routed_session_with_location_hint(self): self.httpretty_enable() httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), status=200, content_type=u('text/xml')) session = self.opentok.create_session(location='12.34.56.78', media_mode=MediaModes.routed) if self.mock: expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) # ordering of keys is non-deterministic, must parse the body to see if it is correct body = parse_qs(httpretty.last_request().body) expect(body).to.have.key(b('location')).being.equal([b('12.34.56.78')]) expect(body).to.have.key(b('p2p.preference')).being.equal([b('disabled')]) expect(session).to.be.a(Session) if self.mock: expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) expect(session).to.have.property(u('media_mode')).being.equal(MediaModes.routed) expect(session).to.have.property(u('location')).being.equal(u('12.34.56.78')) self.httpretty_disable()
from opentok import OpenTok opentok = OpenTok(45391562, ad29bbb303526b268937e661ae51dfdaf88a8307) # Create a session that attempts to send streams directly between clients (falling back # to use the OpenTok TURN server to relay streams if the clients cannot connect): session = opentok.create_session() from opentok import MediaModes # A session that uses the OpenTok Media Router: session = opentok.create_session(media_mode=MediaModes.routed) # A session with a location hint session = opentok.create_session(location=u'12.34.56.78') # An automatically archived session: session = opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always) # Store this session ID in the database session_id = session.session_id # Generate a Token from just a session_id (fetched from a database) token = opentok.generate_token(session_id) # Generate a Token by calling the method on the Session (returned from create_session) token = session.generate_token() from opentok import Roles # Set some options in a token token = session.generate_token(role=Roles.moderator, expire_time=int(time.time()) + 10, data=u'name=Johnny')
from flask import Flask, render_template, request, redirect, url_for from opentok import OpenTok, MediaModes, OutputModes from email.utils import formatdate import os, time try: api_key = os.environ['API_KEY'] api_secret = os.environ['API_SECRET'] except Exception: raise Exception('You must define API_KEY and API_SECRET environment variables') app = Flask(__name__) opentok = OpenTok(api_key, api_secret) session = opentok.create_session(media_mode=MediaModes.routed) @app.template_filter('datefmt') def datefmt(dt): return formatdate(time.mktime(dt.timetuple())) @app.route("/") def index(): return render_template('index.html') @app.route("/host") def host(): key = api_key session_id = session.session_id token = opentok.generate_token(session_id) return render_template('host.html', api_key=key, session_id=session_id, token=token) @app.route("/participant")
class OpenTokSessionCreationTest(unittest.TestCase): def setUp(self): self.api_key = u("123456") self.api_secret = u("1234567890abcdef1234567890abcdef1234567890") self.opentok = OpenTok(self.api_key, self.api_secret) @httpretty.activate def test_create_default_session(self): httpretty.register_uri( httpretty.POST, u("https://api.opentok.com/session/create"), body=u( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u("text/xml"), ) session = self.opentok.create_session() validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) expect(httpretty.last_request().headers[u("user-agent")]).to(contain(u("OpenTok-Python-SDK/") + __version__)) body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b("p2p.preference"), [b("enabled")])) expect(body).to(have_key(b("archiveMode"), [b("manual")])) expect(session).to(be_a(Session)) expect(session).to( have_property( u("session_id"), u("1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg") ) ) expect(session).to(have_property(u("media_mode"), MediaModes.relayed)) expect(session).to(have_property(u("location"), None)) @httpretty.activate def test_create_routed_session(self): httpretty.register_uri( httpretty.POST, u("https://api.opentok.com/session/create"), body=u( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u("text/xml"), ) session = self.opentok.create_session(media_mode=MediaModes.routed) validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) expect(httpretty.last_request().headers[u("user-agent")]).to(contain(u("OpenTok-Python-SDK/") + __version__)) body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b("p2p.preference"), [b("disabled")])) expect(body).to(have_key(b("archiveMode"), [b("manual")])) expect(session).to(be_a(Session)) expect(session).to( have_property( u("session_id"), u("1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg") ) ) expect(session).to(have_property(u("media_mode"), MediaModes.routed)) expect(session).to(have_property(u("location"), None)) @httpretty.activate def test_create_session_with_location_hint(self): httpretty.register_uri( httpretty.POST, u("https://api.opentok.com/session/create"), body=u( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u("text/xml"), ) session = self.opentok.create_session(location="12.34.56.78") validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) expect(httpretty.last_request().headers[u("user-agent")]).to(contain(u("OpenTok-Python-SDK/") + __version__)) # ordering of keys is non-deterministic, must parse the body to see if it is correct body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b("location"), [b("12.34.56.78")])) expect(body).to(have_key(b("p2p.preference"), [b("enabled")])) expect(session).to(be_a(Session)) expect(session).to( have_property( u("session_id"), u("1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg") ) ) expect(session).to(have_property(u("media_mode"), MediaModes.relayed)) expect(session).to(have_property(u("location"), u("12.34.56.78"))) @httpretty.activate def test_create_routed_session_with_location_hint(self): httpretty.register_uri( httpretty.POST, u("https://api.opentok.com/session/create"), body=u( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u("text/xml"), ) session = self.opentok.create_session(location="12.34.56.78", media_mode=MediaModes.routed) validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) expect(httpretty.last_request().headers[u("user-agent")]).to(contain(u("OpenTok-Python-SDK/") + __version__)) # ordering of keys is non-deterministic, must parse the body to see if it is correct body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b("location"), [b("12.34.56.78")])) expect(body).to(have_key(b("p2p.preference"), [b("disabled")])) expect(session).to(be_a(Session)) expect(session).to( have_property( u("session_id"), u("1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg") ) ) expect(session).to(have_property(u("media_mode"), MediaModes.routed)) expect(session).to(have_property(u("location"), u("12.34.56.78"))) @httpretty.activate def test_create_manual_archive_mode_session(self): httpretty.register_uri( httpretty.POST, u("https://api.opentok.com/session/create"), body=u( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u("text/xml"), ) session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.manual) validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) expect(httpretty.last_request().headers[u("user-agent")]).to(contain(u("OpenTok-Python-SDK/") + __version__)) body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b("p2p.preference"), [b("disabled")])) expect(body).to(have_key(b("archiveMode"), [b("manual")])) expect(session).to(be_a(Session)) expect(session).to( have_property( u("session_id"), u("1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg") ) ) expect(session).to(have_property(u("media_mode"), MediaModes.routed)) expect(session).to(have_property(u("archive_mode"), ArchiveModes.manual)) @httpretty.activate def test_create_always_archive_mode_session(self): httpretty.register_uri( httpretty.POST, u("https://api.opentok.com/session/create"), body=u( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u("text/xml"), ) session = self.opentok.create_session(media_mode=MediaModes.routed, archive_mode=ArchiveModes.always) validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) expect(httpretty.last_request().headers[u("user-agent")]).to(contain(u("OpenTok-Python-SDK/") + __version__)) body = parse_qs(httpretty.last_request().body) expect(body).to(have_key(b("p2p.preference"), [b("disabled")])) expect(body).to(have_key(b("archiveMode"), [b("always")])) expect(session).to(be_a(Session)) expect(session).to( have_property( u("session_id"), u("1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg") ) ) expect(session).to(have_property(u("media_mode"), MediaModes.routed)) expect(session).to(have_property(u("archive_mode"), ArchiveModes.always)) @httpretty.activate def test_complains_about_always_archive_mode_and_relayed_session(self): httpretty.register_uri( httpretty.POST, u("https://api.opentok.com/session/create"), body=u( '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>' ), status=200, content_type=u("text/xml"), ) self.assertRaises( OpenTokException, self.opentok.create_session, media_mode=MediaModes.relayed, archive_mode=ArchiveModes.always, )