Class MagicHttpClient

java.lang.Object
dev.ua.theroer.magicutils.http.MagicHttpClient
All Implemented Interfaces:
AutoCloseable

public final class MagicHttpClient extends Object implements AutoCloseable
MagicUtils HTTP client with retries, config defaults, and JSON mapping.
  • Method Details

    • builder

      public static MagicHttpClient.Builder builder(Platform platform, ConfigManager configManager)
      Creates a builder that loads configuration from the provided manager.
      Parameters:
      platform - platform used for logging
      configManager - configuration manager used to register HttpClientConfig
      Returns:
      a new builder instance
    • builder

      public static MagicHttpClient.Builder builder(Platform platform)
      Creates a builder without auto-loading configuration.
      Parameters:
      platform - platform used for logging
      Returns:
      a new builder instance
    • config

      public HttpClientConfig config()
      Returns the resolved configuration instance.
      Returns:
      configuration used by the client
    • mapper

      public com.fasterxml.jackson.databind.ObjectMapper mapper()
      Returns the configured Jackson mapper.
      Returns:
      JSON object mapper
    • request

      public HttpRequest.Builder request(String path)
      Creates a request builder with defaults (base URL, headers, timeouts).
      Parameters:
      path - absolute URL or a path resolved against base URL
      Returns:
      request builder
    • get

      Sends a GET request and returns the response body as string.
      Parameters:
      path - absolute URL or a path resolved against base URL
      Returns:
      HTTP response with string body
      Throws:
      IOException - if request fails
      InterruptedException - if the thread is interrupted
    • getAsync

      public CompletableFuture<HttpResponse<String>> getAsync(String path)
      Sends a GET request asynchronously.
      Parameters:
      path - absolute URL or a path resolved against base URL
      Returns:
      future with response
    • getSmart

      public CompletableFuture<HttpResponse<String>> getSmart(String path)
      Sends a GET request, switching to async on blocking-sensitive threads.
      Parameters:
      path - absolute URL or a path resolved against base URL
      Returns:
      future with response
    • getJson

      public <T> T getJson(String path, Class<T> type) throws IOException, InterruptedException
      Sends a GET request and deserializes the JSON response into a type.
      Type Parameters:
      T - type of result
      Parameters:
      path - absolute URL or a path resolved against base URL
      type - target type
      Returns:
      deserialized response
      Throws:
      IOException - if request fails
      InterruptedException - if the thread is interrupted
    • getJsonAsync

      public <T> CompletableFuture<T> getJsonAsync(String path, Class<T> type)
      Sends a GET request asynchronously and deserializes JSON response.
      Type Parameters:
      T - type of result
      Parameters:
      path - absolute URL or a path resolved against base URL
      type - target type
      Returns:
      future with deserialized response
    • getJsonSmart

      public <T> CompletableFuture<T> getJsonSmart(String path, Class<T> type)
      Sends a GET request and deserializes JSON response, switching to async on blocking-sensitive threads.
      Type Parameters:
      T - type of result
      Parameters:
      path - absolute URL or a path resolved against base URL
      type - target type
      Returns:
      future with deserialized response
    • post

      public HttpResponse<String> post(String path, String body) throws IOException, InterruptedException
      Sends a plain-text POST request.
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body
      Returns:
      HTTP response with string body
      Throws:
      IOException - if request fails
      InterruptedException - if the thread is interrupted
    • postAsync

      public CompletableFuture<HttpResponse<String>> postAsync(String path, String body)
      Sends a plain-text POST request asynchronously.
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body
      Returns:
      future with response
    • postSmart

      public CompletableFuture<HttpResponse<String>> postSmart(String path, String body)
      Sends a plain-text POST request, switching to async on blocking-sensitive threads.
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body
      Returns:
      future with response
    • postJson

      public HttpResponse<String> postJson(String path, Object body) throws IOException, InterruptedException
      Sends a JSON POST request.
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body object
      Returns:
      HTTP response with string body
      Throws:
      IOException - if request fails
      InterruptedException - if the thread is interrupted
    • postJson

      public <T> T postJson(String path, Object body, Class<T> type) throws IOException, InterruptedException
      Sends a JSON POST request and deserializes the response.
      Type Parameters:
      T - type of result
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body object
      type - target type
      Returns:
      deserialized response
      Throws:
      IOException - if request fails
      InterruptedException - if the thread is interrupted
    • postJsonAsync

      public CompletableFuture<HttpResponse<String>> postJsonAsync(String path, Object body)
      Sends a JSON POST request asynchronously.
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body object
      Returns:
      future with response
    • postJsonSmart

      public CompletableFuture<HttpResponse<String>> postJsonSmart(String path, Object body)
      Sends a JSON POST request, switching to async on blocking-sensitive threads.
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body object
      Returns:
      future with response
    • postJsonAsync

      public <T> CompletableFuture<T> postJsonAsync(String path, Object body, Class<T> type)
      Sends a JSON POST request asynchronously and deserializes the response.
      Type Parameters:
      T - type of result
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body object
      type - target type
      Returns:
      future with deserialized response
    • postJsonSmart

      public <T> CompletableFuture<T> postJsonSmart(String path, Object body, Class<T> type)
      Sends a JSON POST request and deserializes the response, switching to async on blocking-sensitive threads.
      Type Parameters:
      T - type of result
      Parameters:
      path - absolute URL or a path resolved against base URL
      body - request body object
      type - target type
      Returns:
      future with deserialized response
    • postMultipart

      public HttpResponse<String> postMultipart(String path, MultipartBody multipart) throws IOException, InterruptedException
      Sends a multipart/form-data POST request.
      Parameters:
      path - absolute URL or a path resolved against base URL
      multipart - multipart body builder
      Returns:
      HTTP response with string body
      Throws:
      IOException - if request fails
      InterruptedException - if the thread is interrupted
    • postMultipartAsync

      public CompletableFuture<HttpResponse<String>> postMultipartAsync(String path, MultipartBody multipart)
      Sends a multipart/form-data POST request asynchronously.
      Parameters:
      path - absolute URL or a path resolved against base URL
      multipart - multipart body builder
      Returns:
      future with response
    • postMultipartSmart

      public CompletableFuture<HttpResponse<String>> postMultipartSmart(String path, MultipartBody multipart)
      Sends a multipart/form-data POST request, switching to async on blocking-sensitive threads.
      Parameters:
      path - absolute URL or a path resolved against base URL
      multipart - multipart body builder
      Returns:
      future with response
    • downloadToFile

      public HttpResponse<Path> downloadToFile(String path, Path target) throws IOException, InterruptedException
      Downloads content into a file.
      Parameters:
      path - absolute URL or a path resolved against base URL
      target - target file path
      Returns:
      HTTP response with file body
      Throws:
      IOException - if request fails
      InterruptedException - if the thread is interrupted
    • downloadToFileAsync

      public CompletableFuture<HttpResponse<Path>> downloadToFileAsync(String path, Path target)
      Downloads content into a file asynchronously.
      Parameters:
      path - absolute URL or a path resolved against base URL
      target - target file path
      Returns:
      future with file response
    • downloadToFileSmart

      public CompletableFuture<HttpResponse<Path>> downloadToFileSmart(String path, Path target)
      Downloads content into a file, switching to async on blocking-sensitive threads.
      Parameters:
      path - absolute URL or a path resolved against base URL
      target - target file path
      Returns:
      future with file response
    • send

      public <T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> handler) throws IOException, InterruptedException
      Sends a request using the underlying client without automatic retries.
      Type Parameters:
      T - body type
      Parameters:
      request - request to send
      handler - body handler
      Returns:
      HTTP response
      Throws:
      IOException - if request fails
      InterruptedException - if the thread is interrupted
    • sendAsync

      public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> handler)
      Sends a request asynchronously using the underlying client without automatic retries.
      Type Parameters:
      T - body type
      Parameters:
      request - request to send
      handler - body handler
      Returns:
      future with response
    • sendSmart

      public <T> CompletableFuture<HttpResponse<T>> sendSmart(HttpRequest request, HttpResponse.BodyHandler<T> handler)
      Sends a request, switching to async on blocking-sensitive threads.
      Type Parameters:
      T - body type
      Parameters:
      request - request to send
      handler - body handler
      Returns:
      future with response
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable