Package net.ygbstudio.powerwp4j.engine
Class WPRestClient
java.lang.Object
net.ygbstudio.powerwp4j.engine.WPRestClient
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 Summary
ConstructorsModifierConstructorDescriptionprotectedWPRestClient(WPSiteInfo siteInfo) protectedWPRestClient(WPSiteInfo siteInfo, boolean ignoreSSL) -
Method Summary
Modifier and TypeMethodDescriptionaddCategory(tools.jackson.databind.JsonNode payload) Adds a category to the WordPress site using the REST API.addTag(tools.jackson.databind.JsonNode payload) Adds a tag to the WordPress site using the REST API.changePostStatus(long postId, PostStatusEnum status) Changes the status of a post on the WordPress site using the REST API.createPost(tools.jackson.databind.JsonNode payload) Creates a new post in the WordPress site using the REST API.deletePost(long postId) Deletes a post from the WordPress site using the REST API.getPost(long id) Fetches a single post by its ID.static @NotNull WPRestClientof(WPSiteInfo siteInfo) Creates a new instance ofWPRestClientusing the providedWPSiteInfo.static @NotNull WPRestClienttestingOf(WPSiteInfo siteInfo, boolean ignoreSSL) Creates a new instance ofWPRestClientusing the providedWPSiteInfo.uploadMedia(Path attachmentPath) Uploads a media file to the WordPress site using the REST API.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.
-
Constructor Details
-
WPRestClient
-
WPRestClient
-
-
Method Details
-
of
@Contract(value="_ -> new", pure=true) @NotNull public static @NotNull WPRestClient of(WPSiteInfo siteInfo) Creates a new instance ofWPRestClientusing the providedWPSiteInfo.- Parameters:
siteInfo- the site information for the WordPress site- Returns:
- the newly created
WPRestClientinstance
-
testingOf
@Contract(value="_, _ -> new", pure=true) @NotNull public static @NotNull WPRestClient testingOf(WPSiteInfo siteInfo, boolean ignoreSSL) Creates a new instance ofWPRestClientusing the providedWPSiteInfo. 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 siteignoreSSL- whether to ignore SSL certificate issues. Suitable for local development only.- Returns:
- the newly created
WPRestClientinstance
-
createPost
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
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
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
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
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
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
Fetches a single post by its ID.- Parameters:
id- The ID of the post to fetch.- Returns:
- An Optional containing the
WPPostif found, or empty if not found or on error.
-