@@ -136,51 +136,23 @@ public class SearchImageService {
|
|||||||
|
|
||||||
try (Response response = client.newCall(request).execute()) {
|
try (Response response = client.newCall(request).execute()) {
|
||||||
ResponseBody body = response.body();
|
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: ");
|
if (!response.isSuccessful()) {
|
||||||
String responseBody = readWithTimeout(body, 30000);
|
String errorBody = response.body() != null ? response.body().string() : "null";
|
||||||
log.info("Search reverse response body length: {} bytes", responseBody.length());
|
throw new IOException("API error " + response.code() + ": " + errorBody);
|
||||||
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()) {
|
if (body == null) {
|
||||||
// String errorBody = response.body() != null ? response.body().string() : "null";
|
throw new IOException("Empty response body");
|
||||||
// throw new IOException("API error " + response.code() + ": " + errorBody);
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// if (body == null) {
|
|
||||||
// throw new IOException("Empty response body");
|
|
||||||
// }
|
|
||||||
|
|
||||||
return body.string();
|
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)
|
public List<YandexSearchResponse.ImageResult> getAllImagesWithoutPagination(String searchApiJson, String findType)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
JsonNode root = objectMapper.readTree(searchApiJson);
|
JsonNode root = objectMapper.readTree(searchApiJson);
|
||||||
|
|||||||
Reference in New Issue
Block a user