|
1 | 1 | package com.translated.lara.net; |
2 | 2 |
|
3 | | -import com.google.gson.FieldNamingPolicy; |
4 | | -import com.google.gson.Gson; |
5 | | -import com.google.gson.GsonBuilder; |
6 | | -import com.google.gson.JsonElement; |
7 | | -import com.google.gson.JsonObject; |
8 | | -import com.google.gson.JsonParser; |
| 3 | +import com.google.gson.*; |
9 | 4 | import com.translated.lara.Credentials; |
10 | 5 | import com.translated.lara.Version; |
11 | 6 | import com.translated.lara.errors.LaraApiConnectionException; |
12 | | -import com.translated.lara.errors.LaraApiException; |
13 | 7 | import com.translated.lara.errors.LaraException; |
14 | 8 | import com.translated.lara.net.json.DocumentStatusTypeAdapter; |
15 | 9 | import com.translated.lara.net.json.TextResultValueTypeAdapter; |
|
18 | 12 |
|
19 | 13 | import javax.crypto.Mac; |
20 | 14 | import javax.crypto.spec.SecretKeySpec; |
21 | | -import java.io.BufferedReader; |
22 | | -import java.io.File; |
23 | | -import java.io.IOException; |
24 | | -import java.io.InputStream; |
25 | | -import java.io.InputStreamReader; |
26 | | -import java.io.OutputStream; |
| 15 | +import java.io.*; |
27 | 16 | import java.net.HttpURLConnection; |
28 | 17 | import java.net.MalformedURLException; |
29 | 18 | import java.net.URL; |
@@ -133,17 +122,19 @@ public ClientResponse put(String path, Map<String, Object> params, Map<String, F |
133 | 122 | return request("PUT", path, params, files, headers); |
134 | 123 | } |
135 | 124 |
|
136 | | - public Stream<TextResult> postAndGetStream(String path, Map<String, Object> params) throws LaraException { |
| 125 | + public Stream<ClientResponse> postAndGetStream(String path, Map<String, Object> params) throws LaraException { |
137 | 126 | return postAndGetStream(path, params, null, null); |
138 | 127 | } |
139 | | - public Stream<TextResult> postAndGetStream(String path, Map<String, Object> params, Map<String, String> headers) throws LaraException { |
| 128 | + |
| 129 | + public Stream<ClientResponse> postAndGetStream(String path, Map<String, Object> params, Map<String, String> headers) throws LaraException { |
140 | 130 | return postAndGetStream(path, params, null, headers); |
141 | 131 | } |
142 | | - public Stream<TextResult> postAndGetStream(String path, Map<String, Object> params, Map<String, File> files, Map<String, String> headers) throws LaraException { |
143 | | - return requestStream("POST", path, params, files, headers).map(this::parseTextResult); |
| 132 | + |
| 133 | + public Stream<ClientResponse> postAndGetStream(String path, Map<String, Object> params, Map<String, File> files, Map<String, String> headers) throws LaraException { |
| 134 | + return requestStream("POST", path, params, files, headers); |
144 | 135 | } |
145 | 136 |
|
146 | | - private Stream<JsonElement> requestStream(String method, String path, Map<String, Object> params, Map<String, File> files, Map<String, String> headers) throws LaraException { |
| 137 | + private Stream<ClientResponse> requestStream(String method, String path, Map<String, Object> params, Map<String, File> files, Map<String, String> headers) throws LaraException { |
147 | 138 | path = normalizePath(path); |
148 | 139 | params = prune(params); |
149 | 140 | files = prune(files); |
@@ -210,21 +201,22 @@ private Stream<JsonElement> requestStream(String method, String path, Map<String |
210 | 201 | throw new LaraApiConnectionException("HTTP error code: " + responseCode); |
211 | 202 | } |
212 | 203 |
|
| 204 | + String contentType = connection.getContentType(); |
213 | 205 | InputStream inputStream = connection.getInputStream(); |
214 | 206 | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); |
215 | 207 |
|
216 | 208 | return reader.lines() |
217 | | - .filter(line -> !line.trim().isEmpty()) |
218 | | - .map(line -> parseStreamLine(line, responseCode)) |
219 | | - .onClose(() -> { |
220 | | - try { |
221 | | - reader.close(); |
222 | | - } catch (IOException e) { |
223 | | - // Ignore close errors |
224 | | - } finally { |
225 | | - connection.disconnect(); |
226 | | - } |
227 | | - }); |
| 209 | + .filter(line -> !line.trim().isEmpty()) |
| 210 | + .map(line -> parseStreamLine(contentType, responseCode, line)) |
| 211 | + .onClose(() -> { |
| 212 | + try { |
| 213 | + reader.close(); |
| 214 | + } catch (IOException e) { |
| 215 | + // Ignore close errors |
| 216 | + } finally { |
| 217 | + connection.disconnect(); |
| 218 | + } |
| 219 | + }); |
228 | 220 |
|
229 | 221 | } catch (IOException e) { |
230 | 222 | throw new LaraApiConnectionException("Streaming request failed: " + e.getMessage(), e); |
@@ -285,7 +277,7 @@ private ClientResponse request(String method, String path, Map<String, Object> p |
285 | 277 | } |
286 | 278 | } |
287 | 279 |
|
288 | | - return new ClientResponse(gson, connection); |
| 280 | + return ClientResponse.fromConnection(gson, connection); |
289 | 281 | } catch (IOException e) { |
290 | 282 | throw new LaraApiConnectionException("Failed to connect to URL: " + baseUrl + path, e); |
291 | 283 | } |
@@ -358,56 +350,13 @@ private TextResult parseTextResult(JsonElement jsonElement) { |
358 | 350 | } |
359 | 351 | } |
360 | 352 |
|
361 | | - private JsonElement parseStreamLine(String line, int responseCode) { |
362 | | - JsonElement jsonElement; |
363 | | - try { |
364 | | - jsonElement = JsonParser.parseString(line); |
365 | | - } catch (Exception e) { |
366 | | - throw new RuntimeException("Failed to parse streaming response line: " + line, e); |
367 | | - } |
368 | | - |
369 | | - if (!jsonElement.isJsonObject()) { |
370 | | - return jsonElement; |
371 | | - } |
372 | | - |
373 | | - JsonObject jsonObject = jsonElement.getAsJsonObject(); |
374 | | - int status = jsonObject.has("status") ? jsonObject.get("status").getAsInt() : responseCode; |
375 | | - |
376 | | - if (status < 200 || status >= 300) { |
377 | | - throw new RuntimeException(buildStreamingApiException(status, jsonObject)); |
378 | | - } |
379 | | - |
380 | | - JsonElement data = jsonObject.has("data") ? jsonObject.get("data") : jsonElement; |
| 353 | + private ClientResponse parseStreamLine(String contentType, int responseCode, String line) { |
| 354 | + JsonObject root = JsonParser.parseString(line).getAsJsonObject(); |
381 | 355 |
|
382 | | - if (data.isJsonObject()) { |
383 | | - JsonObject dataObject = data.getAsJsonObject(); |
384 | | - if (dataObject.has("content")) { |
385 | | - return dataObject.get("content"); |
386 | | - } |
387 | | - } |
388 | | - |
389 | | - return data; |
390 | | - } |
391 | | - |
392 | | - private LaraApiException buildStreamingApiException(int status, JsonElement data) { |
393 | | - String type = "UnknownError"; |
394 | | - String message = "An unknown error occurred"; |
395 | | - |
396 | | - if (data != null && data.isJsonObject()) { |
397 | | - JsonObject dataObject = data.getAsJsonObject(); |
398 | | - JsonObject errorObject = dataObject.has("error") && dataObject.get("error").isJsonObject() |
399 | | - ? dataObject.getAsJsonObject("error") |
400 | | - : dataObject; |
401 | | - |
402 | | - if (errorObject.has("type")) { |
403 | | - type = errorObject.get("type").getAsString(); |
404 | | - } |
405 | | - if (errorObject.has("message")) { |
406 | | - message = errorObject.get("message").getAsString(); |
407 | | - } |
408 | | - } |
| 356 | + int httpStatus = root.has("status") ? root.get("status").getAsInt() : responseCode; |
| 357 | + JsonElement response = root.has("content") ? root.get("content") : root.get("error"); |
409 | 358 |
|
410 | | - return new LaraApiException(status, type, message); |
| 359 | + return new ClientResponse(gson, httpStatus, contentType, response); |
411 | 360 | } |
412 | 361 |
|
413 | 362 | // Helper method to read the error stream |
|
0 commit comments