Class RestClientService

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

public final class RestClientService extends Object
The RestClientService class provides methods for interacting with the WordPress REST API for creating, reading, updating, and deleting posts, pages, and other entities.

This class is a utility service that provides convenience methods for common operations on the WordPress REST API. It is not intended to be instantiated, and its methods are static.

Since:
0.1.0
Author:
Yoham Gabriel B.
  • Method Details

    • postCreate

      @Nullable public static @Nullable HttpResponse<String> postCreate(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode postPayload, SSLContext sslContext)
      Creates a new post in the WordPress site using the REST API.
      Parameters:
      apiBasePath - The base path of the WordPress REST API.
      username - The username for authentication.
      applicationPassword - The application password for authentication.
      postPayload - The JSON payload representing the post to be created.
      sslContext - The SSL context for the HTTPS connection.
      Returns:
      The HTTP response from the server if the request was successful, or null if the request failed.
    • postDelete

      @Nullable public static @Nullable HttpResponse<String> postDelete(String apiBasePath, String username, String applicationPassword, long postId, SSLContext sslContext)
      Deletes a post from the WordPress site using the REST API.
      Parameters:
      apiBasePath - The base path of the WordPress REST API.
      username - The username for authentication.
      applicationPassword - The application password for authentication.
      postId - The ID of the post to be deleted.
      sslContext - The SSL context for the HTTPS connection.
      Returns:
      The HTTP response from the server if the request was successful, or null if the request failed.
    • changePostStatus

      @Nullable public static @Nullable HttpResponse<String> changePostStatus(String apiBasePath, String username, String applicationPassword, long postId, tools.jackson.databind.JsonNode payload, SSLContext sslContext)
      Changes the status of a post on the WordPress site using the REST API.
      Parameters:
      apiBasePath - The base path of the WordPress REST API.
      username - The username for authentication.
      applicationPassword - The application password for authentication.
      postId - The ID of the post to have its status changed.
      payload - The JSON payload representing the new status of the post.
      sslContext - The SSL context for the HTTPS connection.
      Returns:
      The HTTP response from the server if the request was successful, or null if the request failed.
    • addTag

      @Nullable public static @Nullable HttpResponse<String> addTag(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode payload, SSLContext sslContext)
      Adds a tag to the WordPress site using the REST API.
      Parameters:
      apiBasePath - The base path of the WordPress REST API.
      username - The username for authentication.
      applicationPassword - The application password for authentication.
      payload - The JSON payload representing the new tag.
      sslContext - The SSL context for the HTTPS connection.
      Returns:
      The HTTP response from the server if the request was successful, or null if the request failed.
    • addCategory

      @Nullable public static @Nullable HttpResponse<String> addCategory(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode payload, SSLContext sslContext)
      Adds a category to the WordPress site using the REST API.
      Parameters:
      apiBasePath - The base path of the WordPress REST API.
      username - The username for authentication.
      applicationPassword - The application password for authentication.
      payload - The JSON payload representing the new category.
      sslContext - The SSL context for the HTTPS connection.
      Returns:
      The HTTP response from the server if the request was successful, or null if the request failed.
    • postGet

      @Nullable public static @Nullable HttpResponse<String> postGet(String apiBasePath, String username, String applicationPassword, long postId, SSLContext sslContext)
      Fetches a single post by its ID from the WordPress site using the REST API.
      Parameters:
      apiBasePath - The base path of the WordPress REST API.
      username - The username for authentication.
      applicationPassword - The application password for authentication.
      postId - The ID of the post to fetch.
      sslContext - The SSL context for the HTTPS connection.
      Returns:
      The HTTP response from the server if the request was successful, or null if the request failed.
    • uploadMedia

      @Nullable public static @Nullable HttpResponse<String> uploadMedia(String apiBasePath, String username, String applicationPassword, Path attachmentPath, @Nullable @Nullable tools.jackson.databind.JsonNode payload, SSLContext sslContext)
      Uploads a media file to the WordPress site using the REST API.

      Adding a payload to a media file is supported, however, the method will process two requests to the WordPress REST API. The first request will upload the media file, and the second request will update the media file with the provided payload.

      It is possible to set the payload to null and only upload the media file, in which case the method will do so in one request.

      Parameters:
      apiBasePath - The base path of the WordPress REST API.
      username - The username for authentication.
      applicationPassword - The application password for authentication.
      attachmentPath - The file path of the media to be uploaded.
      payload - optional JSON payload used to update the uploaded media metadata
      sslContext - The SSL context for the HTTPS connection.
      Returns:
      The HTTP response from the server if the request was successful, or null if the request failed.
      Throws:
      MediaUploadException - if the media upload request fails.