Skip to content

Commit df143ce

Browse files
normanarguetaManik Sachdeva
authored andcommitted
Get archives by session ID (#137)
* Get archives by session ID
1 parent 2908af4 commit df143ce

File tree

3 files changed

+208
-5
lines changed

3 files changed

+208
-5
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ To delete an Archive, you can call the ``opentok.delete_archive(archive_id)`` me
185185
archive.delete()
186186
187187
You can also get a list of all the Archives you've created (up to 1000) with your API Key. This is
188-
done using the ``opentok.list_archives()`` method. There are two optional keyword parameters:
189-
``count`` and ``offset``; they can help you paginate through the results. This method returns an
190-
instance of the ``ArchiveList`` class.
188+
done using the ``opentok.list_archives()`` method. There are three optional keyword parameters:
189+
``count``, ``offset`` and ``session_id``; they can help you paginate through the results and
190+
filter by session ID. This method returns an instance of the ``ArchiveList`` class.
191191

192192
.. code:: python
193193

opentok/opentok.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def get_archive(self, archive_id):
446446
else:
447447
raise RequestError("An unexpected error occurred", response.status_code)
448448

449-
def get_archives(self, offset=None, count=None):
449+
def get_archives(self, offset=None, count=None, session_id=None):
450450
"""Returns an ArchiveList, which is an array of archives that are completed and in-progress,
451451
for your API key.
452452
@@ -455,6 +455,7 @@ def get_archives(self, offset=None, count=None):
455455
the most recent archive. If you do not specify an offset, 0 is used.
456456
:param int: count Optional. The number of archives to be returned. The maximum
457457
number of archives returned is 1000.
458+
:param string: session_id Optional. Used to list archives for a specific session ID.
458459
459460
:rtype: An ArchiveList object, which is an array of Archive objects.
460461
"""
@@ -463,8 +464,14 @@ def get_archives(self, offset=None, count=None):
463464
params['offset'] = offset
464465
if count is not None:
465466
params['count'] = count
467+
if session_id is not None:
468+
params['sessionId'] = session_id
466469

467-
response = requests.get(self.endpoints.archive_url() + "?" + urlencode(params), headers=self.json_headers(), proxies=self.proxies, timeout=self.timeout)
470+
endpoint = self.endpoints.archive_url() + "?" + urlencode(params)
471+
472+
response = requests.get(
473+
endpoint, headers=self.json_headers(), proxies=self.proxies, timeout=self.timeout
474+
)
468475

469476
if response.status_code < 300:
470477
return ArchiveList(self, response.json())
@@ -475,6 +482,13 @@ def get_archives(self, offset=None, count=None):
475482
else:
476483
raise RequestError("An unexpected error occurred", response.status_code)
477484

485+
def list_archives(self, offset=None, count=None, session_id=None):
486+
"""
487+
New method to get archive list, it's alternative to 'get_archives()',
488+
both methods exist to have backwards compatible
489+
"""
490+
return self.get_archives(offset, count, session_id)
491+
478492
def signal(self, session_id, payload, connection_id=None):
479493
"""
480494
Send signals to all participants in an active OpenTok session or to a specific client

tests/test_archive_api.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,195 @@ def test_find_archives_with_offset_and_count(self):
780780
expect(list(archive_list.items)).to(have_length(4))
781781
# TODO: we could inspect each item in the list
782782

783+
@httpretty.activate
784+
def test_find_archives_with_sessionid(self):
785+
""" Test get_archives method using session_id parameter """
786+
httpretty.register_uri(
787+
httpretty.GET,
788+
u('https://api.opentok.com/v2/project/{0}/archive').format(self.api_key),
789+
body=textwrap.dedent(u("""\
790+
{
791+
"count" : 4,
792+
"items" : [ {
793+
"createdAt" : 1395187836000,
794+
"duration" : 62,
795+
"id" : "f6e7ee58-d6cf-4a59-896b-6d56b158ec71",
796+
"name" : "",
797+
"partnerId" : 123456,
798+
"reason" : "",
799+
"sessionId" : "SESSIONID",
800+
"size" : 8347554,
801+
"status" : "available",
802+
"hasAudio": true,
803+
"hasVideo": true,
804+
"url" : "http://tokbox.com.archive2.s3.amazonaws.com/123456%2Ff6e7ee58-d6cf-4a59-896b-6d56b158ec71%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
805+
}, {
806+
"createdAt" : 1395183243000,
807+
"duration" : 544,
808+
"id" : "30b3ebf1-ba36-4f5b-8def-6f70d9986fe9",
809+
"name" : "",
810+
"partnerId" : 123456,
811+
"reason" : "",
812+
"sessionId" : "SESSIONID",
813+
"size" : 78499758,
814+
"status" : "available",
815+
"hasAudio": true,
816+
"hasVideo": true,
817+
"url" : "http://tokbox.com.archive2.s3.amazonaws.com/123456%2F30b3ebf1-ba36-4f5b-8def-6f70d9986fe9%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
818+
}, {
819+
"createdAt" : 1394396753000,
820+
"duration" : 24,
821+
"id" : "b8f64de1-e218-4091-9544-4cbf369fc238",
822+
"name" : "showtime again",
823+
"partnerId" : 123456,
824+
"reason" : "",
825+
"sessionId" : "SESSIONID",
826+
"size" : 2227849,
827+
"status" : "available",
828+
"hasAudio": true,
829+
"hasVideo": true,
830+
"url" : "http://tokbox.com.archive2.s3.amazonaws.com/123456%2Fb8f64de1-e218-4091-9544-4cbf369fc238%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
831+
}, {
832+
"createdAt" : 1394321113000,
833+
"duration" : 1294,
834+
"id" : "832641bf-5dbf-41a1-ad94-fea213e59a92",
835+
"name" : "showtime",
836+
"partnerId" : 123456,
837+
"reason" : "",
838+
"sessionId" : "SESSIONID",
839+
"size" : 42165242,
840+
"status" : "available",
841+
"hasAudio": true,
842+
"hasVideo": true,
843+
"url" : "http://tokbox.com.archive2.s3.amazonaws.com/123456%2F832641bf-5dbf-41a1-ad94-fea213e59a92%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
844+
}]
845+
}""")),
846+
status=200,
847+
content_type=u('application/json'))
848+
849+
archive_list = self.opentok.get_archives(session_id='SESSIONID')
850+
851+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
852+
expect(httpretty.last_request().headers[u('user-agent')]).to(contain(
853+
u('OpenTok-Python-SDK/')+__version__))
854+
expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json')))
855+
expect(httpretty.last_request()).to(have_property(u('querystring'), {
856+
u('sessionId'): [u('SESSIONID')]
857+
}))
858+
expect(archive_list).to(be_an(ArchiveList))
859+
expect(archive_list).to(have_property(u('count'), 4))
860+
expect(list(archive_list.items)).to(have_length(4))
861+
862+
@httpretty.activate
863+
def test_find_archives_with_offset_count_sessionId(self):
864+
""" Test get_archives method using all parameters: offset, count and sessionId """
865+
httpretty.register_uri(
866+
httpretty.GET,
867+
u('https://api.opentok.com/v2/project/{0}/archive').format(self.api_key),
868+
body=textwrap.dedent(u("""\
869+
{
870+
"count" : 2,
871+
"items" : [ {
872+
"createdAt" : 1394396753000,
873+
"duration" : 24,
874+
"id" : "b8f64de1-e218-4091-9544-4cbf369fc238",
875+
"name" : "showtime again",
876+
"partnerId" : 123456,
877+
"reason" : "",
878+
"sessionId" : "SESSIONID",
879+
"size" : 2227849,
880+
"status" : "available",
881+
"hasAudio": true,
882+
"hasVideo": true,
883+
"url" : "http://tokbox.com.archive2.s3.amazonaws.com/123456%2Fb8f64de1-e218-4091-9544-4cbf369fc238%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
884+
}, {
885+
"createdAt" : 1394321113000,
886+
"duration" : 1294,
887+
"id" : "832641bf-5dbf-41a1-ad94-fea213e59a92",
888+
"name" : "showtime",
889+
"partnerId" : 123456,
890+
"reason" : "",
891+
"sessionId" : "SESSIONID",
892+
"size" : 42165242,
893+
"status" : "available",
894+
"hasAudio": true,
895+
"hasVideo": true,
896+
"url" : "http://tokbox.com.archive2.s3.amazonaws.com/123456%2F832641bf-5dbf-41a1-ad94-fea213e59a92%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
897+
}]
898+
}""")),
899+
status=200,
900+
content_type=u('application/json'))
901+
902+
archive_list = self.opentok.get_archives(offset=2, count=2, session_id='SESSIONID')
903+
904+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
905+
expect(httpretty.last_request().headers[u('user-agent')]).to(contain(
906+
u('OpenTok-Python-SDK/')+__version__))
907+
expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json')))
908+
expect(httpretty.last_request()).to(have_property(u('querystring'), {
909+
u('offset'): [u('2')],
910+
u('count'): [u('2')],
911+
u('sessionId'): [u('SESSIONID')]
912+
}))
913+
expect(archive_list).to(be_an(ArchiveList))
914+
expect(archive_list).to(have_property(u('count'), 2))
915+
expect(list(archive_list.items)).to(have_length(2))
916+
917+
@httpretty.activate
918+
def test_find_archives_alternative_method(self):
919+
""" Test list_archives method using all parameters: offset, count and sessionId """
920+
httpretty.register_uri(
921+
httpretty.GET,
922+
u('https://api.opentok.com/v2/project/{0}/archive').format(self.api_key),
923+
body=textwrap.dedent(u("""\
924+
{
925+
"count" : 2,
926+
"items" : [ {
927+
"createdAt" : 1394396753000,
928+
"duration" : 24,
929+
"id" : "b8f64de1-e218-4091-9544-4cbf369fc238",
930+
"name" : "showtime again",
931+
"partnerId" : 123456,
932+
"reason" : "",
933+
"sessionId" : "SESSIONID",
934+
"size" : 2227849,
935+
"status" : "available",
936+
"hasAudio": true,
937+
"hasVideo": true,
938+
"url" : "http://tokbox.com.archive2.s3.amazonaws.com/123456%2Fb8f64de1-e218-4091-9544-4cbf369fc238%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
939+
}, {
940+
"createdAt" : 1394321113000,
941+
"duration" : 1294,
942+
"id" : "832641bf-5dbf-41a1-ad94-fea213e59a92",
943+
"name" : "showtime",
944+
"partnerId" : 123456,
945+
"reason" : "",
946+
"sessionId" : "SESSIONID",
947+
"size" : 42165242,
948+
"status" : "available",
949+
"hasAudio": true,
950+
"hasVideo": true,
951+
"url" : "http://tokbox.com.archive2.s3.amazonaws.com/123456%2F832641bf-5dbf-41a1-ad94-fea213e59a92%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
952+
}]
953+
}""")),
954+
status=200,
955+
content_type=u('application/json'))
956+
957+
archive_list = self.opentok.list_archives(offset=2, count=2, session_id='SESSIONID')
958+
959+
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
960+
expect(httpretty.last_request().headers[u('user-agent')]).to(contain(
961+
u('OpenTok-Python-SDK/')+__version__))
962+
expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json')))
963+
expect(httpretty.last_request()).to(have_property(u('querystring'), {
964+
u('offset'): [u('2')],
965+
u('count'): [u('2')],
966+
u('sessionId'): [u('SESSIONID')]
967+
}))
968+
expect(archive_list).to(be_an(ArchiveList))
969+
expect(archive_list).to(have_property(u('count'), 2))
970+
expect(list(archive_list.items)).to(have_length(2))
971+
783972
@httpretty.activate
784973
def test_find_paused_archive(self):
785974
archive_id = u('f6e7ee58-d6cf-4a59-896b-6d56b158ec71')

0 commit comments

Comments
 (0)