Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 478309b

Browse files
authored
Merge pull request #100 from AssemblyAI/niels/streaming-token
Add support for temporary token in streaming transcriber
2 parents 3781868 + b8e3e79 commit 478309b

1 file changed

Lines changed: 37 additions & 8 deletions

File tree

src/main/java/com/assemblyai/api/RealtimeTranscriber.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.assemblyai.api;
22

33
import com.assemblyai.api.core.ObjectMappers;
4+
import com.assemblyai.api.resources.realtime.requests.CreateRealtimeTemporaryTokenParams;
45
import com.assemblyai.api.resources.realtime.types.*;
56
import com.fasterxml.jackson.core.JsonProcessingException;
67

@@ -26,6 +27,7 @@ public final class RealtimeTranscriber implements AutoCloseable {
2627
private static final String BASE_URL = "wss://api.assemblyai.com";
2728
private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder().build();
2829
private final String apiKey;
30+
private final String token;
2931
private final int sampleRate;
3032
private final AudioEncoding encoding;
3133
private final boolean disablePartialTranscripts;
@@ -42,6 +44,7 @@ public final class RealtimeTranscriber implements AutoCloseable {
4244

4345
private RealtimeTranscriber(
4446
String apiKey,
47+
String token,
4548
int sampleRate,
4649
AudioEncoding encoding,
4750
boolean disablePartialTranscripts,
@@ -54,6 +57,7 @@ private RealtimeTranscriber(
5457
Consumer<Throwable> onError,
5558
BiConsumer<Integer, String> onClose) {
5659
this.apiKey = apiKey;
60+
this.token = token;
5761
this.sampleRate = sampleRate;
5862
this.encoding = encoding;
5963
this.disablePartialTranscripts = disablePartialTranscripts;
@@ -86,10 +90,18 @@ public void connect() {
8690
throw new RuntimeException("Failed to serialize word boosts");
8791
}
8892
}
89-
Request request = new Request.Builder()
90-
.url(url)
91-
.addHeader("Authorization", apiKey)
92-
.build();
93+
Request.Builder requestBuilder = new Request.Builder();
94+
95+
if (token != null) {
96+
url += "&token=" + token;
97+
} else if (apiKey != null) {
98+
requestBuilder.addHeader("Authorization", apiKey);
99+
} else {
100+
throw new RuntimeException("API key or Token is required to authenticate.");
101+
}
102+
103+
Request request = requestBuilder.url(url).build();
104+
93105
this.webSocket = OK_HTTP_CLIENT.newWebSocket(request, new Listener(
94106
(response) -> endUtteranceSilenceThreshold.ifPresent(this::configureEndUtteranceSilenceThreshold)
95107
));
@@ -154,6 +166,7 @@ public static final class Builder {
154166
private AudioEncoding encoding;
155167
private boolean disablePartialTranscripts;
156168
private List<String> wordBoost;
169+
private String token;
157170
private Optional<Integer> endUtteranceSilenceThreshold = Optional.empty();
158171
private Consumer<SessionBegins> onSessionBegins;
159172
private Consumer<PartialTranscript> onPartialTranscript;
@@ -163,10 +176,11 @@ public static final class Builder {
163176
private BiConsumer<Integer, String> onClose;
164177

165178
/**
166-
* Sets api key
179+
* Sets the AssemblyAI API key used to authenticate the RealtimeTranscriber
167180
*
168-
* @param apiKey The AssemblyAI API Key
181+
* @param apiKey The AssemblyAI API key
169182
* @return this
183+
* @see #token(String) Or authenticate using a temporary token.
170184
*/
171185
public RealtimeTranscriber.Builder apiKey(String apiKey) {
172186
this.apiKey = apiKey;
@@ -216,6 +230,20 @@ public RealtimeTranscriber.Builder wordBoost(List<String> wordBoost) {
216230
return this;
217231
}
218232

233+
/**
234+
* Sets the temporary token used to authenticate the RealtimeTranscriber
235+
*
236+
* @param token The temporary token used to authenticate the RealtimeTranscriber
237+
* @return this
238+
* @see com.assemblyai.api.resources.realtime.RealtimeClient#createTemporaryToken(CreateRealtimeTemporaryTokenParams)
239+
* Generate a temporary auth token using AssemblyAI.realtime().createTemporaryToken()
240+
* @see #apiKey(String) Or authenticate using your AssemblyAI API key
241+
*/
242+
public RealtimeTranscriber.Builder token(String token) {
243+
this.token = token;
244+
return this;
245+
}
246+
219247
/**
220248
* Configure the threshold for how long to wait before ending an utterance. Default is 700ms.
221249
*
@@ -307,11 +335,12 @@ public RealtimeTranscriber.Builder onClose(BiConsumer<Integer, String> onClose)
307335
}
308336

309337
public RealtimeTranscriber build() {
310-
if (apiKey == null) {
311-
throw new RuntimeException("apiKey must be specified to construct RealtimeTranscriber");
338+
if (apiKey == null && token == null) {
339+
throw new RuntimeException("API key or Token must be specified to construct RealtimeTranscriber");
312340
}
313341
return new RealtimeTranscriber(
314342
apiKey,
343+
token,
315344
sampleRate == null ? DEFAULT_SAMPLE_RATE : sampleRate,
316345
encoding,
317346
disablePartialTranscripts,

0 commit comments

Comments
 (0)