Package com.petarmc.lib.net
Class HttpClientWrapper
java.lang.Object
com.petarmc.lib.net.HttpClientWrapper
A simple wrapper around Java's HttpClient with support for retries and asycn execution.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final HttpClientprivate final ExecutorServiceprivate static final PLoggerprivate final int -
Constructor Summary
ConstructorsConstructorDescriptionHttpClientWrapper(int maxRetries) Creates a new HttpClientWrapper with a specified maximum number of retries. -
Method Summary
Modifier and TypeMethodDescriptionSends an async GET request to the specified URL.post(HttpRequest request) Sends an async POST request using the provided HttpRequest.private CompletableFuture<String> sendWithRetry(HttpRequest req, int attempt) voidshutdown()Shuts down the executor used for async HTTP requests.
-
Field Details
-
log
-
client
-
maxRetries
private final int maxRetries -
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
Sends an async GET request to the specified URL.- Parameters:
url- the target URL- Returns:
- a CompletableFuture containing the response body
-
post
Sends an async POST request using the provided HttpRequest.- Parameters:
request- the HttpRequest to send- Returns:
- a CompletableFuture containing the response body
-
sendWithRetry
-
shutdown
public void shutdown()Shuts down the executor used for async HTTP requests.
-