Skip to content

Commit

Permalink
Add Generic getResponse method for LeMUR
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger committed Jun 17, 2024
1 parent 27de673 commit 3293359
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
1 change: 1 addition & 0 deletions .fernignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Specify files that shouldn't be modified by Fern
src/main/java/com/assemblyai/api/AssemblyAI.java
src/main/java/com/assemblyai/api/PollingTranscriptsClient.java
src/main/java/com/assemblyai/api/EnhancedLemurClient.java
src/main/java/com/assemblyai/api/Transcriber.java
src/main/java/com/assemblyai/api/RealtimeTranscriber.java
src/main/java/com/assemblyai/api/core/Constants.java
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ publishing {
maven(MavenPublication) {
groupId = 'com.assemblyai'
artifactId = 'assemblyai-java'
version = '1.2.0'
version = '2.0.0'
from components.java
pom {
scm {
Expand Down
4 changes: 2 additions & 2 deletions sample-app/src/main/java/sample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public static void main(String... args) throws IOException, InterruptedException

System.out.println("Summary: " + response.getResponse());

LemurResponse response2 = client.lemur().getResponse(response.getRequestId());
LemurTaskResponse response2 = client.lemur().getResponse(response.getRequestId(), LemurTaskResponse.class);

System.out.println("Summary 2: " + ((LemurTaskResponse)response2.get()).getResponse());
System.out.println("Summary 2: " + response2.getResponse());

transcript = client.transcripts().delete(transcript.getId());
System.out.println("Delete transcript. " + transcript);
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/assemblyai/api/AssemblyAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,18 @@ public class AssemblyAI {

protected final Supplier<RealtimeClient> realtimeClient;

protected final Supplier<LemurClient> lemurClient;
protected final Supplier<EnhancedLemurClient> lemurClient;

public AssemblyAI(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
this.filesClient = Suppliers.memoize(() -> new FilesClient(clientOptions));
this.transcriptClient = Suppliers.memoize(() -> new PollingTranscriptsClient(clientOptions, this));
this.realtimeClient = Suppliers.memoize(() -> new RealtimeClient(clientOptions));
this.lemurClient = Suppliers.memoize(() -> new LemurClient(clientOptions));
this(clientOptions, clientOptions);
}

public AssemblyAI(ClientOptions clientOptions, ClientOptions lemurClientOptions) {
this.clientOptions = clientOptions;
this.filesClient = Suppliers.memoize(() -> new FilesClient(clientOptions));
this.transcriptClient = Suppliers.memoize(() -> new PollingTranscriptsClient(clientOptions, this));
this.realtimeClient = Suppliers.memoize(() -> new RealtimeClient(clientOptions));
this.lemurClient = Suppliers.memoize(() -> new LemurClient(lemurClientOptions));
this.lemurClient = Suppliers.memoize(() -> new EnhancedLemurClient(lemurClientOptions));
}

public FilesClient files() {
Expand All @@ -50,7 +46,7 @@ public RealtimeClient realtime() {
return this.realtimeClient.get();
}

public LemurClient lemur() {
public EnhancedLemurClient lemur() {
return this.lemurClient.get();
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/com/assemblyai/api/EnhancedLemurClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.assemblyai.api;

import com.assemblyai.api.core.ApiError;
import com.assemblyai.api.core.ClientOptions;
import com.assemblyai.api.core.ObjectMappers;
import com.assemblyai.api.core.RequestOptions;
import com.assemblyai.api.resources.lemur.LemurClient;
import com.assemblyai.api.resources.lemur.types.ILemurBaseResponse;
import okhttp3.*;

import java.io.IOException;

public final class EnhancedLemurClient extends LemurClient {
public EnhancedLemurClient(ClientOptions clientOptions) {
super(clientOptions);
}

/**
* Retrieve a LeMUR response that was previously generated.
*/
public <T extends ILemurBaseResponse> T getResponse(String requestId, Class<T> responseType) {
return this.getResponse(requestId, responseType, null);
}

/**
* Retrieve a LeMUR response that was previously generated.
*/
public <T extends ILemurBaseResponse> T getResponse(String requestId, Class<T> responseType, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("lemur/v3")
.addPathSegment(requestId)
.build();
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("GET", null)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.build();
try {
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
Response response = client.newCall(okhttpRequest).execute();
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), responseType);
}
throw new ApiError(
response.code(),
ObjectMappers.JSON_MAPPER.readValue(
responseBody != null ? responseBody.string() : "{}", Object.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/assemblyai/api/core/Constants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.assemblyai.api.core;

public class Constants {
public static final String SDK_VERSION = "1.1.3";
public static final String SDK_VERSION = "2.0.0";
}

0 comments on commit 3293359

Please sign in to comment.