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 provides convenience methods for common flows in the library.
Since:
0.1.0
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

      @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 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 - logger for error logging
      sslContext - the SSL context to use for the HTTPS connection
      Returns:
      HttpResponse<String> containing the response from the REST API; the trailing return null is unreachable because all failure paths throw WPRequestException
      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 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 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 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, or null
      Returns:
      an Optional containing 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 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

      @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 to
      username - the username for the WordPress site
      applicationPassword - the application password for the WordPress site
      classLogger - the logger to use for logging, or null
      sslContext - the SSL context to use for the HTTPS connection
      Returns:
      HttpResponse containing the response from the WordPress REST API
      Throws:
      InvalidApiUrlException - if the URL is invalid
      WPRequestException - 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 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
      sslContext - 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 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 - predicate to test whether a retry is needed, or null for no retry
      intervalUnit - the time unit for the retry interval, or null for no delay
      intervalTime - the time for the retry interval
      retryAttempts - the number of retry attempts
      retryFailedMessage - supplier for the message logged when retries are exhausted, or null
      sslContext - SSL context for HTTPS connections
      Returns:
      the result of the processing