Package net.ygbstudio.powerwp4j.services
Class HttpRequestService
java.lang.Object
net.ygbstudio.powerwp4j.services.HttpRequestService
HttpRequestService is a utility class for interacting with the WordPress REST API and provides
convenience methods for common flows in the library.
- Since:
- 0.1.0
- Author:
- Yoham Gabriel B.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringMIME type used for JSON requests and responses. -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpRequestbuildWpDeleteRequest(String url, String username, String applicationPassword, @Nullable org.slf4j.Logger classLogger) Builds a DELETE request for the WordPress REST API.static HttpRequestbuildWpGetRequest(String url, String username, String applicationPassword, @Nullable org.slf4j.Logger classLogger) Builds a GET request for the WordPress REST API.static Optional<HttpRequest> buildWpPostRequest(String url, String username, String applicationPassword, @NotNull Path attachmentPath, @Nullable org.slf4j.Logger classLogger) Builds a POST request for the WordPress REST API to upload a media file.static HttpRequestbuildWpPostRequest(@NotNull tools.jackson.databind.JsonNode body, String url, String username, String applicationPassword, @Nullable org.slf4j.Logger classLogger) Builds a POST request for the WordPress REST API.static @NotNull HttpResponse<String> clientSend(@NotNull HttpRequest request, @NotNull org.slf4j.Logger classLogger, @NotNull SSLContext sslContext) Sends a request to the WordPress REST API and returns the response.static @NotNull HttpResponse<String> connectGetWP(String url, String username, String applicationPassword, @Nullable org.slf4j.Logger classLogger, @NotNull SSLContext sslContext) Connects to the WordPress REST API and returns the response.static <R> RlinkProcessor(@NotNull List<String> linkList, BiFunction<HttpClient, String, CompletableFuture<R>> clientProcedure, Predicate<? super R> filterPred, Collector<? super R, R, R> collector, @NotNull SSLContext sslContext) Processes a list of links using a client function and returns a result artifact.static <R> RlinkProcessor(List<String> linkList, BiFunction<HttpClient, String, CompletableFuture<R>> clientProcedure, Predicate<? super R> filterPred, Collector<? super R, R, R> collector, @Nullable Predicate<? super R> retryPred, @Nullable TimeUnit intervalUnit, long intervalTime, int retryAttempts, @Nullable Supplier<String> retryFailedMessage, @NotNull SSLContext sslContext) Processes a list of links using a client function and returns a result artifact with retry.static <E extends QueryParamEnum>
@NotNull StringmakeRequestURL(String apiBasePath, @Nullable Map<E, String> queryParams, @NotNull WPRestPath pathParam) Creates a request URL for the WordPress REST API.
-
Field Details
-
JSON_CONTENT_TYPE
MIME type used for JSON requests and responses.- See Also:
-
-
Method Details
-
makeRequestURL
@NotNull public static <E extends QueryParamEnum> @NotNull String makeRequestURL(String apiBasePath, @Nullable @Nullable Map<E, String> queryParams, @NotNull @NotNull WPRestPath pathParam) Creates a request URL for the WordPress REST API.- Type Parameters:
E- the query parameter enum type- Parameters:
apiBasePath- the base path for the WordPress REST APIqueryParams- the query parameters to be used in the requestpathParam- the path parameter to be used in the request- Returns:
- the request URL
-
clientSend
@NotNull public static @NotNull HttpResponse<String> clientSend(@NotNull @NotNull HttpRequest request, @NotNull @NotNull org.slf4j.Logger classLogger, @NotNull @NotNull SSLContext sslContext) Sends a request to the WordPress REST API and returns the response. This method is a wrapper around theHttpClientclass and is used to send requests with centralised error handling and resource management.- Parameters:
request-HttpRequestobject that will be sentclassLogger- logger for error loggingsslContext- the SSL context to use for the HTTPS connection- Returns:
HttpResponse<String>containing the response from the REST API; the trailingreturn nullis unreachable because all failure paths throwWPRequestException- Throws:
WPRequestException- if the request fails due to I/O or interruption
-
buildWpGetRequest
public static HttpRequest buildWpGetRequest(String url, String username, String applicationPassword, @Nullable @Nullable org.slf4j.Logger classLogger) Builds a GET request for the WordPress REST API.- Parameters:
url- the URL to connect tousername- the username for the WordPress siteapplicationPassword- the application password for the WordPress siteclassLogger- the logger to use for logging- Returns:
- an
HttpRequestobject representing the GET request
-
buildWpPostRequest
public static HttpRequest buildWpPostRequest(@NotNull @NotNull tools.jackson.databind.JsonNode body, String url, String username, String applicationPassword, @Nullable @Nullable org.slf4j.Logger classLogger) Builds a POST request for the WordPress REST API.- Parameters:
body- the JSON body of the requesturl- the URL to connect tousername- the username for the WordPress siteapplicationPassword- the application password for the WordPress siteclassLogger- the logger to use for logging- Returns:
- an HttpRequest object representing the POST request
-
buildWpPostRequest
public static Optional<HttpRequest> buildWpPostRequest(String url, String username, String applicationPassword, @NotNull @NotNull Path attachmentPath, @Nullable @Nullable org.slf4j.Logger classLogger) Builds a POST request for the WordPress REST API to upload a media file.- Parameters:
url- the URL to connect tousername- the username for the WordPress siteapplicationPassword- the application password for the WordPress siteattachmentPath- the path to the media file to be uploadedclassLogger- the logger to use for logging, ornull- Returns:
- an
Optionalcontaining the POST request, or empty if the media could not be processed due to I/O - Throws:
MediaUploadException- if the attachment path does not exist
-
buildWpDeleteRequest
public static HttpRequest buildWpDeleteRequest(String url, String username, String applicationPassword, @Nullable @Nullable org.slf4j.Logger classLogger) Builds a DELETE request for the WordPress REST API.- Parameters:
url- the URL to connect tousername- the username for the WordPress siteapplicationPassword- the application password for the WordPress siteclassLogger- the logger to use for logging- Returns:
- an HttpRequest object representing the DELETE request
-
connectGetWP
@NotNull public static @NotNull HttpResponse<String> connectGetWP(String url, String username, String applicationPassword, @Nullable @Nullable org.slf4j.Logger classLogger, @NotNull @NotNull SSLContext sslContext) throws InvalidApiUrlException Connects to the WordPress REST API and returns the response.- Parameters:
url- the URL to connect tousername- the username for the WordPress siteapplicationPassword- the application password for the WordPress siteclassLogger- the logger to use for logging, ornullsslContext- the SSL context to use for the HTTPS connection- Returns:
HttpResponsecontaining the response from the WordPress REST API- Throws:
InvalidApiUrlException- if the URL is invalidWPRequestException- if the request fails
-
linkProcessor
public static <R> R linkProcessor(@NotNull @NotNull List<String> linkList, BiFunction<HttpClient, String, CompletableFuture<R>> clientProcedure, Predicate<? super R> filterPred, Collector<? super R, R, R> collector, @NotNull @NotNull SSLContext sslContext) Processes a list of links using a client function and returns a result artifact.- Type Parameters:
R- the type of the result- Parameters:
linkList- the list of links to processclientProcedure- the client function to use for link processingfilterPred- the predicate to use for filtering resultscollector- the collector to use for collecting resultssslContext- SSL context for HTTPS connections- Returns:
- the result of the processing
-
linkProcessor
public static <R> R linkProcessor(List<String> linkList, BiFunction<HttpClient, String, CompletableFuture<R>> clientProcedure, Predicate<? super R> filterPred, Collector<? super R, R, R> collector, @Nullable @Nullable Predicate<? super R> retryPred, @Nullable @Nullable TimeUnit intervalUnit, long intervalTime, int retryAttempts, @Nullable @Nullable Supplier<String> retryFailedMessage, @NotNull @NotNull SSLContext sslContext) Processes a list of links using a client function and returns a result artifact with retry.- Type Parameters:
R- the type of the result- Parameters:
linkList- the list of links to processclientProcedure- the client function to use for link processingfilterPred- the predicate to use for filtering resultscollector- the collector to use for collecting resultsretryPred- predicate to test whether a retry is needed, ornullfor no retryintervalUnit- the time unit for the retry interval, ornullfor no delayintervalTime- the time for the retry intervalretryAttempts- the number of retry attemptsretryFailedMessage- supplier for the message logged when retries are exhausted, ornullsslContext- SSL context for HTTPS connections- Returns:
- the result of the processing
-