@@ -136,51 +136,23 @@ public class SearchImageService {
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
ResponseBody body = response.body();
|
||||
long duration = System.currentTimeMillis() - start;
|
||||
log.info("SearchAPI response code={}, duration={}ms, engine={}",
|
||||
response.code(), duration, engine);
|
||||
|
||||
log.info("START READ BODY: ");
|
||||
String responseBody = readWithTimeout(body, 30000);
|
||||
log.info("Search reverse response body length: {} bytes", responseBody.length());
|
||||
log.info("Response preview: {}", responseBody.substring(0, Math.min(500, responseBody.length())));
|
||||
log.info("END READ BODY: ");
|
||||
// long duration = System.currentTimeMillis() - start;
|
||||
// log.info("SearchAPI response code={}, duration={}ms, engine={}",
|
||||
// response.code(), duration, engine);
|
||||
if (!response.isSuccessful()) {
|
||||
String errorBody = response.body() != null ? response.body().string() : "null";
|
||||
throw new IOException("API error " + response.code() + ": " + errorBody);
|
||||
}
|
||||
|
||||
// if (!response.isSuccessful()) {
|
||||
// String errorBody = response.body() != null ? response.body().string() : "null";
|
||||
// throw new IOException("API error " + response.code() + ": " + errorBody);
|
||||
// }
|
||||
|
||||
// if (body == null) {
|
||||
// throw new IOException("Empty response body");
|
||||
// }
|
||||
if (body == null) {
|
||||
throw new IOException("Empty response body");
|
||||
}
|
||||
|
||||
return body.string();
|
||||
}
|
||||
}
|
||||
|
||||
private String readWithTimeout(ResponseBody body, long timeoutMs) throws IOException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(body.charStream())) {
|
||||
char[] buffer = new char[8192];
|
||||
int read;
|
||||
|
||||
while ((read = reader.read(buffer)) != -1) {
|
||||
result.append(buffer, 0, read);
|
||||
|
||||
if (System.currentTimeMillis() - startTime > timeoutMs) {
|
||||
log.warn("Partial read timeout after {} ms, received {} bytes",
|
||||
timeoutMs, result.length());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public List<YandexSearchResponse.ImageResult> getAllImagesWithoutPagination(String searchApiJson, String findType)
|
||||
throws IOException {
|
||||
JsonNode root = objectMapper.readTree(searchApiJson);
|
||||
|
||||
Reference in New Issue
Block a user