-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageIdClient.java
311 lines (289 loc) · 13.6 KB
/
LanguageIdClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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;
import ai.rev.speechtotext.FileStreamRequestBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
/**
* The LanguageIdClient object provides methods to send and retrieve information from all the
* Rev AI Language Identification API endpoints using the Retrofit HTTP client.
*/
public class LanguageIdClient {
private OkHttpClient client;
/**
* Interface that LanguageIdClient methods use to make requests
*/
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
* href="https://www.rev.ai/access_token">https://www.rev.ai/access_token</a>.
*
* @param accessToken Rev AI authorization token associate with the account.
* @throws IllegalArgumentException If the access token is null or empty.
*/
public LanguageIdClient(String accessToken) {
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");
this.apiInterface = retrofit.create(LanguageIdInterface.class);
}
/**
* Manually closes the connection when the code is running in a JVM
*/
public void closeConnection() {
client.dispatcher().executorService().shutdown();
client.connectionPool().evictAll();
}
/**
* This method sends a GET request to the /jobs endpoint and returns a list of {@link LanguageIdJob}
* objects.
*
* @param limit The maximum number of jobs to return. The default is 100, max is 1000.
* @param startingAfter The job ID at which the list begins.
* @return A list of {@link LanguageIdJob} objects.
* @throws IOException If the response has a status code > 399.
* @see LanguageIdJob
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/GetListOfLanguageIdentificationJobs">https://docs.rev.ai/api/language-identification/reference/#operation/GetListOfLanguageIdentificationJobs</a>
*/
public List<LanguageIdJob> getListOfJobs(Integer limit, String startingAfter) throws IOException {
Map<String, String> options = new HashMap<>();
if (startingAfter != null) {
options.put("starting_after", startingAfter);
}
if (limit != null) {
options.put("limit", String.valueOf(limit));
}
return apiInterface.getListOfJobs(options).execute().body();
}
/**
* Overload of {@link LanguageIdClient#getListOfJobs(Integer, String)} without the optional startingAfter
* parameter.
*
* @param limit The maximum number of jobs to return. The default is 100, max is 1000.
* @return A list of {@link LanguageIdJob} objects.
* @throws IOException If the response has a status code > 399.
*/
public List<LanguageIdJob> getListOfJobs(Integer limit) throws IOException {
return getListOfJobs(limit, null);
}
/**
* Overload of {@link LanguageIdClient#getListOfJobs(Integer, String)} without the optional limit
* parameter.
*
* @param startingAfter The job ID at which the list begins.
* @return A list of {@link LanguageIdJob} objects.
* @throws IOException If the response has a status code > 399.
*/
public List<LanguageIdJob> getListOfJobs(String startingAfter) throws IOException {
return getListOfJobs(null, startingAfter);
}
/**
* Overload of {@link LanguageIdClient#getListOfJobs(Integer, String)} without the optional limit and
* startingAfter parameter.
*
* @return A list of {@link LanguageIdJob} objects.
* @throws IOException If the response has a status code > 399.
*/
public List<LanguageIdJob> getListOfJobs() throws IOException {
return getListOfJobs(null, null);
}
/**
* This method sends a GET request to the /jobs/{id} endpoint and returns a {@link LanguageIdJob}
* object.
*
* @param id The ID of the job to return an object for.
* @return A {@link LanguageIdJob} object.
* @throws IOException If the response has a status code > 399.
* @throws IllegalArgumentException If the job ID is null.
*/
public LanguageIdJob getJobDetails(String id) throws IOException {
if (id == null || id.isEmpty()) {
throw new IllegalArgumentException("Job ID must be provided");
}
return apiInterface.getJobDetails(id).execute().body();
}
/**
* This method sends a DELETE request to the /jobs/{id} endpoint.
*
* @param id The id of the job to be deleted.
* @throws IOException If the response has a status code > 399.
* @throws IllegalArgumentException If the job ID is null.
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/DeleteLanguageIdentificationJobById">https://docs.rev.ai/api/language-identification/reference/#operation/DeleteLanguageIdentificationJobById</a>
*/
public void deleteJob(String id) throws IOException {
if (id == null) {
throw new IllegalArgumentException("Job ID must be provided");
}
apiInterface.deleteJob(id).execute();
}
/**
* The method sends a GET request to the /jobs/{id}/result endpoint and returns a {@link LanguageIdResult} object.
*
* @param id The id of the job to return a result for.
* @return LanguageIdResult The result object.
* @throws IOException If the response has a status code > 399.
* @see LanguageIdResult
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/GetLanguageIdentificationResultById">https://docs.rev.ai/api/language-identification/reference/#operation/GetLanguageIdentificationResultById</a>
*/
public LanguageIdResult getResultObject(String id) throws IOException {
return apiInterface.getResultObject(id).execute().body();
}
/**
* The method sends a POST request to the /jobs endpoint, starts a language id job for the
* provided options and returns a {@link LanguageIdJob} object.
*
* @param options The language id options associated with this job.
* @return LanguageIdJob A representation of the language id job.
* @throws IOException If the response has a status code > 399.
* @see LanguageIdJob
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob">https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob</a>
*/
public LanguageIdJob submitJob(LanguageIdJobOptions options) throws IOException {
return apiInterface.submitJob(options).execute().body();
}
/**
* The method sends multipart/form POST request to the /jobs endpoint, starts a language id job for the
* provided local media file and returns a {@link LanguageIdJob} object.
*
* @param filePath A local path to the file on the computer.
* @param options The language id options associated with this job.
* @return LanguageIdJob A representation of the language id job.
* @throws IOException If the response has a status code > 399.
* @throws IllegalArgumentException If the file path is null.
* @see LanguageIdJob
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob">https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob</a>
*/
public LanguageIdJob submitJobLocalFile(String filePath, LanguageIdJobOptions options) throws IOException {
if (filePath == null) {
throw new IllegalArgumentException("File path must be provided");
}
if (options == null) {
options = new LanguageIdJobOptions();
}
File file = new File(filePath);
return submitMultipartRequest(new FileInputStream(file.getAbsoluteFile()), file.getName(), options);
}
/**
* The method sends a multipart/form POST request to the /jobs endpoint, starts a language id job for the
* provided media file provided by InputStream and returns a {@link LanguageIdJob} object.
*
* @param inputStream An InputStream of the media file.
* @param fileName The name of the file being streamed.
* @param options The language id options associated with this job.
* @return LanguageIdJob A representation of the language id job.
* @throws IOException If the response has a status code > 399.
* @throws IllegalArgumentException If the InputStream provided is null.
* @see LanguageIdJob
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob">https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob</a>
*/
public LanguageIdJob submitJobLocalFile(
InputStream inputStream, String fileName, LanguageIdJobOptions options) throws IOException {
if (inputStream == null) {
throw new IllegalArgumentException("File stream must be provided");
}
if (options == null) {
options = new LanguageIdJobOptions();
}
if (fileName == null) {
fileName = "audio_file";
}
return submitMultipartRequest(inputStream, fileName, options);
}
/**
* An overload of {@link LanguageIdClient#submitJobLocalFile(InputStream, String, LanguageIdJobOptions)}
* without the optional filename and language id options.
*
* @param inputStream An InputStream of the media file.
* @return LanguageIdJob A representation of the language id job.
* @throws IOException If the response has a status code > 399.
* @see LanguageIdJob
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob">https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob</a>
*/
public LanguageIdJob submitJobLocalFile(InputStream inputStream) throws IOException {
return submitJobLocalFile(inputStream, null, null);
}
/**
* An overload of {@link LanguageIdClient#submitJobLocalFile(InputStream, String, LanguageIdJobOptions)}
* without the additional language id options.
*
* @param inputStream An InputStream of the media file.
* @param fileName The name of the file being streamed.
* @return LanguageIdJob A representation of the language id job.
* @throws IOException If the response has a status code > 399.
* @see LanguageIdJob
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob">https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob</a>
*/
public LanguageIdJob submitJobLocalFile(InputStream inputStream, String fileName) throws IOException {
return submitJobLocalFile(inputStream, fileName, null);
}
/**
* An overload of {@link LanguageIdClient#submitJobLocalFile(InputStream, String, LanguageIdJobOptions)}
* without the optional filename.
*
* @param inputStream An InputStream of the media file.
* @param options The language id options associated with this job.
* @return LanguageIdJob A representation of the language id job.
* @throws IOException If the response has a status code > 399.
* @see LanguageIdJob
* @see <a
* href="https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob">https://docs.rev.ai/api/language-identification/reference/#operation/SubmitLanguageIdentificationJob</a>
*/
public LanguageIdJob submitJobLocalFile(InputStream inputStream, LanguageIdJobOptions options)
throws IOException {
return submitJobLocalFile(inputStream, null, options);
}
private LanguageIdJob submitMultipartRequest(
InputStream inputStream, String fileName, LanguageIdJobOptions options) throws IOException {
RequestBody fileRequest = FileStreamRequestBody.create(inputStream, MediaType.parse("audio/*"));
MultipartBody.Part filePart = MultipartBody.Part.createFormData("media", fileName, fileRequest);
return apiInterface.submitJobLocalFile(filePart, options).execute().body();
}
}