<commit_before>package org.folio.rest.tools.client;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.commons.io.IOUtils;
import org.folio.rest.tools.parser.JsonPathParser;
import org.folio.rest.tools.utils.VertxUtils;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
 * @author shale
 *
 */
public class HttpModuleClient {

  private static final String CTYPE = "Content-Type";
  private static final String ACCEPT = "Accept";
  private static final String APP_JSON_CTYPE = "application/json";
  private static final String APP_JSON_ACCEPT = "application/json";
  private static final String X_OKAPI_HEADER = "x-okapi-tenant";

  private static final Logger log = LoggerFactory.getLogger(HttpModuleClient.class);

  private String tenantId;
  private HttpClientOptions options;
  private HttpClient httpClient;
  private LoadingCache<String, Response> cache = null;
  private Vertx vertx;
  private boolean autoCloseConnections = true;
  private Map<String, String> headers = new HashMap<>();
  private long cacheTO = 30; //minutes
  private int connTO = 2000;
  private int idleTO = 5000;

  public HttpModuleClient(String host, int port, String tenantId, boolean keepAlive, int connTO,
      int idleTO, boolean autoCloseConnections, long cacheTO) {
    this.tenantId = tenantId;
    this.cacheTO = cacheTO;
    this.connTO = connTO;
    this.idleTO = idleTO;
    options = new HttpClientOptions().setLogActivity(true).setKeepAlive(keepAlive)
        .setDefaultHost(host).setDefaultPort(port).setConnectTimeout(connTO).setIdleTimeout(idleTO);
    this.autoCloseConnections = autoCloseConnections;
    vertx = VertxUtils.getVertxFromContextOrNew();
    setDefaultHeaders();
  }

  public HttpModuleClient(String host, int port, String tenantId) {
    this(host, port, tenantId, true, 2000, 5000, true, 30);
  }

  public HttpModuleClient(String host, int port, String tenantId, boolean autoCloseConnections) {
    this(host, port, tenantId, true, 2000, 5000, autoCloseConnections, 30);
  }

  private void setDefaultHeaders(){
    headers.put(X_OKAPI_HEADER, tenantId);
    headers.put(CTYPE, APP_JSON_CTYPE);
    headers.put(ACCEPT, APP_JSON_ACCEPT);
  }

  private void request(HttpMethod method, String endpoint, Map<String, String> headers,
      boolean cache, Handler<HttpClientResponse> responseHandler, CompletableFuture<Response> cf2){

    if(responseHandler == null){
      CompletableFuture<Response> cf = new CompletableFuture<>();
      responseHandler = new HTTPJsonResponseHandler(endpoint, cf);
    }
    httpClient = vertx.createHttpClient(options);
    HttpClientRequest request = httpClient.request(method, endpoint);
    request.exceptionHandler(error -> {
      Response r = new Response();
      r.populateError(endpoint, -1, error.getMessage());
      cf2.complete(r);
    })
    .handler(responseHandler);
    if(headers != null){
      this.headers.putAll(headers);
    }
    request.headers().setAll(this.headers);<commit_msg>http client can get abs path<commit_after>package org.folio.rest.tools.client;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.commons.io.IOUtils;
import org.folio.rest.tools.parser.JsonPathParser;
import org.folio.rest.tools.utils.VertxUtils;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
 * @author shale
 *
 */
public class HttpModuleClient {

  private static final String CTYPE = "Content-Type";
  private static final String ACCEPT = "Accept";
  private static final String APP_JSON_CTYPE = "application/json";
  private static final String APP_JSON_ACCEPT = "application/json";
  private static final String X_OKAPI_HEADER = "x-okapi-tenant";

  private static final Logger log = LoggerFactory.getLogger(HttpModuleClient.class);

  private String tenantId;
  private HttpClientOptions options;
  private HttpClient httpClient;
  private LoadingCache<String, Response> cache = null;
  private Vertx vertx;
  private boolean autoCloseConnections = true;
  private Map<String, String> headers = new HashMap<>();
  private long cacheTO = 30; //minutes
  private int connTO = 2000;
  private int idleTO = 5000;
  private boolean absoluteHostAddr = false;

  public HttpModuleClient(String host, int port, String tenantId, boolean keepAlive, int connTO,
      int idleTO, boolean autoCloseConnections, long cacheTO) {

    this.tenantId = tenantId;
    this.cacheTO = cacheTO;
    this.connTO = connTO;
    this.idleTO = idleTO;
    options = new HttpClientOptions().setLogActivity(true).setKeepAlive(keepAlive)
        .setConnectTimeout(connTO).setIdleTimeout(idleTO);

    if(port == -1){
      absoluteHostAddr = true;
    }
    else{
      options.setDefaultPort(port).setDefaultHost(host);
    }
    this.autoCloseConnections = autoCloseConnections;
    vertx = VertxUtils.getVertxFromContextOrNew();
    setDefaultHeaders();
  }

  public HttpModuleClient(String host, int port, String tenantId) {
    this(host, port, tenantId, true, 2000, 5000, true, 30);
  }

  public HttpModuleClient(String absHost, String tenantId) {
    this(absHost, -1, tenantId, true, 2000, 5000, true, 30);
  }

  public HttpModuleClient(String host, int port, String tenantId, boolean autoCloseConnections) {
    this(host, port, tenantId, true, 2000, 5000, autoCloseConnections, 30);
  }

  public HttpModuleClient(String absHost, String tenantId, boolean autoCloseConnections) {
    this(absHost, -1, tenantId, true, 2000, 5000, autoCloseConnections, 30);
  }

  private void setDefaultHeaders(){
    headers.put(X_OKAPI_HEADER, tenantId);
    headers.put(CTYPE, APP_JSON_CTYPE);
    headers.put(ACCEPT, APP_JSON_ACCEPT);
  }

  private void request(HttpMethod method, String endpoint, Map<String, String> headers,
      boolean cache, Handler<HttpClientResponse> responseHandler, CompletableFuture<Response> cf2){

    if(responseHandler == null){
      CompletableFuture<Response> cf = new CompletableFuture<>();
      responseHandler = new HTTPJsonResponseHandler(endpoint, cf);
    }
    httpClient = vertx.createHttpClient(options);
    HttpClientRequest request = httpClient.request(method, endpoint);
    request.exceptionHandler(error -> {
      Response r = new Response();
      r.populateError(endpoint, -1, error.getMessage());
      cf2.complete(r);
    })
    .handler(responseHandler);
    if(headers != null){
      this.headers.putAll(headers);
    }
    request.headers().setAll(this.headers);
{'fraction_non_alphanumeric': 0.10499935740907339, 'fraction_numerical': 0.011438118493766868, 'mean_word_length': 4.655523255813954, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6991446', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2315, 'n_words': 540}