Class HttpClient

java.lang.Object
org.nanonative.nano.services.http.logic.HttpClient

public class HttpClient extends Object
  • Field Details

    • context

      protected final Context context
    • client

      protected final HttpClient client
    • retries

      protected final int retries
    • readTimeoutMs

      protected final int readTimeoutMs
  • Constructor Details

    • HttpClient

      public HttpClient()
      Constructs a new HttpClient with default settings.
    • HttpClient

      public HttpClient(Context context)
      Constructs a new HttpClient with the provided context.
      Parameters:
      context - the context to use for configuration
    • HttpClient

      public HttpClient(Context context, HttpClient client)
      Constructs a new HttpClient with the optional provided context and custom HttpClient.
      Parameters:
      context - the context to use for configuration
      client - the custom HttpClient instance to use
  • Method Details

    • context

      public Context context()
      Returns the context used for configuring this HttpClient.
      Returns:
      the context used for configuring this HttpClient
    • retries

      public int retries()
      Returns the number of retries configured for this HttpClient.
      Returns:
      the number of retries
    • followRedirects

      public boolean followRedirects()
      Returns whether this HttpClient follows redirects.
      Returns:
      true if redirects are followed, false otherwise
    • readTimeoutMs

      public int readTimeoutMs()
      Returns the read timeout in milliseconds configured for this HttpClient.
      Returns:
      the read timeout in milliseconds
    • connectionTimeoutMs

      public long connectionTimeoutMs()
      Returns the connection timeout in milliseconds configured for this HttpClient.
      Returns:
      the connection timeout in milliseconds
    • version

      public HttpClient.Version version()
      Returns the HttpClient.Version used by this HttpClient.
      Returns:
      the HttpClient.Version
    • client

      public HttpClient client()
    • send

      public HttpObject send(HttpRequest request)
      Sends an HTTP request using the provided HttpObject or HttpRequest. For async processing, use the send(HttpRequest, Consumer) method.
      Parameters:
      request - the HttpObject or HttpRequest representing the HTTP request to send
      Returns:
      the response as an HttpObject
    • send

      public HttpObject send(HttpRequest request, Consumer<HttpObject> callback)
      Sends an HTTP request using the provided HttpObject or HttpRequest. If a response listener is provided, it processes the response asynchronously.
      Parameters:
      request - the HttpObject or HttpRequest representing the HTTP request to send
      callback - an optional consumer to process the response asynchronously
      Returns:
      the response as an HttpObject
    • send

      protected HttpObject send(int attempt, HttpRequest request, HttpObject response, Consumer<HttpObject> callback)
    • responseOf

      protected HttpObject responseOf(HttpResponse<byte[]> httpResponse, HttpObject response)
    • circuitBreaker

      protected HttpObject circuitBreaker(int attempt, HttpRequest request, HttpObject response, Consumer<HttpObject> callback, Throwable throwable)
      Implements a circuit breaker pattern to handle retries for HTTP requests in case of failures. This method attempts to resend the request after a delay that increases exponentially with the number of attempts. If the maximum number of retries is reached, it logs the failure and stops retrying.
      Parameters:
      attempt - The current retry attempt number.
      request - The HttpObject representing the original HTTP request.
      response - The HttpObject to populate with the response upon successful request completion.
      throwable - The Throwable that triggered the need for a retry.
      Returns:
      A modified HttpObject containing the result of the retry attempts. If all retries are exhausted without success, it returns the HttpObject populated with the failure information.
    • toString

      public String toString()
      Overrides:
      toString in class Object