Class WPRestClient

java.lang.Object
net.ygbstudio.powerwp4j.engine.WPRestClient

public class WPRestClient extends Object
This class provides a facade for accessing the WordPress REST API. It wraps the RestClientService class and provides a more user-friendly interface for interacting with the WordPress site.
Author:
Yoham Gabriel @ YGBStudio
  • Constructor Details

    • WPRestClient

      protected WPRestClient(WPSiteInfo siteInfo)
    • WPRestClient

      protected WPRestClient(WPSiteInfo siteInfo, boolean ignoreSSL)
  • Method Details

    • of

      @Contract(value="_ -> new", pure=true) @NotNull public static @NotNull WPRestClient of(WPSiteInfo siteInfo)
      Creates a new instance of WPRestClient using the provided WPSiteInfo.
      Parameters:
      siteInfo - the site information for the WordPress site
      Returns:
      the newly created WPRestClient instance
    • testingOf

      @Contract(value="_, _ -> new", pure=true) @NotNull public static @NotNull WPRestClient testingOf(WPSiteInfo siteInfo, boolean ignoreSSL)
      Creates a new instance of WPRestClient using the provided WPSiteInfo. This method is intended for testing purposes and can ignore SSL certificate issues for all REST API calls.
      Parameters:
      siteInfo - the site information for the WordPress site
      ignoreSSL - whether to ignore SSL certificate issues. Suitable for local development only.
      Returns:
      the newly created WPRestClient instance
    • createPost

      public Optional<HttpResponse<String>> createPost(tools.jackson.databind.JsonNode payload)
      Creates a new post in the WordPress site using the REST API.
      Parameters:
      payload - The JSON payload representing the post to be created.
      Returns:
      An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
    • deletePost

      public Optional<HttpResponse<String>> deletePost(long postId)
      Deletes a post from the WordPress site using the REST API.
      Parameters:
      postId - The ID of the post to be deleted.
      Returns:
      An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
    • changePostStatus

      public Optional<HttpResponse<String>> changePostStatus(long postId, PostStatusEnum status)
      Changes the status of a post on the WordPress site using the REST API.
      Parameters:
      postId - The ID of the post to have its status changed.
      status - The new status of the post.
      Returns:
      An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
    • addTag

      public Optional<HttpResponse<String>> addTag(tools.jackson.databind.JsonNode payload)
      Adds a tag to the WordPress site using the REST API.
      Parameters:
      payload - The JSON payload representing the new tag.
      Returns:
      An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
    • addCategory

      public Optional<HttpResponse<String>> addCategory(tools.jackson.databind.JsonNode payload)
      Adds a category to the WordPress site using the REST API.
      Parameters:
      payload - The JSON payload representing the new category.
      Returns:
      An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
    • uploadMedia

      public Optional<HttpResponse<String>> uploadMedia(Path attachmentPath)
      Uploads a media file to the WordPress site using the REST API.
      Parameters:
      attachmentPath - The file path of the media to be uploaded.
      Returns:
      An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
      Throws:
      MediaUploadException - if the media upload request fails.
    • uploadMedia

      public Optional<HttpResponse<String>> uploadMedia(Path attachmentPath, tools.jackson.databind.JsonNode payload)
      Uploads a media file to the WordPress site using the REST API and attaches a payload to the media file.
      Parameters:
      attachmentPath - The file path of the media to be uploaded.
      payload - The JSON payload representing the media.
      Returns:
      An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
      Throws:
      MediaUploadException - if the media upload request fails.
    • getPost

      public Optional<WPPost> getPost(long id)
      Fetches a single post by its ID.
      Parameters:
      id - The ID of the post to fetch.
      Returns:
      An Optional containing the WPPost if found, or empty if not found or on error.