Skip to content

Commit 27b2cec

Browse files
Skip token creation if no authentication is present. closes #440
1 parent e756dc5 commit 27b2cec

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public abstract class WatsonService {
7878
private String endPoint;
7979
private final String name;
8080
protected Headers defaultHeaders = null;
81-
private boolean skipAuthentication = false;
81+
protected boolean skipAuthentication = false;
8282

8383
/** The Constant MESSAGE_CODE. */
8484
protected static final String MESSAGE_CODE = "code";

src/main/java/com/ibm/watson/developer_cloud/speech_to_text/v1/SpeechToText.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -346,21 +346,25 @@ public void recognizeUsingWebSocket(final InputStream audio, final RecognizeOpti
346346
Validator.notNull(options, "options cannot be null");
347347
Validator.notNull(options.contentType(), "options.contentType cannot be null");
348348
Validator.notNull(callback, "callback cannot be null");
349-
350-
351-
getToken().enqueue(new ServiceCallback<String>() {
352-
@Override
353-
public void onFailure(Exception e) {
354-
callback.onError(e);
355-
}
356-
357-
@Override
358-
public void onResponse(String token) {
359-
String url = getEndPoint().replaceFirst("(https|http)", "wss");
360-
WebSocketManager wsManager = new WebSocketManager(url + PATH_RECOGNIZE, configureHttpClient(), defaultHeaders, token);
361-
wsManager.recognize(audio, options, callback);
362-
}
363-
});
364-
349+
350+
final String url = getEndPoint().replaceFirst("(https|http)", "wss");
351+
352+
if (skipAuthentication) {
353+
WebSocketManager wsManager = new WebSocketManager(url + PATH_RECOGNIZE, configureHttpClient(), defaultHeaders, null);
354+
wsManager.recognize(audio, options, callback);
355+
} else {
356+
getToken().enqueue(new ServiceCallback<String>() {
357+
@Override
358+
public void onFailure(Exception e) {
359+
callback.onError(e);
360+
}
361+
362+
@Override
363+
public void onResponse(String token) {
364+
WebSocketManager wsManager = new WebSocketManager(url + PATH_RECOGNIZE, configureHttpClient(), defaultHeaders, token);
365+
wsManager.recognize(audio, options, callback);
366+
}
367+
});
368+
}
365369
}
366370
}

src/main/java/com/ibm/watson/developer_cloud/speech_to_text/v1/websocket/WebSocketManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,12 @@ public WebSocketManager(String url, OkHttpClient client, Headers defaultHeaders,
272272
private WebSocketCall createConnection(RecognizeOptions options) {
273273
String speechModel = options.model() == null ? "" : "?model=" + options.model();
274274
Builder builder = new Request.Builder()
275-
.url(url + speechModel)
276-
.addHeader(HttpHeaders.X_WATSON_AUTHORIZATION_TOKEN, token);
275+
.url(url + speechModel);
277276

277+
if (token != null) {
278+
builder.addHeader(HttpHeaders.X_WATSON_AUTHORIZATION_TOKEN, token);
279+
}
280+
278281
if (defaultHeaders != null) {
279282
for (String key : defaultHeaders.names()) {
280283
builder.header(key, defaultHeaders.get(key));

0 commit comments

Comments
 (0)