Skip to content

Commit 8e70d03

Browse files
committed
implements listArchives, adds tests #11
1 parent 6f853e6 commit 8e70d03

File tree

3 files changed

+173
-15
lines changed

3 files changed

+173
-15
lines changed

src/main/java/com/opentok/OpenTok.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
package com.opentok;
1212

13+
import java.io.IOException;
1314
import java.io.StringReader;
1415
import java.io.UnsupportedEncodingException;
16+
import java.util.ArrayList;
1517
import java.util.Collection;
1618
import java.util.List;
1719
import java.util.Map;
@@ -20,6 +22,11 @@
2022
import javax.xml.xpath.XPathExpressionException;
2123
import javax.xml.xpath.XPathFactory;
2224

25+
import com.fasterxml.jackson.core.JsonParseException;
26+
import com.fasterxml.jackson.core.JsonParser;
27+
import com.fasterxml.jackson.core.JsonProcessingException;
28+
import com.fasterxml.jackson.databind.JsonMappingException;
29+
import com.fasterxml.jackson.databind.type.TypeFactory;
2330
import com.opentok.exception.OpenTokException;
2431
import com.opentok.exception.InvalidArgumentException;
2532
import com.opentok.exception.RequestException;
@@ -324,9 +331,9 @@ public Archive getArchive(String archiveId) throws OpenTokException {
324331
*
325332
* @return A List of {@link Archive} objects.
326333
*/
327-
// public List<Archive> listArchives() throws OpenTokException {
328-
// return listArchives(0, 1000);
329-
// }
334+
public List<Archive> listArchives() throws OpenTokException {
335+
return listArchives(0, 1000);
336+
}
330337

331338
/**
332339
* Returns a List of {@link Archive} objects, representing archives that are both
@@ -337,18 +344,23 @@ public Archive getArchive(String archiveId) throws OpenTokException {
337344
* @param count The number of archives to be returned. The maximum number of archives returned is 1000.
338345
* @return A List of {@link Archive} objects.
339346
*/
340-
// public List<Archive> listArchives(int offset, int count) throws OpenTokException {
341-
// ObjectMapper mapper = new ObjectMapper();
342-
// String archive = HttpClient.makeGetRequest("/v2/partner/" + this.apiKey + "/archive?offset=" + offset + "&count="
343-
// + count);
344-
// try {
345-
// JsonNode node = mapper.readTree(archive);
346-
// return mapper.readValue(node.get("items"), new TypeReference<List<Archive>>() {
347-
// });
348-
// } catch (Exception e) {
349-
// throw new RequestException(500, "Exception mapping json: " + e.getMessage());
350-
// }
351-
// }
347+
public List<Archive> listArchives(int offset, int count) throws OpenTokException {
348+
ObjectMapper mapper = new ObjectMapper();
349+
String archive = this.client.getArchives(offset, count);
350+
try {
351+
JsonParser jp = mapper.getFactory().createParser(archive);
352+
return mapper.readValue(mapper.treeAsTokens(mapper.readTree(jp).get("items")),
353+
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Archive.class));
354+
} catch (JsonMappingException e) {
355+
throw new RequestException("Exception mapping json: " + e.getMessage());
356+
} catch (JsonParseException e) {
357+
throw new RequestException("Exception mapping json: " + e.getMessage());
358+
} catch (JsonProcessingException e) {
359+
throw new RequestException("Exception mapping json: " + e.getMessage());
360+
} catch (IOException e) {
361+
throw new RequestException("Exception mapping json: " + e.getMessage());
362+
}
363+
}
352364

353365
/**
354366
* Starts archiving an OpenTok 2.0 session.

src/main/java/com/opentok/util/HttpClient.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,46 @@ public String getArchive(String archiveId) {
8989
return responseString;
9090
}
9191

92+
public String getArchives(int offset, int count) {
93+
String responseString = null;
94+
Future<Response> request = null;
95+
// TODO: maybe use a StringBuilder?
96+
String url = this.apiUrl+"/v2/partner/"+this.apiKey+"/archive";
97+
if (offset != 0 || count != 1000) {
98+
url += "?";
99+
if (offset != 0) {
100+
url += ("offset=" + Integer.toString(offset));
101+
}
102+
if (count != 1000) {
103+
url += ("count=" + Integer.toString(count));
104+
}
105+
}
106+
107+
try {
108+
request = this.prepareGet(url).execute();
109+
} catch (IOException e) {
110+
// TODO: throw OpenTokException
111+
e.printStackTrace();
112+
}
113+
114+
try {
115+
Response response = request.get();
116+
// TODO: check response code
117+
responseString = response.getResponseBody();
118+
} catch (InterruptedException e) {
119+
// TODO: throw OpenTokException
120+
e.printStackTrace();
121+
} catch (ExecutionException e) {
122+
// TODO: throw OpenTokException
123+
e.printStackTrace();
124+
} catch (IOException e) {
125+
// TODO: throw OpenTokException
126+
e.printStackTrace();
127+
}
128+
129+
return responseString;
130+
}
131+
92132
// protected static String makeDeleteRequest(String resource) throws OpenTokException {
93133
// BoundRequestBuilder get = client.prepareDelete(apiUrl + resource);
94134
// addCommonHeaders(get);

src/test/java/com/opentok/test/OpenTokTest.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.security.NoSuchAlgorithmException;
1212
import java.security.SignatureException;
1313
import java.util.ArrayList;
14+
import java.util.List;
1415
import java.util.Map;
1516

1617
import com.opentok.*;
@@ -361,4 +362,109 @@ public void testGetArchive() throws OpenTokException {
361362

362363
// TODO: test get archive failure scenarios
363364

365+
@Test
366+
public void testListArchives() throws OpenTokException {
367+
368+
stubFor(get(urlEqualTo("/v2/partner/"+this.apiKey+"/archive"))
369+
.willReturn(aResponse()
370+
.withStatus(200)
371+
.withHeader("Content-Type", "application/json")
372+
.withBody("{\n" +
373+
" \"count\" : 6,\n" +
374+
" \"items\" : [ {\n" +
375+
" \"createdAt\" : 1395187930000,\n" +
376+
" \"duration\" : 22,\n" +
377+
" \"id\" : \"ef546c5a-4fd7-4e59-ab3d-f1cfb4148d1d\",\n" +
378+
" \"name\" : \"\",\n" +
379+
" \"partnerId\" : 123456,\n" +
380+
" \"reason\" : \"\",\n" +
381+
" \"sessionId\" : \"SESSIONID\",\n" +
382+
" \"size\" : 2909274,\n" +
383+
" \"status\" : \"available\",\n" +
384+
" \"url\" : \"http://tokbox.com.archive2.s3.amazonaws.com/123456%2Fef546c5" +
385+
"a-4fd7-4e59-ab3d-f1cfb4148d1d%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6" +
386+
"LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n" +
387+
" }, {\n" +
388+
" \"createdAt\" : 1395187910000,\n" +
389+
" \"duration\" : 14,\n" +
390+
" \"id\" : \"5350f06f-0166-402e-bc27-09ba54948512\",\n" +
391+
" \"name\" : \"\",\n" +
392+
" \"partnerId\" : 123456,\n" +
393+
" \"reason\" : \"\",\n" +
394+
" \"sessionId\" : \"SESSIONID\",\n" +
395+
" \"size\" : 1952651,\n" +
396+
" \"status\" : \"available\",\n" +
397+
" \"url\" : \"http://tokbox.com.archive2.s3.amazonaws.com/123456%2F5350f06" +
398+
"f-0166-402e-bc27-09ba54948512%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6" +
399+
"LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n" +
400+
" }, {\n" +
401+
" \"createdAt\" : 1395187836000,\n" +
402+
" \"duration\" : 62,\n" +
403+
" \"id\" : \"f6e7ee58-d6cf-4a59-896b-6d56b158ec71\",\n" +
404+
" \"name\" : \"\",\n" +
405+
" \"partnerId\" : 123456,\n" +
406+
" \"reason\" : \"\",\n" +
407+
" \"sessionId\" : \"SESSIONID\",\n" +
408+
" \"size\" : 8347554,\n" +
409+
" \"status\" : \"available\",\n" +
410+
" \"url\" : \"http://tokbox.com.archive2.s3.amazonaws.com/123456%2Ff6e7ee5" +
411+
"8-d6cf-4a59-896b-6d56b158ec71%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6" +
412+
"LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n" +
413+
" }, {\n" +
414+
" \"createdAt\" : 1395183243000,\n" +
415+
" \"duration\" : 544,\n" +
416+
" \"id\" : \"30b3ebf1-ba36-4f5b-8def-6f70d9986fe9\",\n" +
417+
" \"name\" : \"\",\n" +
418+
" \"partnerId\" : 123456,\n" +
419+
" \"reason\" : \"\",\n" +
420+
" \"sessionId\" : \"SESSIONID\",\n" +
421+
" \"size\" : 78499758,\n" +
422+
" \"status\" : \"available\",\n" +
423+
" \"url\" : \"http://tokbox.com.archive2.s3.amazonaws.com/123456%2F30b3ebf" +
424+
"1-ba36-4f5b-8def-6f70d9986fe9%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6" +
425+
"LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n" +
426+
" }, {\n" +
427+
" \"createdAt\" : 1394396753000,\n" +
428+
" \"duration\" : 24,\n" +
429+
" \"id\" : \"b8f64de1-e218-4091-9544-4cbf369fc238\",\n" +
430+
" \"name\" : \"showtime again\",\n" +
431+
" \"partnerId\" : 123456,\n" +
432+
" \"reason\" : \"\",\n" +
433+
" \"sessionId\" : \"SESSIONID\",\n" +
434+
" \"size\" : 2227849,\n" +
435+
" \"status\" : \"available\",\n" +
436+
" \"url\" : \"http://tokbox.com.archive2.s3.amazonaws.com/123456%2Fb8f64de" +
437+
"1-e218-4091-9544-4cbf369fc238%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6" +
438+
"LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n" +
439+
" }, {\n" +
440+
" \"createdAt\" : 1394321113000,\n" +
441+
" \"duration\" : 1294,\n" +
442+
" \"id\" : \"832641bf-5dbf-41a1-ad94-fea213e59a92\",\n" +
443+
" \"name\" : \"showtime\",\n" +
444+
" \"partnerId\" : 123456,\n" +
445+
" \"reason\" : \"\",\n" +
446+
" \"sessionId\" : \"SESSIONID\",\n" +
447+
" \"size\" : 42165242,\n" +
448+
" \"status\" : \"available\",\n" +
449+
" \"url\" : \"http://tokbox.com.archive2.s3.amazonaws.com/123456%2F832641b" +
450+
"f-5dbf-41a1-ad94-fea213e59a92%2Farchive.mp4?Expires=1395188695&AWSAccessKeyId=AKIAI6" +
451+
"LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n" +
452+
" } ]\n" +
453+
" }")));
454+
455+
List<Archive> archives = sdk.listArchives();
456+
457+
// NOTE: what about archive totalCount (total number of archives for API Key)?
458+
assertNotNull(archives);
459+
assertEquals(6, archives.size());
460+
461+
verify(getRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
462+
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
463+
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
464+
}
465+
466+
// TODO: test list archives with count and offset
467+
468+
// TODO: test list archives failure scenarios
469+
364470
}

0 commit comments

Comments
 (0)