Class HttpRequestService

java.lang.Object
net.ygbstudio.powerwp4j.services.HttpRequestService

public final class HttpRequestService extends Object
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 Details

    • JSON_CONTENT_TYPE

      public static final String 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 API
      queryParams - the query parameters to be used in the request
      pathParam - 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 the HttpClient class and is used to send requests with centralised error handling and resource management.
      Parameters:
      request - HttpRequest object that will be sent
      classLogger - Instance of a class logger for error logging
      ignoreSSLHandshakeException - 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 to
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
      classLogger - 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 request
      url - the URL to connect to
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
      classLogger - 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 to
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
      attachmentPath - the path to the media file to be uploaded
      classLogger - 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 to
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
      classLogger - 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 to
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
      classLogger - 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 process
      clientProcedure - the client function to use for link processing
      filterPred - the predicate to use for filtering results
      collector - 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 process
      clientProcedure - the client function to use for link processing
      filterPred - the predicate to use for filtering results
      collector - the collector to use for collecting results
      retryPred - the predicate to use for retrying results
      intervalUnit - the time unit for the retry interval
      intervalTime - the time for the retry interval
      retryAttempts - the number of retry attempts
      retryFailedMessage - the message to use for retry failure
      Returns:
      the result of the processing