Skip to content

Commit 6bd6461

Browse files
authored
Merge pull request #104 from mukesh-kum/issue-94
Apply builder patter for construction of OpenTok object. fixes #94
2 parents 6f2b60c + c9aa4fb commit 6bd6461

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

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

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,15 @@ public class OpenTok {
6262
* page.)
6363
*/
6464
public OpenTok(int apiKey, String apiSecret) {
65-
this(apiKey, apiSecret, defaultApiUrl);
66-
}
67-
68-
public OpenTok(int apiKey, String apiSecret, String apiUrl) {
69-
this(apiKey, apiSecret, apiUrl, null);
65+
this.apiKey = apiKey;
66+
this.apiSecret = apiSecret.trim();
67+
this.client = new HttpClient.Builder(apiKey, apiSecret).build();
7068
}
71-
72-
public OpenTok(int apiKey, String apiSecret, String apiUrl, Proxy proxy) {
69+
70+
private OpenTok(int apiKey, String apiSecret, HttpClient httpClient) {
7371
this.apiKey = apiKey;
7472
this.apiSecret = apiSecret.trim();
75-
if (apiUrl == null) {
76-
apiUrl = defaultApiUrl;
77-
}
78-
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret)
79-
.apiUrl(apiUrl);
80-
if (proxy != null) {
81-
clientBuilder.proxy(proxy);
82-
}
83-
this.client = clientBuilder.build();
73+
this.client = httpClient;
8474
}
8575

8676
/**
@@ -135,7 +125,7 @@ public OpenTok(int apiKey, String apiSecret, String apiUrl, Proxy proxy) {
135125
*/
136126
public String generateToken(String sessionId, TokenOptions tokenOptions) throws OpenTokException {
137127
List<String> sessionIdParts = null;
138-
if(sessionId == null || sessionId == "") {
128+
if (sessionId == null || sessionId == "") {
139129
throw new InvalidArgumentException("Session not valid");
140130
}
141131

@@ -255,7 +245,7 @@ public Session createSession(SessionProperties properties) throws OpenTokExcepti
255245
String xpathQuery = "/sessions/Session/session_id";
256246

257247
// NOTE: doing this null check twice is kind of ugly
258-
if(properties != null) {
248+
if (properties != null) {
259249
params = properties.toMap();
260250
} else {
261251
params = new SessionProperties.Builder().build().toMap();
@@ -324,7 +314,7 @@ private static String readXml(String xpathQuery, String xml) throws XPathExpress
324314
InputSource source = new InputSource(new StringReader(xml));
325315
return xpath.evaluate(xpathQuery, source);
326316
}
327-
317+
328318
/**
329319
* Gets an {@link Archive} object for the given archive ID.
330320
*
@@ -456,4 +446,39 @@ public Archive stopArchive(String archiveId) throws OpenTokException {
456446
public void deleteArchive(String archiveId) throws OpenTokException {
457447
this.client.deleteArchive(archiveId);
458448
}
449+
450+
public static class Builder {
451+
private int apiKey;
452+
private String apiSecret;
453+
private String apiUrl;
454+
private Proxy proxy;
455+
456+
public Builder(int apiKey, String apiSecret) {
457+
this.apiKey = apiKey;
458+
this.apiSecret = apiSecret;
459+
}
460+
461+
public Builder apiUrl(String apiUrl) {
462+
this.apiUrl = apiUrl;
463+
return this;
464+
}
465+
466+
public Builder proxy(Proxy proxy) {
467+
this.proxy = proxy;
468+
return this;
469+
}
470+
471+
public OpenTok build() {
472+
HttpClient.Builder clientBuilder = new HttpClient.Builder(apiKey, apiSecret);
473+
474+
if (this.apiUrl != null) {
475+
clientBuilder.apiUrl(this.apiUrl);
476+
}
477+
if (this.proxy != null) {
478+
clientBuilder.proxy(this.proxy);
479+
}
480+
481+
return new OpenTok(this.apiKey, this.apiSecret, clientBuilder.build());
482+
}
483+
}
459484
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* OpenTok Java SDK
3+
* Copyright (C) 2016 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.constants;
9+
10+
public class DefaultApiUrl {
11+
public static final String DEFAULT_API_URI = "https://api.opentok.com";
12+
13+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.ning.http.client.filter.FilterException;
2626
import com.ning.http.client.filter.RequestFilter;
2727
import com.opentok.ArchiveProperties;
28+
import com.opentok.constants.DefaultApiUrl;
2829
import com.opentok.constants.Version;
2930
import com.opentok.exception.OpenTokException;
3031
import com.opentok.exception.RequestException;
@@ -311,9 +312,14 @@ public HttpClient build() {
311312
AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder()
312313
.setUserAgent("Opentok-Java-SDK/" + Version.VERSION + " JRE/" + System.getProperty("java.version"))
313314
.addRequestFilter(new PartnerAuthRequestFilter(this.apiKey, this.apiSecret));
315+
if (this.apiUrl == null) {
316+
this.apiUrl=DefaultApiUrl.DEFAULT_API_URI;
317+
}
318+
314319
if (this.proxy != null) {
315320
configBuilder.setProxyServer(createProxyServer(this.proxy));
316321
}
322+
317323
this.config = configBuilder.build();
318324
// NOTE: not thread-safe, config could be modified by another thread here?
319325
HttpClient client = new HttpClient(this);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void setUp() {
9393
apiKey = anApiKey;
9494
apiSecret = anApiSecret;
9595
}
96-
sdk = new OpenTok(apiKey, apiSecret, apiUrl);
96+
sdk = new OpenTok.Builder(apiKey, apiSecret).apiUrl(apiUrl).build();
9797
}
9898

9999
@Test
@@ -868,7 +868,9 @@ public void testCreateSessionWithProxy() throws OpenTokException, UnknownHostExc
868868
String sessionId = "SESSIONID";
869869
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getLocalHost(), proxyingService.port()));
870870

871-
sdk = new OpenTok(apiKey, apiSecret, apiUrl, proxy);
871+
872+
sdk = new OpenTok.Builder(apiKey, apiSecret).apiUrl(targetServiceBaseUrl).proxy(proxy).build();
873+
872874
stubFor(post(urlEqualTo("/session/create"))
873875
.willReturn(aResponse()
874876
.withStatus(200)

0 commit comments

Comments
 (0)