Skip to content

Commit 82eaed7

Browse files
committed
adds ArchiveList class, backwards compatible, fixes #29
1 parent 06f3e5b commit 82eaed7

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2015 TokBox, Inc.
4+
* http://www.tokbox.com
5+
*
6+
* Licensed under The MIT License (MIT). See LICENSE file for more information.
7+
*/
8+
package com.opentok;
9+
10+
import com.fasterxml.jackson.annotation.JsonFormat;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
/**
16+
* Represents an list of archives of OpenTok session(s).
17+
*/
18+
@JsonFormat(shape= JsonFormat.Shape.OBJECT)
19+
public class ArchiveList extends ArrayList<Archive> {
20+
21+
private int totalCount;
22+
23+
/**
24+
* The total number of Archives for the API Key.
25+
*/
26+
public int getTotalCount() {
27+
return totalCount;
28+
}
29+
30+
private void setItems(List<Archive> archives) {
31+
this.addAll(archives);
32+
}
33+
34+
private void setCount(int count) {
35+
this.totalCount = count;
36+
}
37+
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.io.IOException;
1111
import java.io.StringReader;
1212
import java.io.UnsupportedEncodingException;
13-
import java.util.ArrayList;
1413
import java.util.Collection;
1514
import java.util.List;
1615
import java.util.Map;
@@ -20,10 +19,8 @@
2019
import javax.xml.xpath.XPathFactory;
2120

2221
import com.fasterxml.jackson.core.JsonParseException;
23-
import com.fasterxml.jackson.core.JsonParser;
2422
import com.fasterxml.jackson.core.JsonProcessingException;
2523
import com.fasterxml.jackson.databind.JsonMappingException;
26-
import com.fasterxml.jackson.databind.type.TypeFactory;
2724
import com.opentok.exception.OpenTokException;
2825
import com.opentok.exception.InvalidArgumentException;
2926
import com.opentok.exception.RequestException;
@@ -51,6 +48,8 @@ public class OpenTok {
5148
protected HttpClient client;
5249
static protected ObjectReader archiveReader = new ObjectMapper()
5350
.reader(Archive.class);
51+
static protected ObjectReader archiveListReader = new ObjectMapper()
52+
.reader(ArchiveList.class);
5453

5554
/**
5655
* Creates an OpenTok object.
@@ -338,7 +337,7 @@ public Archive getArchive(String archiveId) throws OpenTokException {
338337
*
339338
* @return A List of {@link Archive} objects.
340339
*/
341-
public List<Archive> listArchives() throws OpenTokException {
340+
public ArchiveList listArchives() throws OpenTokException {
342341
return listArchives(0, 1000);
343342
}
344343

@@ -353,13 +352,10 @@ public List<Archive> listArchives() throws OpenTokException {
353352
* is 1000.
354353
* @return A List of {@link Archive} objects.
355354
*/
356-
public List<Archive> listArchives(int offset, int count) throws OpenTokException {
357-
ObjectMapper mapper = new ObjectMapper();
358-
String archive = this.client.getArchives(offset, count);
355+
public ArchiveList listArchives(int offset, int count) throws OpenTokException {
356+
String archives = this.client.getArchives(offset, count);
359357
try {
360-
JsonParser jp = mapper.getFactory().createParser(archive);
361-
return mapper.readValue(mapper.treeAsTokens(mapper.readTree(jp).get("items")),
362-
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Archive.class));
358+
return archiveListReader.readValue(archives);
363359

364360
// if we only wanted Java 7 and above, we could DRY this into one catch clause
365361
} catch (JsonMappingException e) {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.security.NoSuchAlgorithmException;
1313
import java.security.SignatureException;
1414
import java.util.ArrayList;
15-
import java.util.List;
1615
import java.util.Map;
1716

1817
import com.opentok.*;
@@ -27,6 +26,8 @@
2726
import org.junit.Test;
2827
import static org.junit.Assert.*;
2928

29+
import static org.hamcrest.CoreMatchers.*;
30+
3031
import static com.github.tomakehurst.wiremock.client.WireMock.*;
3132
import com.github.tomakehurst.wiremock.junit.WireMockRule;
3233

@@ -371,7 +372,7 @@ public void testListArchives() throws OpenTokException {
371372
.withStatus(200)
372373
.withHeader("Content-Type", "application/json")
373374
.withBody("{\n" +
374-
" \"count\" : 6,\n" +
375+
" \"count\" : 60,\n" +
375376
" \"items\" : [ {\n" +
376377
" \"createdAt\" : 1395187930000,\n" +
377378
" \"duration\" : 22,\n" +
@@ -453,11 +454,13 @@ public void testListArchives() throws OpenTokException {
453454
" } ]\n" +
454455
" }")));
455456

456-
List<Archive> archives = sdk.listArchives();
457+
ArchiveList archives = sdk.listArchives();
457458

458-
// NOTE: what about archive totalCount (total number of archives for API Key)?
459459
assertNotNull(archives);
460460
assertEquals(6, archives.size());
461+
assertEquals(60, archives.getTotalCount());
462+
assertThat(archives.get(0), instanceOf(Archive.class));
463+
assertEquals("ef546c5a-4fd7-4e59-ab3d-f1cfb4148d1d", archives.get(0).getId());
461464

462465
verify(getRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
463466
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))

0 commit comments

Comments
 (0)