Class HttpClientWrapper

java.lang.Object
com.petarmc.lib.net.HttpClientWrapper

public class HttpClientWrapper extends Object
A simple wrapper around Java's HttpClient with support for retries and asycn execution.
  • Field Details

    • log

      private static final PLogger log
    • client

      private final HttpClient client
    • maxRetries

      private final int maxRetries
    • executor

      private final ExecutorService executor
  • Constructor Details

    • HttpClientWrapper

      public HttpClientWrapper(int maxRetries)
      Creates a new HttpClientWrapper with a specified maximum number of retries.
      Parameters:
      maxRetries - maximum number of attempts for failed reqs, minimum 1
  • Method Details

    • get

      public CompletableFuture<String> get(String url)
      Sends an async GET request to the specified URL.
      Parameters:
      url - the target URL
      Returns:
      a CompletableFuture containing the response body
    • post

      public CompletableFuture<String> post(HttpRequest request)
      Sends an async POST request using the provided HttpRequest.
      Parameters:
      request - the HttpRequest to send
      Returns:
      a CompletableFuture containing the response body
    • sendWithRetry

      private CompletableFuture<String> sendWithRetry(HttpRequest req, int attempt)
    • shutdown

      public void shutdown()
      Shuts down the executor used for async HTTP requests.