Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new delete endpoint #776

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/WordPressKit/Services/PostServiceRemoteExtended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down