diff --git a/Sources/WordPressKit/Services/PostServiceRemoteExtended.swift b/Sources/WordPressKit/Services/PostServiceRemoteExtended.swift index 4a531d80..fb7ed17e 100644 --- a/Sources/WordPressKit/Services/PostServiceRemoteExtended.swift +++ b/Sources/WordPressKit/Services/PostServiceRemoteExtended.swift @@ -9,6 +9,12 @@ public protocol PostServiceRemoteExtended: PostServiceRemote { /// - throws: ``PostServiceRemoteUpdatePostError`` or oher underlying errors /// (see ``WordPressAPIError``) func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost + + /// Permanently deletes a post with the given ID. + /// + /// - throws: ``PostServiceRemoteUpdatePostError`` or oher underlying errors + /// (see ``WordPressAPIError``) + func deletePost(withID postID: Int) async throws } public enum PostServiceRemoteUpdatePostError: Error { diff --git a/Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift b/Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift index 0af670fb..6637a785 100644 --- a/Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift +++ b/Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift @@ -28,6 +28,23 @@ extension PostServiceRemoteREST: PostServiceRemoteExtended { } } } + + public func deletePost(withID postID: Int) async throws { + let path = self.path(forEndpoint: "sites/\(siteID)/posts/\(postID)/delete", withVersion: ._1_1) + let result = await wordPressComRestApi.perform(.post, URLString: path) + switch result { + case .success: + return + case .failure(let error): + guard case .endpointError(let error) = error else { + throw error + } + switch error.apiErrorCode ?? "" { + case "unknown_post": throw PostServiceRemoteUpdatePostError.notFound + default: throw error + } + } + } } // Decodes the post in the background. diff --git a/Sources/WordPressKit/Services/PostServiceRemoteXMLRPC+Extended.swift b/Sources/WordPressKit/Services/PostServiceRemoteXMLRPC+Extended.swift index 9a81b1e3..b1bccf1a 100644 --- a/Sources/WordPressKit/Services/PostServiceRemoteXMLRPC+Extended.swift +++ b/Sources/WordPressKit/Services/PostServiceRemoteXMLRPC+Extended.swift @@ -34,6 +34,23 @@ extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended { } } + public func deletePost(withID postID: Int) async throws { + let parameters = xmlrpcArguments(withExtra: postID) as [AnyObject] + let result = await api.call(method: "wp.deletePost", parameters: parameters) + switch result { + case .success: + return + case .failure(let error): + guard case .endpointError(let error) = error else { + throw error + } + switch error.code ?? 0 { + case 404: throw PostServiceRemoteUpdatePostError.notFound + default: throw error + } + } + } + private func getPost(withID postID: NSNumber) async throws -> RemotePost { try await withUnsafeThrowingContinuation { continuation in getPostWithID(postID) { post in