This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFilesClient.java
119 lines (112 loc) · 5.02 KB
/
FilesClient.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
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.assemblyai.api.resources.files;
import com.assemblyai.api.core.AssemblyAIApiException;
import com.assemblyai.api.core.AssemblyAIException;
import com.assemblyai.api.core.ClientOptions;
import com.assemblyai.api.core.InputStreamRequestBody;
import com.assemblyai.api.core.ObjectMappers;
import com.assemblyai.api.core.RequestOptions;
import com.assemblyai.api.errors.BadRequestError;
import com.assemblyai.api.errors.GatewayTimeoutError;
import com.assemblyai.api.errors.InternalServerError;
import com.assemblyai.api.errors.NotFoundError;
import com.assemblyai.api.errors.ServiceUnavailableError;
import com.assemblyai.api.errors.TooManyRequestsError;
import com.assemblyai.api.errors.UnauthorizedError;
import com.assemblyai.api.resources.files.types.UploadedFile;
import com.assemblyai.api.types.Error;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class FilesClient {
protected final ClientOptions clientOptions;
public FilesClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
/**
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(InputStream request) {
return upload(request, null);
}
/**
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(InputStream request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/upload")
.build();
RequestBody body = new InputStreamRequestBody(MediaType.parse("application/octet-stream"), request);
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
client = clientOptions.httpClientWithTimeout(requestOptions);
}
try (Response response = client.newCall(okhttpRequest).execute()) {
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UploadedFile.class);
}
String responseBodyString = responseBody != null ? responseBody.string() : "{}";
try {
switch (response.code()) {
case 400:
throw new BadRequestError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
case 401:
throw new UnauthorizedError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
case 404:
throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
case 429:
throw new TooManyRequestsError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
case 500:
throw new InternalServerError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Error.class));
case 503:
throw new ServiceUnavailableError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
case 504:
throw new GatewayTimeoutError(
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
}
} catch (JsonProcessingException ignored) {
// unable to map error response, throwing generic error
}
throw new AssemblyAIApiException(
"Error with status code " + response.code(),
response.code(),
ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
} catch (IOException e) {
throw new AssemblyAIException("Network error executing HTTP request", e);
}
}
/**
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(byte[] request) {
return upload(new ByteArrayInputStream(request));
}
/**
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(byte[] request, RequestOptions requestOptions) {
return upload(new ByteArrayInputStream(request), requestOptions);
}
}