Skip to content

Commit

Permalink
Add new delete endpoint (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
kean authored Apr 8, 2024
2 parents deeb89d + cd55758 commit 5bf57f8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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

0 comments on commit 5bf57f8

Please sign in to comment.