private static final String PROXY_HOST = System.getenv().get("PROXY_HOST");
private static final String PROXY_PORT = System.getenv().get("PROXY_PORT");
protected static final OkHttpClient httpClient = getHttpClient();
private static OkHttpClient getHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(100);
dispatcher.setMaxRequestsPerHost(50);
builder.dispatcher(dispatcher);
builder.retryOnConnectionFailure(true);
builder.connectionPool(new ConnectionPool(50, 120, TimeUnit.SECONDS));
if ("DEBUG".equalsIgnoreCase(LOG_LEVEL)) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new Logger());
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.interceptors().add(httpLoggingInterceptor);
}
if (StringUtils.isNotBlank(PROXY_HOST) && StringUtils.isNotBlank(PROXY_PORT)) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, Integer.parseInt(PROXY_PORT))));
}
return builder.build();
}