This commit is contained in:
@@ -28,10 +28,38 @@ public class SearchImageService {
|
|||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
private final OkHttpClient httpClient;
|
// private final OkHttpClient httpClient;
|
||||||
|
private final OkHttpClient yandexHttpClient;
|
||||||
|
private final OkHttpClient googleHttpClient;
|
||||||
|
|
||||||
|
// public SearchImageService() {
|
||||||
|
// this.httpClient = new OkHttpClient.Builder()
|
||||||
|
// .connectTimeout(18, TimeUnit.SECONDS)
|
||||||
|
// .writeTimeout(18, TimeUnit.SECONDS)
|
||||||
|
// .readTimeout(18, TimeUnit.SECONDS)
|
||||||
|
// .callTimeout(18, TimeUnit.SECONDS)
|
||||||
|
// .followRedirects(true)
|
||||||
|
// .followSslRedirects(true)
|
||||||
|
// .retryOnConnectionFailure(true)
|
||||||
|
// .connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
|
||||||
|
// .build();
|
||||||
|
//
|
||||||
|
// this.objectMapper = new ObjectMapper();
|
||||||
|
// }
|
||||||
|
|
||||||
public SearchImageService() {
|
public SearchImageService() {
|
||||||
this.httpClient = new OkHttpClient.Builder()
|
this.yandexHttpClient = new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(18, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(18, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(18, TimeUnit.SECONDS)
|
||||||
|
.callTimeout(18, TimeUnit.SECONDS)
|
||||||
|
.followRedirects(true)
|
||||||
|
.followSslRedirects(true)
|
||||||
|
.retryOnConnectionFailure(true)
|
||||||
|
.connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
this.googleHttpClient = new OkHttpClient.Builder()
|
||||||
.connectTimeout(18, TimeUnit.SECONDS)
|
.connectTimeout(18, TimeUnit.SECONDS)
|
||||||
.writeTimeout(18, TimeUnit.SECONDS)
|
.writeTimeout(18, TimeUnit.SECONDS)
|
||||||
.readTimeout(18, TimeUnit.SECONDS)
|
.readTimeout(18, TimeUnit.SECONDS)
|
||||||
@@ -103,6 +131,8 @@ public class SearchImageService {
|
|||||||
throw new IllegalStateException("SearchAPI key not configured");
|
throw new IllegalStateException("SearchAPI key not configured");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OkHttpClient client = engine.contains("yandex_reverse_image") ? yandexHttpClient : googleHttpClient;
|
||||||
|
|
||||||
HttpUrl url = HttpUrl.parse("https://www.searchapi.io/api/v1/search")
|
HttpUrl url = HttpUrl.parse("https://www.searchapi.io/api/v1/search")
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.addQueryParameter("engine", engine)
|
.addQueryParameter("engine", engine)
|
||||||
@@ -123,7 +153,7 @@ public class SearchImageService {
|
|||||||
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
|
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
try (Response response = httpClient.newCall(request).execute()) {
|
try (Response response = client.newCall(request).execute()) {
|
||||||
long duration = System.currentTimeMillis() - start;
|
long duration = System.currentTimeMillis() - start;
|
||||||
log.info("Yandex response code={}, duration={}ms, attempt={}",
|
log.info("Yandex response code={}, duration={}ms, attempt={}",
|
||||||
response.code(), duration, attempt);
|
response.code(), duration, attempt);
|
||||||
|
|||||||
Reference in New Issue
Block a user