Class HttpClient
java.lang.Object
org.nanonative.nano.services.http.logic.HttpClient
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final HttpClientprotected final Contextprotected final intprotected final int -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newHttpClientwith default settings.HttpClient(Context context) Constructs a newHttpClientwith the provided context.HttpClient(Context context, HttpClient client) Constructs a newHttpClientwith the optional provided context and customHttpClient. -
Method Summary
Modifier and TypeMethodDescriptionprotected HttpObjectcircuitBreaker(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.client()longReturns the connection timeout in milliseconds configured for thisHttpClient.context()Returns the context used for configuring thisHttpClient.booleanReturns whether thisHttpClientfollows redirects.intReturns the read timeout in milliseconds configured for thisHttpClient.protected HttpObjectresponseOf(HttpResponse<byte[]> httpResponse, HttpObject response) intretries()Returns the number of retries configured for thisHttpClient.protected HttpObjectsend(int attempt, HttpRequest request, HttpObject response, Consumer<HttpObject> callback) send(HttpRequest request) Sends an HTTP request using the providedHttpObjectorHttpRequest.send(HttpRequest request, Consumer<HttpObject> callback) Sends an HTTP request using the providedHttpObjectorHttpRequest.toString()version()Returns theHttpClient.Versionused by thisHttpClient.
-
Field Details
-
context
-
client
-
retries
protected final int retries -
readTimeoutMs
protected final int readTimeoutMs
-
-
Constructor Details
-
HttpClient
public HttpClient()Constructs a newHttpClientwith default settings. -
HttpClient
Constructs a newHttpClientwith the provided context.- Parameters:
context- the context to use for configuration
-
HttpClient
Constructs a newHttpClientwith the optional provided context and customHttpClient.- Parameters:
context- the context to use for configurationclient- the custom HttpClient instance to use
-
-
Method Details
-
context
Returns the context used for configuring thisHttpClient.- Returns:
- the context used for configuring this
HttpClient
-
retries
public int retries()Returns the number of retries configured for thisHttpClient.- Returns:
- the number of retries
-
followRedirects
public boolean followRedirects()Returns whether thisHttpClientfollows redirects.- Returns:
trueif redirects are followed,falseotherwise
-
readTimeoutMs
public int readTimeoutMs()Returns the read timeout in milliseconds configured for thisHttpClient.- Returns:
- the read timeout in milliseconds
-
connectionTimeoutMs
public long connectionTimeoutMs()Returns the connection timeout in milliseconds configured for thisHttpClient.- Returns:
- the connection timeout in milliseconds
-
version
Returns theHttpClient.Versionused by thisHttpClient.- Returns:
- the
HttpClient.Version
-
client
-
send
Sends an HTTP request using the providedHttpObjectorHttpRequest. For async processing, use thesend(HttpRequest, Consumer)method.- Parameters:
request- theHttpObjectorHttpRequestrepresenting the HTTP request to send- Returns:
- the response as an
HttpObject
-
send
Sends an HTTP request using the providedHttpObjectorHttpRequest. If a response listener is provided, it processes the response asynchronously.- Parameters:
request- theHttpObjectorHttpRequestrepresenting the HTTP request to sendcallback- 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
-
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- TheHttpObjectrepresenting the original HTTP request.response- TheHttpObjectto populate with the response upon successful request completion.throwable- TheThrowablethat triggered the need for a retry.- Returns:
- A modified
HttpObjectcontaining the result of the retry attempts. If all retries are exhausted without success, it returns theHttpObjectpopulated with the failure information.
-
toString
-