Class MagicHttpClient
java.lang.Object
dev.ua.theroer.magicutils.http.MagicHttpClient
- All Implemented Interfaces:
AutoCloseable
MagicUtils HTTP client with retries, config defaults, and JSON mapping.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic MagicHttpClient.BuilderCreates a builder without auto-loading configuration.static MagicHttpClient.Builderbuilder(Platform platform, ConfigManager configManager) Creates a builder that loads configuration from the provided manager.voidclose()config()Returns the resolved configuration instance.downloadToFile(String path, Path target) Downloads content into a file.downloadToFileAsync(String path, Path target) Downloads content into a file asynchronously.downloadToFileSmart(String path, Path target) Downloads content into a file, switching to async on blocking-sensitive threads.Sends a GET request and returns the response body as string.Sends a GET request asynchronously.<T> TSends a GET request and deserializes the JSON response into a type.<T> CompletableFuture<T> getJsonAsync(String path, Class<T> type) Sends a GET request asynchronously and deserializes JSON response.<T> CompletableFuture<T> getJsonSmart(String path, Class<T> type) Sends a GET request and deserializes JSON response, switching to async on blocking-sensitive threads.Sends a GET request, switching to async on blocking-sensitive threads.com.fasterxml.jackson.databind.ObjectMappermapper()Returns the configured Jackson mapper.Sends a plain-text POST request.Sends a plain-text POST request asynchronously.Sends a JSON POST request.<T> TSends a JSON POST request and deserializes the response.postJsonAsync(String path, Object body) Sends a JSON POST request asynchronously.<T> CompletableFuture<T> postJsonAsync(String path, Object body, Class<T> type) Sends a JSON POST request asynchronously and deserializes the response.postJsonSmart(String path, Object body) Sends a JSON POST request, switching to async on blocking-sensitive threads.<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.postMultipart(String path, MultipartBody multipart) Sends a multipart/form-data POST request.postMultipartAsync(String path, MultipartBody multipart) Sends a multipart/form-data POST request asynchronously.postMultipartSmart(String path, MultipartBody multipart) Sends a multipart/form-data POST request, switching to async on blocking-sensitive threads.Sends a plain-text POST request, switching to async on blocking-sensitive threads.Creates a request builder with defaults (base URL, headers, timeouts).<T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> handler) Sends a request using the underlying client without automatic retries.<T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> handler) Sends a request asynchronously using the underlying client without automatic retries.<T> CompletableFuture<HttpResponse<T>> sendSmart(HttpRequest request, HttpResponse.BodyHandler<T> handler) Sends a request, switching to async on blocking-sensitive threads.
-
Method Details
-
builder
Creates a builder that loads configuration from the provided manager.- Parameters:
platform- platform used for loggingconfigManager- configuration manager used to registerHttpClientConfig- Returns:
- a new builder instance
-
builder
Creates a builder without auto-loading configuration.- Parameters:
platform- platform used for logging- Returns:
- a new builder instance
-
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
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 failsInterruptedException- if the thread is interrupted
-
getAsync
Sends a GET request asynchronously.- Parameters:
path- absolute URL or a path resolved against base URL- Returns:
- future with response
-
getSmart
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
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 URLtype- target type- Returns:
- deserialized response
- Throws:
IOException- if request failsInterruptedException- if the thread is interrupted
-
getJsonAsync
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 URLtype- target type- Returns:
- future with deserialized response
-
getJsonSmart
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 URLtype- target type- Returns:
- future with deserialized response
-
post
Sends a plain-text POST request.- Parameters:
path- absolute URL or a path resolved against base URLbody- request body- Returns:
- HTTP response with string body
- Throws:
IOException- if request failsInterruptedException- if the thread is interrupted
-
postAsync
Sends a plain-text POST request asynchronously.- Parameters:
path- absolute URL or a path resolved against base URLbody- request body- Returns:
- future with response
-
postSmart
Sends a plain-text POST request, switching to async on blocking-sensitive threads.- Parameters:
path- absolute URL or a path resolved against base URLbody- 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 URLbody- request body object- Returns:
- HTTP response with string body
- Throws:
IOException- if request failsInterruptedException- 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 URLbody- request body objecttype- target type- Returns:
- deserialized response
- Throws:
IOException- if request failsInterruptedException- if the thread is interrupted
-
postJsonAsync
Sends a JSON POST request asynchronously.- Parameters:
path- absolute URL or a path resolved against base URLbody- request body object- Returns:
- future with response
-
postJsonSmart
Sends a JSON POST request, switching to async on blocking-sensitive threads.- Parameters:
path- absolute URL or a path resolved against base URLbody- request body object- Returns:
- future with response
-
postJsonAsync
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 URLbody- request body objecttype- target type- Returns:
- future with deserialized response
-
postJsonSmart
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 URLbody- request body objecttype- 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 URLmultipart- multipart body builder- Returns:
- HTTP response with string body
- Throws:
IOException- if request failsInterruptedException- 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 URLmultipart- 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 URLmultipart- 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 URLtarget- target file path- Returns:
- HTTP response with file body
- Throws:
IOException- if request failsInterruptedException- if the thread is interrupted
-
downloadToFileAsync
Downloads content into a file asynchronously.- Parameters:
path- absolute URL or a path resolved against base URLtarget- target file path- Returns:
- future with file response
-
downloadToFileSmart
Downloads content into a file, switching to async on blocking-sensitive threads.- Parameters:
path- absolute URL or a path resolved against base URLtarget- 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 sendhandler- body handler- Returns:
- HTTP response
- Throws:
IOException- if request failsInterruptedException- 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 sendhandler- 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 sendhandler- body handler- Returns:
- future with response
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-