diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index f2b17067..d98aeb6f 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -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 diff --git a/README.md b/README.md index 139d672d..5be09583 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/pom.xml b/pom.xml index 75bec693..f32336f4 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ ai.rev revai-java-sdk - 2.4.2 + 2.5.0 Rev AI SDK for Java Java SDK for Rev AI API https://docs.rev.ai/ diff --git a/src/main/java/ai/rev/helpers/ClientHelper.java b/src/main/java/ai/rev/helpers/ClientHelper.java index ed96e47a..a148bc12 100644 --- a/src/main/java/ai/rev/helpers/ClientHelper.java +++ b/src/main/java/ai/rev/helpers/ClientHelper.java @@ -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) diff --git a/src/main/java/ai/rev/helpers/RevAiApiDeploymentConfiguration.java b/src/main/java/ai/rev/helpers/RevAiApiDeploymentConfiguration.java new file mode 100644 index 00000000..3021c471 --- /dev/null +++ b/src/main/java/ai/rev/helpers/RevAiApiDeploymentConfiguration.java @@ -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 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); + } +} diff --git a/src/main/java/ai/rev/languageid/LanguageIdClient.java b/src/main/java/ai/rev/languageid/LanguageIdClient.java index 1de2b4db..df70a0b7 100644 --- a/src/main/java/ai/rev/languageid/LanguageIdClient.java +++ b/src/main/java/ai/rev/languageid/LanguageIdClient.java @@ -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; @@ -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. @@ -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 https://www.rev.ai/access_token. + * + * @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 https://www.rev.ai/access_token. * * @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 https://www.rev.ai/access_token. + * + * @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) { diff --git a/src/test/java/ai/rev/speechtotext/integration/SubmitJobTest.java b/src/test/java/ai/rev/speechtotext/integration/SubmitJobTest.java index 2f4856a5..3b380202 100644 --- a/src/test/java/ai/rev/speechtotext/integration/SubmitJobTest.java +++ b/src/test/java/ai/rev/speechtotext/integration/SubmitJobTest.java @@ -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 { @@ -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