Skip to content

Commit

Permalink
DOCS-297: Update Rev AI java sdk to support different base url (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennywong2129 authored Oct 28, 2024
1 parent 1b58d4f commit fd52684
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
run: mkdir staging && cp target/*.jar staging

- name: Upload build artifacts
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v3
with:
name: Package
path: staging
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ given Access Token:
```
// Initialize your client with your Rev AI access token
String accessToken = "Your Access Token";
ApiClient apiClient = new ApiClient(accessToken);
// Optionally set the Rev AI deployment base url to use
String baseUrl = RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl();
ApiClient apiClient = new ApiClient(accessToken, baseUrl);
```

### Checking credits remaining
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- groupId, artifactId, these namespaces should not be changed -->
<groupId>ai.rev</groupId>
<artifactId>revai-java-sdk</artifactId>
<version>2.4.2</version>
<version>2.5.0</version>
<name>Rev AI SDK for Java</name>
<description>Java SDK for Rev AI API</description>
<url>https://docs.rev.ai/</url>
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/ai/rev/helpers/ClientHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,31 @@ public static Retrofit createRetrofitInstance(
String apiVersion
) {
return new Retrofit.Builder()
.baseUrl(String.format("https://api.rev.ai/%s/%s/", apiName, apiVersion))
.baseUrl(String.format(
"%s/%s/%s/",
RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl(),
apiName,
apiVersion
))
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}

public static Retrofit createRetrofitInstance(
OkHttpClient client,
String apiName,
String apiVersion,
String baseUrl
) {
return new Retrofit.Builder()
.baseUrl(String.format(
"%s/%s/%s/",
baseUrl != null ? baseUrl : RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl(),
apiName,
apiVersion
))
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client)
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/ai/rev/helpers/RevAiApiDeploymentConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ai.rev.helpers;

import java.util.HashMap;
import java.util.Map;

public class RevAiApiDeploymentConfiguration {

// Enum for API deployment regions
public enum RevAiApiDeployment {
US("US"),
EU("EU");

private final String value;

RevAiApiDeployment(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

// Configuration map for deployment URLs
private static final Map<RevAiApiDeployment, DeploymentConfig> RevAiApiDeploymentConfigMap;

static {
RevAiApiDeploymentConfigMap = new HashMap<>();
RevAiApiDeploymentConfigMap.put(
RevAiApiDeployment.US,
new DeploymentConfig("https://api.rev.ai", "wss://api.rev.ai")
);
RevAiApiDeploymentConfigMap.put(
RevAiApiDeployment.EU,
new DeploymentConfig("https://ec1.api.rev.ai", "wss://ec1.api.rev.ai")
);
}

// Inner class for deployment configurations
public static class DeploymentConfig {
private final String baseUrl;
private final String baseWebsocketUrl;

public DeploymentConfig(String baseUrl, String baseWebsocketUrl) {
this.baseUrl = baseUrl;
this.baseWebsocketUrl = baseWebsocketUrl;
}

public String getBaseUrl() {
return baseUrl;
}

public String getBaseWebsocketUrl() {
return baseWebsocketUrl;
}
}

// Method to get the configuration for a specific deployment
public static DeploymentConfig getConfig(RevAiApiDeployment deployment) {
return RevAiApiDeploymentConfigMap.get(deployment);
}
}
42 changes: 34 additions & 8 deletions src/main/java/ai/rev/languageid/LanguageIdClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package ai.rev.languageid;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ai.rev.helpers.ClientHelper;
import ai.rev.helpers.RevAiApiDeploymentConfiguration;
import ai.rev.languageid.models.LanguageIdJob;
import ai.rev.languageid.models.LanguageIdJobOptions;
import ai.rev.languageid.models.LanguageIdResult;
Expand All @@ -11,14 +20,6 @@
import okhttp3.RequestBody;
import retrofit2.Retrofit;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* The LanguageIdClient object provides methods to send and retrieve information from all the
* Rev AI Language Identification API endpoints using the Retrofit HTTP client.
Expand All @@ -32,6 +33,31 @@ public class LanguageIdClient {
*/
public LanguageIdInterface apiInterface;

/**
* Constructs the API client used to send HTTP requests to Rev AI. The user access token can be
* generated on the website at <a
* href="https://www.rev.ai/access_token">https://www.rev.ai/access_token</a>.
*
* @param accessToken Rev AI authorization token associate with the account.
* @param baseUrl Optional url of the Rev AI API deployment to use, defaults to the US
deployement, i.e. 'https://api.rev.ai', which can be referenced as
RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl().
* @throws IllegalArgumentException If the access token is null or empty.
*/
public LanguageIdClient(String accessToken, String baseUrl) {
if (accessToken == null || accessToken.isEmpty()) {
throw new IllegalArgumentException("Access token must be provided");
}
this.client = ClientHelper.createOkHttpClient(accessToken);
Retrofit retrofit = ClientHelper.createRetrofitInstance(
client,
"languageid",
"v1",
baseUrl != null ? baseUrl : RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl()
);
this.apiInterface = retrofit.create(LanguageIdInterface.class);
}

/**
* Constructs the API client used to send HTTP requests to Rev AI. The user access token can be
* generated on the website at <a
Expand Down
50 changes: 42 additions & 8 deletions src/main/java/ai/rev/speechtotext/ApiClient.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package ai.rev.speechtotext;

import ai.rev.helpers.ClientHelper;
import ai.rev.speechtotext.models.asynchronous.*;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -16,6 +8,20 @@
import java.util.List;
import java.util.Map;

import ai.rev.helpers.ClientHelper;
import ai.rev.helpers.RevAiApiDeploymentConfiguration;
import ai.rev.speechtotext.models.asynchronous.RevAiAccount;
import ai.rev.speechtotext.models.asynchronous.RevAiCaptionType;
import ai.rev.speechtotext.models.asynchronous.RevAiJob;
import ai.rev.speechtotext.models.asynchronous.RevAiJobOptions;
import ai.rev.speechtotext.models.asynchronous.RevAiTranscript;
import ai.rev.speechtotext.models.asynchronous.Summary;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;

/**
* The ApiClient object provides methods to send and retrieve information from all the Rev AI API
* endpoints using the Retrofit HTTP client.
Expand All @@ -33,6 +39,34 @@ public class ApiClient {
* href="https://www.rev.ai/access_token">https://www.rev.ai/access_token</a>.
*
* @param accessToken Rev AI authorization token associate with the account.
* @param baseUrl Optional url of the Rev AI API deployment to use, defaults to the US
deployement, i.e. 'https://api.rev.ai', which can be referenced as
RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl().
* @throws IllegalArgumentException If the access token is null or empty.
*/
public ApiClient(String accessToken, String baseUrl) {
if (accessToken == null || accessToken.isEmpty()) {
throw new IllegalArgumentException("Access token must be provided");
}
this.client = ClientHelper.createOkHttpClient(accessToken);
Retrofit retrofit = ClientHelper.createRetrofitInstance(
client,
"speechtotext",
"v1",
baseUrl != null ? baseUrl : RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl()
);
this.apiInterface = retrofit.create(ApiInterface.class);
}

/**
* Constructs the API client used to send HTTP requests to Rev AI. The user access token can be
* generated on the website at <a
* href="https://www.rev.ai/access_token">https://www.rev.ai/access_token</a>.
*
* @param accessToken Rev AI authorization token associate with the account.
* @param baseUrl Optional url of the Rev AI API deployment to use, defaults to the US
deployement, i.e. 'https://api.rev.ai', which can be referenced as
RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl().
* @throws IllegalArgumentException If the access token is null or empty.
*/
public ApiClient(String accessToken) {
Expand Down
35 changes: 26 additions & 9 deletions src/test/java/ai/rev/speechtotext/integration/SubmitJobTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package ai.rev.speechtotext.integration;

import ai.rev.speechtotext.ApiClient;
import ai.rev.speechtotext.models.asynchronous.TranslationModel;
import ai.rev.speechtotext.models.asynchronous.*;
import ai.rev.testutils.EnvHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import java.io.*;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
import ai.rev.helpers.RevAiApiDeploymentConfiguration;
import ai.rev.speechtotext.ApiClient;
import ai.rev.speechtotext.models.asynchronous.RevAiCaptionType;
import ai.rev.speechtotext.models.asynchronous.RevAiJob;
import ai.rev.speechtotext.models.asynchronous.RevAiJobOptions;
import ai.rev.speechtotext.models.asynchronous.RevAiJobStatus;
import ai.rev.speechtotext.models.asynchronous.RevAiTranscript;
import ai.rev.speechtotext.models.asynchronous.SummarizationFormattingOptions;
import ai.rev.speechtotext.models.asynchronous.SummarizationJobStatus;
import ai.rev.speechtotext.models.asynchronous.SummarizationModel;
import ai.rev.speechtotext.models.asynchronous.SummarizationOptions;
import ai.rev.speechtotext.models.asynchronous.Summary;
import ai.rev.speechtotext.models.asynchronous.TranslationJobStatus;
import ai.rev.speechtotext.models.asynchronous.TranslationLanguageOptions;
import ai.rev.speechtotext.models.asynchronous.TranslationModel;
import ai.rev.speechtotext.models.asynchronous.TranslationOptions;
import ai.rev.testutils.EnvHelper;

public class SubmitJobTest {

Expand All @@ -25,7 +42,7 @@ public class SubmitJobTest {

@Before
public void setup() {
apiClient = new ApiClient(EnvHelper.getToken());
apiClient = new ApiClient(EnvHelper.getToken(), RevAiApiDeploymentConfiguration.getConfig(RevAiApiDeploymentConfiguration.RevAiApiDeployment.US).getBaseUrl());
}

@Test
Expand Down

0 comments on commit fd52684

Please sign in to comment.