Skip to content

Commit cf3c6ff

Browse files
committed
add stopArchive implementation, adds tests #11
1 parent 4a18251 commit cf3c6ff

File tree

3 files changed

+71
-13
lines changed

3 files changed

+71
-13
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,16 @@ public Archive startArchive(String sessionId, String name) throws OpenTokExcepti
400400
* @param archiveId The archive ID of the archive you want to stop recording.
401401
* @return The Archive object corresponding to the archive being STOPPED.
402402
*/
403-
// public Archive stopArchive(String archiveId) throws OpenTokException {
404-
// HashMap<String, String> headers = new HashMap<String, String>();
405-
// headers.put("content-type", "application/json");
406-
// String archive = HttpClient.makePostRequest("/v2/partner/" + this.apiKey + "/archive/" + archiveId + "/stop", headers, null,
407-
// "");
408-
// ObjectMapper mapper = new ObjectMapper();
409-
// try {
410-
// return mapper.readValue(archive, Archive.class);
411-
// } catch (Exception e) {
412-
// throw new RequestException(500, "Exception mapping json: " + e.getMessage());
413-
// }
414-
// }
403+
public Archive stopArchive(String archiveId) throws OpenTokException {
404+
405+
String archive = this.client.stopArchive(archiveId);
406+
ObjectMapper mapper = new ObjectMapper();
407+
try {
408+
return mapper.readValue(archive, Archive.class);
409+
} catch (Exception e) {
410+
throw new RequestException("Exception mapping json: " + e.getMessage());
411+
}
412+
}
415413

416414
/**
417415
* Deletes an OpenTok archive.

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public String startArchive(String sessionId, String name) {
139139
// TODO: maybe use a StringBuilder?
140140
String url = this.apiUrl+"/v2/partner/"+this.apiKey+"/archive";
141141

142-
// TODO: create JSON body string from sessionId and name
143142
ObjectMapper mapper = new ObjectMapper();
144143
HashMap<String, String> jsonBody = new HashMap<String, String>();
145144
jsonBody.put("sessionId", sessionId);
@@ -179,6 +178,36 @@ public String startArchive(String sessionId, String name) {
179178
return responseString;
180179
}
181180

181+
public String stopArchive(String archiveId) {
182+
String responseString = null;
183+
Future<Response> request = null;
184+
// TODO: maybe use a StringBuilder?
185+
String url = this.apiUrl+"/v2/partner/"+this.apiKey+"/archive/stop";
186+
187+
try {
188+
request = this.preparePost(url).execute();
189+
} catch (IOException e) {
190+
// TODO: throw OpenTokException
191+
e.printStackTrace();
192+
}
193+
194+
try {
195+
Response response = request.get();
196+
// TODO: check response code
197+
responseString = response.getResponseBody();
198+
} catch (InterruptedException e) {
199+
// TODO: throw OpenTokException
200+
e.printStackTrace();
201+
} catch (ExecutionException e) {
202+
// TODO: throw OpenTokException
203+
e.printStackTrace();
204+
} catch (IOException e) {
205+
// TODO: throw OpenTokException
206+
e.printStackTrace();
207+
}
208+
return responseString;
209+
}
210+
182211
// protected static String makeDeleteRequest(String resource) throws OpenTokException {
183212
// BoundRequestBuilder get = client.prepareDelete(apiUrl + resource);
184213
// addCommonHeaders(get);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,4 +502,35 @@ public void testStartArchive() throws OpenTokException {
502502

503503
// TODO: test start archive failure scenarios
504504

505+
@Test
506+
public void testStopArchive() throws OpenTokException {
507+
String archiveId = "ARCHIVEID";
508+
509+
stubFor(post(urlEqualTo("/v2/partner/"+this.apiKey+"/archive/stop"))
510+
.willReturn(aResponse()
511+
.withStatus(200)
512+
.withHeader("Content-Type", "application/json")
513+
.withBody("{\n" +
514+
" \"createdAt\" : 1395183243000,\n" +
515+
" \"duration\" : 0,\n" +
516+
" \"id\" : \"ARCHIVEID\",\n" +
517+
" \"name\" : \"\",\n" +
518+
" \"partnerId\" : 123456,\n" +
519+
" \"reason\" : \"\",\n" +
520+
" \"sessionId\" : \"SESSIONID\",\n" +
521+
" \"size\" : 0,\n" +
522+
" \"status\" : \"stopped\",\n" +
523+
" \"url\" : null\n" +
524+
" }")));
525+
526+
Archive archive = sdk.stopArchive(archiveId);
527+
assertNotNull(archive);
528+
assertEquals("SESSIONID", archive.getSessionId());
529+
assertEquals(archiveId, archive.getId());
530+
531+
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive/stop"))
532+
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
533+
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
534+
}
535+
505536
}

0 commit comments

Comments
 (0)