-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamRequest.java
More file actions
202 lines (169 loc) · 6.58 KB
/
StreamRequest.java
File metadata and controls
202 lines (169 loc) · 6.58 KB
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
package io.getstream.services.framework;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.getstream.exceptions.StreamException;
import io.getstream.models.UploadFileRequest;
import io.getstream.models.UploadImageRequest;
import io.getstream.models.framework.RateLimit;
import io.getstream.models.framework.StreamResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
public class StreamRequest<T> {
private final OkHttpClient client;
private final Request request;
private final ObjectMapper objectMapper;
private final TypeReference<T> typeReference;
public StreamRequest(
OkHttpClient client,
ObjectMapper mapper,
String baseURL,
String method,
String path,
Object jRequest,
Map<String, String> pathParams,
TypeReference<T> typeReference)
throws StreamException {
this.objectMapper = mapper;
this.client = client;
this.typeReference = typeReference;
Request request;
try {
RequestBody rawBody;
if (List.of("GET", "DELETE", "HEAD", "OPTIONS").contains(method) || jRequest == null) {
rawBody = null;
} else if (jRequest instanceof UploadFileRequest) {
rawBody = createMultipartBody((UploadFileRequest) jRequest);
} else if (jRequest instanceof UploadImageRequest) {
rawBody = createMultipartBody((UploadImageRequest) jRequest);
} else {
rawBody = RequestBody.create(objectMapper.writeValueAsBytes(jRequest));
}
request =
new Request.Builder()
.url(buildUrl(baseURL, path, pathParams, jRequest))
.method(method, rawBody)
.build();
} catch (Throwable e) {
throw new StreamException(e);
}
this.request = request;
}
@NotNull
private static RateLimit getRateLimit(Response response) {
Headers headers = response.headers();
RateLimit rateLimit = new RateLimit();
var header = headers.get("X-Ratelimit-Limit");
if (header != null) {
rateLimit.setLimit(Integer.parseInt(header));
}
header = headers.get("X-Ratelimit-Remaining");
if (header != null) {
rateLimit.setRemaining(Integer.parseInt(header));
}
header = headers.get("X-Ratelimit-Reset");
if (header != null) {
rateLimit.setReset(new Date(Long.parseLong(header) * 1000));
}
return rateLimit;
}
public HttpUrl buildUrl(
String baseUrl, String path, Map<String, String> pathParams, Object queryParams)
throws JsonProcessingException, NullPointerException, IllegalAccessException {
// Handle path parameters
if (pathParams != null && !pathParams.isEmpty()) {
for (Map.Entry<String, String> entry : pathParams.entrySet()) {
path = path.replace("{" + entry.getKey() + "}", entry.getValue());
}
}
// Add the processed path
// Remove leading slash if present to avoid double slashes
String processedPath = path.startsWith("/") ? path.substring(1) : path;
// Start building with the base URL
HttpUrl.Builder urlBuilder = HttpUrl.parse(baseUrl).newBuilder(processedPath);
// Add query parameters
if (queryParams != null) {
Map<String, String> queryMap = QueryConverter.getQueryParameters(queryParams, objectMapper);
for (Map.Entry<String, String> entry : queryMap.entrySet()) {
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
}
}
return urlBuilder.build();
}
private RequestBody createMultipartBody(UploadFileRequest request) throws IOException {
if (request.getFile() == null || request.getFile().isEmpty()) {
throw new IllegalArgumentException("File path must be provided");
}
File file = new File(request.getFile());
if (!file.exists()) {
throw new IOException("File not found: " + request.getFile());
}
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
// Add file
RequestBody fileBody = RequestBody.create(file, MediaType.parse("application/octet-stream"));
builder.addFormDataPart("file", file.getName(), fileBody);
// Add user field if present
if (request.getUser() != null) {
String userJson = objectMapper.writeValueAsString(request.getUser());
builder.addFormDataPart("user", userJson);
}
return builder.build();
}
private RequestBody createMultipartBody(UploadImageRequest request) throws IOException {
if (request.getFile() == null || request.getFile().isEmpty()) {
throw new IllegalArgumentException("File path must be provided");
}
File file = new File(request.getFile());
if (!file.exists()) {
throw new IOException("File not found: " + request.getFile());
}
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
// Add file
RequestBody fileBody = RequestBody.create(file, MediaType.parse("image/*"));
builder.addFormDataPart("file", file.getName(), fileBody);
// Add upload_sizes field if present
if (request.getUploadSizes() != null && !request.getUploadSizes().isEmpty()) {
String uploadSizesJson = objectMapper.writeValueAsString(request.getUploadSizes());
builder.addFormDataPart("upload_sizes", uploadSizesJson);
}
// Add user field if present
if (request.getUser() != null) {
String userJson = objectMapper.writeValueAsString(request.getUser());
builder.addFormDataPart("user", userJson);
}
return builder.build();
}
public StreamResponse<T> execute() throws StreamException {
okhttp3.Call call = client.newCall(request);
Response response;
try {
response = call.execute();
} catch (IOException e) {
throw StreamException.build(e);
}
return this.parseResponse(response);
}
private StreamResponse<T> parseResponse(okhttp3.Response response) throws StreamException {
if (!response.isSuccessful()) {
throw StreamException.build(response);
}
ResponseBody rawBody = response.body();
// unmarshal the response body to the expected type using jackson
T result;
try {
result = objectMapper.readValue(rawBody.string(), typeReference);
} catch (Throwable e) {
throw StreamException.build(e);
}
StreamResponse<T> streamResponse = new StreamResponse<>();
streamResponse.setData(result);
RateLimit rateLimit = getRateLimit(response);
streamResponse.setRateLimit(rateLimit);
return streamResponse;
}
}