Package net.ygbstudio.powerwp4j.services
Class RestClientService
java.lang.Object
net.ygbstudio.powerwp4j.services.RestClientService
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.
- Author:
- Yoham Gabriel B.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<HttpResponse<String>> addCategory(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode payload, boolean ignoreSSLHandshakeException) Adds a category to the WordPress site using the REST API.static Optional<HttpResponse<String>> addTag(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode payload, boolean ignoreSSLHandshakeException) Adds a tag to the WordPress site using the REST API.static Optional<HttpResponse<String>> changePostStatus(String apiBasePath, String username, String applicationPassword, long postId, tools.jackson.databind.JsonNode payload, boolean ignoreSSLHandshakeException) Changes the status of a post on the WordPress site using the REST API.static Optional<HttpResponse<String>> postCreate(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode postPayload, boolean ignoreSSLHandshakeException) Creates a new post in the WordPress site using the REST API.static Optional<HttpResponse<String>> postDelete(String apiBasePath, String username, String applicationPassword, long postId, boolean ignoreSSLHandshakeException) Deletes a post from the WordPress site using the REST API.static Optional<HttpResponse<String>> postGet(String apiBasePath, String username, String applicationPassword, long postId, boolean ignoreSSLHandshakeException) Fetches a single post by its ID from the WordPress site using the REST API.static Optional<HttpResponse<String>> uploadMedia(String apiBasePath, String username, String applicationPassword, Path attachmentPath, @Nullable tools.jackson.databind.JsonNode payload, boolean ignoreSSLHandshakeException) Uploads a media file to the WordPress site using the REST API.
-
Method Details
-
postCreate
public static Optional<HttpResponse<String>> postCreate(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode postPayload, boolean ignoreSSLHandshakeException) 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.ignoreSSLHandshakeException- Whether to ignore SSL Handshake Exception, useful for testing purposes or local environments.- Returns:
- An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
-
postDelete
public static Optional<HttpResponse<String>> postDelete(String apiBasePath, String username, String applicationPassword, long postId, boolean ignoreSSLHandshakeException) 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.ignoreSSLHandshakeException- Whether to ignore SSL Handshake Exception, useful for testing purposes or local environments.- 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 static Optional<HttpResponse<String>> changePostStatus(String apiBasePath, String username, String applicationPassword, long postId, tools.jackson.databind.JsonNode payload, boolean ignoreSSLHandshakeException) 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.ignoreSSLHandshakeException- Whether to ignore SSL Handshake Exception, useful for testing purposes or local environments.- 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 static Optional<HttpResponse<String>> addTag(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode payload, boolean ignoreSSLHandshakeException) 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.ignoreSSLHandshakeException- Whether to ignore SSL Handshake Exception, useful for testing purposes or local environments.- 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 static Optional<HttpResponse<String>> addCategory(String apiBasePath, String username, String applicationPassword, tools.jackson.databind.JsonNode payload, boolean ignoreSSLHandshakeException) 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.ignoreSSLHandshakeException- Whether to ignore SSL Handshake Exception, useful for testing purposes or local environments.- Returns:
- An Optional containing the JSON response from the server if the request was successful, or an empty Optional if the request failed.
-
postGet
public static Optional<HttpResponse<String>> postGet(String apiBasePath, String username, String applicationPassword, long postId, boolean ignoreSSLHandshakeException) 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.ignoreSSLHandshakeException- Whether to ignore SSL Handshake Exception.- Returns:
- An Optional containing the JSON response from the server if the request was successful.
-
uploadMedia
public static Optional<HttpResponse<String>> uploadMedia(String apiBasePath, String username, String applicationPassword, Path attachmentPath, @Nullable @Nullable tools.jackson.databind.JsonNode payload, boolean ignoreSSLHandshakeException) 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
nulland 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 metadataignoreSSLHandshakeException- Whether to ignore SSL Handshake Exception, useful for testing purposes or local environments.- 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.
-