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 provide
convenience methods for common flows in the library.
- 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 Optional<HttpResponse<String>> clientSend(HttpRequest request, org.slf4j.Logger classLogger, boolean ignoreSSLHandshakeException) Sends a request to the WordPress REST API and returns the response wrapped in an Optional type.static Optional<HttpResponse<String>> connectGetWP(String url, String username, String applicationPassword, @Nullable org.slf4j.Logger classLogger) 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) 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) Processes a list of links using a client function and returns a result artifact.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
public static Optional<HttpResponse<String>> clientSend(HttpRequest request, org.slf4j.Logger classLogger, boolean ignoreSSLHandshakeException) Sends a request to the WordPress REST API and returns the response wrapped in an Optional type. 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- Instance of a class logger for error loggingignoreSSLHandshakeException- if set to true, SSLHandshakeExceptions will be ignored and the method will return an empty Optional- Returns:
- Optional containing the response from the REST API
-
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 Optional containing the 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- Returns:
- an HttpRequest object representing the POST request
-
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
public static Optional<HttpResponse<String>> connectGetWP(String url, String username, String applicationPassword, @Nullable @Nullable org.slf4j.Logger classLogger) 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- Returns:
- an Optional containing the response from the WordPress REST API
- Throws:
InvalidApiUrlException- if the URL is invalid
-
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) 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 results- 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) 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 resultsretryPred- the predicate to use for retrying resultsintervalUnit- the time unit for the retry intervalintervalTime- the time for the retry intervalretryAttempts- the number of retry attemptsretryFailedMessage- the message to use for retry failure- Returns:
- the result of the processing
-