package HttpCall;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class HttpGet {

  public String HttpGetCall(String baseURL, String token)
    throws InterruptedException, ExecutionException, TimeoutException {
    var client = HttpClient
      .newBuilder()
      .version(HttpClient.Version.HTTP_1_1)
      .followRedirects(HttpClient.Redirect.ALWAYS)
      .build();
    HttpRequest request = null;
    try {
      request =
        HttpRequest
          .newBuilder(new URI(baseURL))
          .setHeader("Authorization", token)
          //                        .setHeader("access_token", token)
          .setHeader("Content-Type", "application/json")
          .setHeader("Accept", "application/json")
          .header("Authority", "https://api.dev.whysor.com/")
          .build();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
    CompletableFuture<HttpResponse<String>> response = client.sendAsync(
      request,
      HttpResponse.BodyHandlers.ofString()
    );
    String result = response
      .thenApply(HttpResponse::body)
      .get(5, TimeUnit.SECONDS);

    return result;
  }
}
