Class WPRestClient

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

public class WPRestClient extends Object
Facade for the WordPress REST API.

Wraps RestClientService with a user-friendly, stateful client bound to a WPSiteInfo and an SSLContext. Obtain instances via of(WPSiteInfo) or of(WPSiteInfo, SSLContext).

Since:
0.1.0
Author:
Yoham Gabriel @ YGBStudio
  • Constructor Details

    • WPRestClient

      protected WPRestClient(WPSiteInfo siteInfo)
      Creates a client with the default SSL context.
      Parameters:
      siteInfo - site credentials and base URL
    • WPRestClient

      protected WPRestClient(WPSiteInfo siteInfo, SSLContext sslContext)
      Creates a client with a custom SSL context.
      Parameters:
      siteInfo - site credentials and base URL
      sslContext - SSL context for HTTPS requests
  • 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
    • of

      @Contract(value="_, _ -> new", pure=true) @NotNull public static @NotNull WPRestClient of(WPSiteInfo siteInfo, SSLContext sslContext)
      Creates a new client with a custom SSL context.
      Parameters:
      siteInfo - site credentials and base URL
      sslContext - SSL context for HTTPS requests
      Returns:
      newly created instance
    • createPost

      @Nullable public @Nullable HttpResponse<String> createPost(tools.jackson.databind.JsonNode payload)
      Creates a new post.
      Parameters:
      payload - JSON payload representing the post
      Returns:
      HTTP response with the server body, or null if the request failed
    • deletePost

      @Nullable public @Nullable HttpResponse<String> deletePost(long postId)
      Deletes a post by ID.
      Parameters:
      postId - ID of the post to delete
      Returns:
      HTTP response with the server body, or null if the request failed
    • changePostStatus

      @Nullable public @Nullable HttpResponse<String> changePostStatus(long postId, PostStatusEnum status)
      Parameters:
      postId - ID of the post to update
      status - new status to set
      Returns:
      HTTP response with the server body, or null if the request failed
    • addTag

      @Nullable public @Nullable HttpResponse<String> addTag(tools.jackson.databind.JsonNode payload)
      Adds a tag.
      Parameters:
      payload - JSON payload representing the new tag
      Returns:
      HTTP response with the server body, or null if the request failed
    • addCategory

      @Nullable public @Nullable HttpResponse<String> addCategory(tools.jackson.databind.JsonNode payload)
      Adds a category.
      Parameters:
      payload - JSON payload representing the new category
      Returns:
      HTTP response with the server body, or null if the request failed
    • uploadMedia

      @Nullable public @Nullable HttpResponse<String> uploadMedia(Path attachmentPath)
      Uploads a media file.
      Parameters:
      attachmentPath - file path of the media to upload
      Returns:
      HTTP response with the server body, or null if the request failed
      Throws:
      MediaUploadException - if the upload fails
    • uploadMedia

      @Nullable public @Nullable HttpResponse<String> uploadMedia(Path attachmentPath, tools.jackson.databind.JsonNode payload)
      Uploads a media file with an attached JSON payload.
      Parameters:
      attachmentPath - file path of the media to upload
      payload - JSON payload to attach to the media
      Returns:
      HTTP response with the server body, or null if the request failed
      Throws:
      MediaUploadException - if the upload fails
    • getPost

      public Optional<WPPost> getPost(long id)
      Fetches a single post by ID.
      Parameters:
      id - ID of the post to fetch
      Returns:
      optional containing the WPPost if found, otherwise empty