Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into niels/close-properly
  • Loading branch information
Swimburger committed Apr 16, 2024
2 parents 79b5c8c + 478309b commit eecc79a
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/main/java/com/assemblyai/api/RealtimeTranscriber.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.assemblyai.api;

import com.assemblyai.api.core.ObjectMappers;
import com.assemblyai.api.resources.realtime.requests.CreateRealtimeTemporaryTokenParams;
import com.assemblyai.api.resources.realtime.types.*;
import com.fasterxml.jackson.core.JsonProcessingException;

Expand Down Expand Up @@ -28,6 +29,7 @@ public final class RealtimeTranscriber implements AutoCloseable {
private static final String BASE_URL = "wss://api.assemblyai.com";
private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder().build();
private final String apiKey;
private final String token;
private final int sampleRate;
private final AudioEncoding encoding;
private final boolean disablePartialTranscripts;
Expand All @@ -48,6 +50,7 @@ public final class RealtimeTranscriber implements AutoCloseable {

private RealtimeTranscriber(
String apiKey,
String token,
int sampleRate,
AudioEncoding encoding,
boolean disablePartialTranscripts,
Expand All @@ -61,6 +64,7 @@ private RealtimeTranscriber(
Consumer<SessionInformation> onSessionInformation,
BiConsumer<Integer, String> onClose) {
this.apiKey = apiKey;
this.token = token;
this.sampleRate = sampleRate;
this.encoding = encoding;
this.disablePartialTranscripts = disablePartialTranscripts;
Expand Down Expand Up @@ -98,10 +102,18 @@ public void connect() {
throw new RuntimeException("Failed to serialize word boosts");
}
}
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", apiKey)
.build();
Request.Builder requestBuilder = new Request.Builder();

if (token != null) {
url += "&token=" + token;
} else if (apiKey != null) {
requestBuilder.addHeader("Authorization", apiKey);
} else {
throw new RuntimeException("API key or Token is required to authenticate.");
}

Request request = requestBuilder.url(url).build();

this.webSocket = OK_HTTP_CLIENT.newWebSocket(request, new Listener(
(response) -> endUtteranceSilenceThreshold.ifPresent(this::configureEndUtteranceSilenceThreshold)
));
Expand Down Expand Up @@ -184,6 +196,7 @@ public static final class Builder {
private AudioEncoding encoding;
private boolean disablePartialTranscripts;
private List<String> wordBoost;
private String token;
private Optional<Integer> endUtteranceSilenceThreshold = Optional.empty();
private Consumer<SessionBegins> onSessionBegins;
private Consumer<PartialTranscript> onPartialTranscript;
Expand All @@ -194,10 +207,11 @@ public static final class Builder {
private Consumer<SessionInformation> onSessionInformation;

/**
* Sets api key
* Sets the AssemblyAI API key used to authenticate the RealtimeTranscriber
*
* @param apiKey The AssemblyAI API Key
* @param apiKey The AssemblyAI API key
* @return this
* @see #token(String) Or authenticate using a temporary token.
*/
public RealtimeTranscriber.Builder apiKey(String apiKey) {
this.apiKey = apiKey;
Expand Down Expand Up @@ -247,6 +261,20 @@ public RealtimeTranscriber.Builder wordBoost(List<String> wordBoost) {
return this;
}

/**
* Sets the temporary token used to authenticate the RealtimeTranscriber
*
* @param token The temporary token used to authenticate the RealtimeTranscriber
* @return this
* @see com.assemblyai.api.resources.realtime.RealtimeClient#createTemporaryToken(CreateRealtimeTemporaryTokenParams)
* Generate a temporary auth token using AssemblyAI.realtime().createTemporaryToken()
* @see #apiKey(String) Or authenticate using your AssemblyAI API key
*/
public RealtimeTranscriber.Builder token(String token) {
this.token = token;
return this;
}

/**
* Configure the threshold for how long to wait before ending an utterance. Default is 700ms.
*
Expand Down Expand Up @@ -351,11 +379,12 @@ public RealtimeTranscriber.Builder onClose(BiConsumer<Integer, String> onClose)
}

public RealtimeTranscriber build() {
if (apiKey == null) {
throw new RuntimeException("apiKey must be specified to construct RealtimeTranscriber");
if (apiKey == null && token == null) {
throw new RuntimeException("API key or Token must be specified to construct RealtimeTranscriber");
}
return new RealtimeTranscriber(
apiKey,
token,
sampleRate == null ? DEFAULT_SAMPLE_RATE : sampleRate,
encoding,
disablePartialTranscripts,
Expand Down

0 comments on commit eecc79a

Please sign in to comment.