Package net.ygbstudio.powerwp4j.engine
Class WPRestClient
java.lang.Object
net.ygbstudio.powerwp4j.engine.WPRestClient
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 Summary
ConstructorsModifierConstructorDescriptionprotectedWPRestClient(WPSiteInfo siteInfo) Creates a client with the default SSL context.protectedWPRestClient(WPSiteInfo siteInfo, SSLContext sslContext) Creates a client with a custom SSL context. -
Method Summary
Modifier and TypeMethodDescription@Nullable HttpResponse<String> addCategory(tools.jackson.databind.JsonNode payload) Adds a category.@Nullable HttpResponse<String> addTag(tools.jackson.databind.JsonNode payload) Adds a tag.@Nullable HttpResponse<String> changePostStatus(long postId, PostStatusEnum status) Changes the status of a post by building a minimal payload viaWPBasicPayloadBuilderand delegating toRestClientService.changePostStatus(java.lang.String, java.lang.String, java.lang.String, long, tools.jackson.databind.JsonNode, javax.net.ssl.SSLContext).@Nullable HttpResponse<String> createPost(tools.jackson.databind.JsonNode payload) Creates a new post.@Nullable HttpResponse<String> deletePost(long postId) Deletes a post by ID.getPost(long id) Fetches a single post by ID.static @NotNull WPRestClientof(WPSiteInfo siteInfo) Creates a new instance ofWPRestClientusing the providedWPSiteInfo.static @NotNull WPRestClientof(WPSiteInfo siteInfo, SSLContext sslContext) Creates a new client with a custom SSL context.@Nullable HttpResponse<String> uploadMedia(Path attachmentPath) Uploads a media file.@Nullable HttpResponse<String> uploadMedia(Path attachmentPath, tools.jackson.databind.JsonNode payload) Uploads a media file with an attached JSON payload.
-
Constructor Details
-
WPRestClient
Creates a client with the default SSL context.- Parameters:
siteInfo- site credentials and base URL
-
WPRestClient
Creates a client with a custom SSL context.- Parameters:
siteInfo- site credentials and base URLsslContext- 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 ofWPRestClientusing the providedWPSiteInfo.- Parameters:
siteInfo- the site information for the WordPress site- Returns:
- the newly created
WPRestClientinstance
-
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 URLsslContext- SSL context for HTTPS requests- Returns:
- newly created instance
-
createPost
Creates a new post.- Parameters:
payload- JSON payload representing the post- Returns:
- HTTP response with the server body, or
nullif the request failed
-
deletePost
Deletes a post by ID.- Parameters:
postId- ID of the post to delete- Returns:
- HTTP response with the server body, or
nullif the request failed
-
changePostStatus
@Nullable public @Nullable HttpResponse<String> changePostStatus(long postId, PostStatusEnum status) Changes the status of a post by building a minimal payload viaWPBasicPayloadBuilderand delegating toRestClientService.changePostStatus(java.lang.String, java.lang.String, java.lang.String, long, tools.jackson.databind.JsonNode, javax.net.ssl.SSLContext).- Parameters:
postId- ID of the post to updatestatus- new status to set- Returns:
- HTTP response with the server body, or
nullif the request failed
-
addTag
Adds a tag.- Parameters:
payload- JSON payload representing the new tag- Returns:
- HTTP response with the server body, or
nullif 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
nullif the request failed
-
uploadMedia
Uploads a media file.- Parameters:
attachmentPath- file path of the media to upload- Returns:
- HTTP response with the server body, or
nullif 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 uploadpayload- JSON payload to attach to the media- Returns:
- HTTP response with the server body, or
nullif the request failed - Throws:
MediaUploadException- if the upload fails
-
getPost
Fetches a single post by ID.- Parameters:
id- ID of the post to fetch- Returns:
- optional containing the
WPPostif found, otherwise empty
-