package HttpCall;

import Credentials.PlonkTest;
import eu.hsrw.ias.ggd.PlonkHttpPostRequest;

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.TimeUnit;

public class HttpPost {

  public String HttpCallPost(String baseUrl, String body) throws Exception {
    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))
          .POST(HttpRequest.BodyPublishers.ofString(body))
          .setHeader("Content-Type", "application/json")
//                .setHeader("Content-Type","application/x-www-form-urlencoded")
          .setHeader("Accept", "application/json")
          .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;
  }
}
