Skip to content

Commit 4a18251

Browse files
committed
add startArchive implementation, adds tests #11
1 parent 8e70d03 commit 4a18251

File tree

3 files changed

+98
-15
lines changed

3 files changed

+98
-15
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -377,21 +377,19 @@ public List<Archive> listArchives(int offset, int count) throws OpenTokException
377377
*
378378
* @return The Archive object. This object includes properties defining the archive, including the archive ID.
379379
*/
380-
// public Archive startArchive(String sessionId, String name) throws OpenTokException {
381-
// if (sessionId == null || sessionId == "") {
382-
// throw new InvalidArgumentException("Session not valid");
383-
// }
384-
// HashMap<String, String> headers = new HashMap<String, String>();
385-
// headers.put("content-type", "application/json");
386-
// String archive = HttpClient.makePostRequest("/v2/partner/" + this.apiKey + "/archive", headers, null,
387-
// "{ \"sessionId\" : \"" + sessionId + "\", \"name\": \"" + name + "\" }");
388-
// ObjectMapper mapper = new ObjectMapper();
389-
// try {
390-
// return mapper.readValue(archive, Archive.class);
391-
// } catch (Exception e) {
392-
// throw new RequestException(500, "Exception mapping json: " + e.getMessage());
393-
// }
394-
// }
380+
public Archive startArchive(String sessionId, String name) throws OpenTokException {
381+
if (sessionId == null || sessionId == "") {
382+
throw new InvalidArgumentException("Session not valid");
383+
}
384+
// TODO: do validation on sessionId and name
385+
String archive = this.client.startArchive(sessionId, name);
386+
ObjectMapper mapper = new ObjectMapper();
387+
try {
388+
return mapper.readValue(archive, Archive.class);
389+
} catch (Exception e) {
390+
throw new RequestException("Exception mapping json: " + e.getMessage());
391+
}
392+
}
395393

396394
/**
397395
* Stops an OpenTok archive that is being recorded.

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import java.io.IOException;
44
import java.util.Collection;
5+
import java.util.HashMap;
56
import java.util.Map;
67
import java.util.concurrent.ExecutionException;
78
import java.util.concurrent.Future;
89

10+
import com.fasterxml.jackson.core.JsonProcessingException;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
912
import com.ning.http.client.*;
1013
import com.ning.http.client.filter.FilterContext;
1114
import com.ning.http.client.filter.FilterException;
@@ -129,6 +132,53 @@ public String getArchives(int offset, int count) {
129132
return responseString;
130133
}
131134

135+
public String startArchive(String sessionId, String name) {
136+
String responseString = null;
137+
Future<Response> request = null;
138+
String requestBody = null;
139+
// TODO: maybe use a StringBuilder?
140+
String url = this.apiUrl+"/v2/partner/"+this.apiKey+"/archive";
141+
142+
// TODO: create JSON body string from sessionId and name
143+
ObjectMapper mapper = new ObjectMapper();
144+
HashMap<String, String> jsonBody = new HashMap<String, String>();
145+
jsonBody.put("sessionId", sessionId);
146+
if (name != null) {
147+
jsonBody.put("name", name);
148+
}
149+
try {
150+
requestBody = mapper.writeValueAsString(jsonBody);
151+
} catch (JsonProcessingException e) {
152+
// TODO: throw OpenTokException
153+
e.printStackTrace();
154+
}
155+
try {
156+
request = this.preparePost(url)
157+
.setBody(requestBody)
158+
.setHeader("Content-Type", "application/json")
159+
.execute();
160+
} catch (IOException e) {
161+
// TODO: throw OpenTokException
162+
e.printStackTrace();
163+
}
164+
165+
try {
166+
Response response = request.get();
167+
// TODO: check response code
168+
responseString = response.getResponseBody();
169+
} catch (InterruptedException e) {
170+
// TODO: throw OpenTokException
171+
e.printStackTrace();
172+
} catch (ExecutionException e) {
173+
// TODO: throw OpenTokException
174+
e.printStackTrace();
175+
} catch (IOException e) {
176+
// TODO: throw OpenTokException
177+
e.printStackTrace();
178+
}
179+
return responseString;
180+
}
181+
132182
// protected static String makeDeleteRequest(String resource) throws OpenTokException {
133183
// BoundRequestBuilder get = client.prepareDelete(apiUrl + resource);
134184
// addCommonHeaders(get);

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,39 @@ public void testListArchives() throws OpenTokException {
467467

468468
// TODO: test list archives failure scenarios
469469

470+
@Test
471+
public void testStartArchive() throws OpenTokException {
472+
String sessionId = "SESSIONID";
473+
474+
stubFor(post(urlEqualTo("/v2/partner/"+this.apiKey+"/archive"))
475+
.willReturn(aResponse()
476+
.withStatus(200)
477+
.withHeader("Content-Type", "application/json")
478+
.withBody("{\n" +
479+
" \"createdAt\" : 1395183243556,\n" +
480+
" \"duration\" : 0,\n" +
481+
" \"id\" : \"30b3ebf1-ba36-4f5b-8def-6f70d9986fe9\",\n" +
482+
" \"name\" : \"\",\n" +
483+
" \"partnerId\" : 123456,\n" +
484+
" \"reason\" : \"\",\n" +
485+
" \"sessionId\" : \"SESSIONID\",\n" +
486+
" \"size\" : 0,\n" +
487+
" \"status\" : \"started\",\n" +
488+
" \"url\" : null\n" +
489+
" }")));
490+
491+
Archive archive = sdk.startArchive(sessionId, null);
492+
assertNotNull(archive);
493+
assertEquals(sessionId, archive.getSessionId());
494+
assertNotNull(archive.getId());
495+
496+
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
497+
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
498+
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
499+
}
500+
501+
// TODO: test start archive with name
502+
503+
// TODO: test start archive failure scenarios
504+
470505
}

0 commit comments

Comments
 (0)