Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit a7fc344

Browse files
committed
Add PostServiceRemoteUpdatePostError
1 parent c504256 commit a7fc344

File tree

6 files changed

+44
-35
lines changed

6 files changed

+44
-35
lines changed

WordPressKit/PostServiceRemote.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,4 @@
103103
*/
104104
- (NSDictionary *)dictionaryWithRemoteOptions:(id <PostServiceRemoteOptions>)options;
105105

106-
/// Returns a remote post with the given data.
107-
+ (RemotePost *)remotePostFromJSONDictionary:(NSDictionary *)jsonPost;
108-
109106
@end

WordPressKit/PostServiceRemoteExtended.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,15 @@ public protocol PostServiceRemoteExtended: PostServiceRemote {
55
func createPost(with parameters: RemotePostCreateParameters) async throws -> RemotePost
66

77
/// Performs a partial update to the existing post.
8+
///
9+
/// - throws: ``PostServiceRemoteUpdatePostError`` or oher underlying errors
10+
/// (see ``WordPressAPIError``)
811
func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost
912
}
13+
14+
public enum PostServiceRemoteUpdatePostError: Error {
15+
/// 409 (Conflict)
16+
case conflict
17+
/// 404 (Not Found)
18+
case notFound
19+
}

WordPressKit/PostServiceRemoteREST+Extended.swift

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,37 @@ extension PostServiceRemoteREST: PostServiceRemoteExtended {
55
let path = self.path(forEndpoint: "sites/\(siteID)/posts/new?context=edit", withVersion: ._1_2)
66
let parameters = try makeParameters(from: RemotePostCreateParametersWordPressComEncoder(parameters: parameters))
77

8-
let response = try await withUnsafeThrowingContinuation { continuation in
9-
wordPressComRestApi.POST(path, parameters: parameters) { object, _ in
10-
continuation.resume(returning: object)
11-
} failure: { error, _ in
12-
continuation.resume(throwing: error)
13-
}
14-
}
15-
return try await decodePost(from: response)
8+
let response = try await wordPressComRestApi.perform(.post, URLString: path, parameters: parameters).get()
9+
return try await decodePost(from: response.body)
1610
}
1711

1812
public func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost {
1913
let path = self.path(forEndpoint: "sites/\(siteID)/posts/\(postID)?context=edit", withVersion: ._1_2)
2014
let parameters = try makeParameters(from: RemotePostUpdateParametersWordPressComEncoder(parameters: parameters))
2115

22-
let response = try await withUnsafeThrowingContinuation { continuation in
23-
wordPressComRestApi.POST(path, parameters: parameters) { object, _ in
24-
continuation.resume(returning: object)
25-
} failure: { error, _ in
26-
continuation.resume(throwing: error)
16+
let result = await wordPressComRestApi.perform(.post, URLString: path, parameters: parameters)
17+
switch result {
18+
case .success(let response):
19+
return try await decodePost(from: response.body)
20+
case .failure(let error):
21+
guard case .endpointError(let error) = error else {
22+
throw error
23+
}
24+
switch error.apiErrorCode ?? "" {
25+
case "unknown_post": throw PostServiceRemoteUpdatePostError.notFound
26+
case "old-revision": throw PostServiceRemoteUpdatePostError.conflict
27+
default: throw error
2728
}
2829
}
29-
return try await decodePost(from: response)
3030
}
3131
}
3232

3333
// Decodes the post in the background.
3434
private func decodePost(from object: AnyObject) async throws -> RemotePost {
35-
guard let dictionary = object as? [AnyHashable: Any],
36-
let post = PostServiceRemoteREST.remotePost(fromJSONDictionary: dictionary) else {
35+
guard let dictionary = object as? [AnyHashable: Any] else {
3736
throw WordPressAPIError<WordPressComRestApiEndpointError>.unparsableResponse(response: nil, body: nil)
3837
}
39-
return post
38+
return PostServiceRemoteREST.remotePost(fromJSONDictionary: dictionary)
4039
}
4140

4241
private func makeParameters<T: Encodable>(from value: T) throws -> [String: AnyObject] {

WordPressKit/PostServiceRemoteREST.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,7 @@
7676
success:(void (^ _Nullable)(NSArray<RemoteLikeUser *> * _Nonnull users, NSNumber * _Nonnull found))success
7777
failure:(void (^ _Nullable)(NSError * _Nullable))failure;
7878

79+
/// Returns a remote post with the given data.
80+
+ (nonnull RemotePost *)remotePostFromJSONDictionary:(nonnull NSDictionary *)jsonPost;
81+
7982
@end

WordPressKit/PostServiceRemoteXMLRPC+Extended.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
55
public func createPost(with parameters: RemotePostCreateParameters) async throws -> RemotePost {
66
let dictionary = try makeParameters(from: RemotePostCreateParametersXMLRPCEncoder(parameters: parameters, type: .post))
77
let parameters = xmlrpcArguments(withExtra: dictionary) as [AnyObject]
8-
let response = try await withUnsafeThrowingContinuation { continuation in
9-
api.callMethod("metaWeblog.newPost", parameters: parameters) { object, _ in
10-
continuation.resume(returning: object)
11-
} failure: { error, _ in
12-
continuation.resume(throwing: error)
13-
}
14-
}
15-
guard let postID = (response as? NSNumber) else {
8+
let response = try await api.call(method: "metaWeblog.newPost", parameters: parameters).get()
9+
guard let postID = (response.body as? NSNumber) else {
1610
throw URLError(.unknown) // Should never happen
1711
}
1812
return try await getPost(withID: postID)
@@ -24,14 +18,20 @@ extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
2418
if parameters.count > 0 {
2519
parameters[0] = postID as NSNumber
2620
}
27-
try await withUnsafeThrowingContinuation { continuation in
28-
api.callMethod("metaWeblog.editPost", parameters: parameters) { _, _ in
29-
continuation.resume(returning: ())
30-
} failure: { error, _ in
31-
continuation.resume(throwing: error)
21+
let result = await api.call(method: "metaWeblog.editPost", parameters: parameters)
22+
switch result {
23+
case .success:
24+
return try await getPost(withID: postID as NSNumber)
25+
case .failure(let error):
26+
guard case .endpointError(let error) = error else {
27+
throw error
28+
}
29+
switch error.code ?? 0 {
30+
case 404: throw PostServiceRemoteUpdatePostError.notFound
31+
case 409: throw PostServiceRemoteUpdatePostError.conflict
32+
default: throw error
3233
}
3334
}
34-
return try await getPost(withID: postID as NSNumber)
3535
}
3636

3737
private func getPost(withID postID: NSNumber) async throws -> RemotePost {

WordPressKit/WordPressOrgXMLRPCApi.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ open class WordPressOrgXMLRPCApi: NSObject {
180180
/// - Parameters:
181181
/// - streaming: set to `true` if there are large data (i.e. uploading files) in given `parameters`. `false` by default.
182182
/// - Returns: A `Result` type that contains the XMLRPC success or failure result.
183-
func call(method: String, parameters: [AnyObject]?, fulfilling progress: Progress, streaming: Bool = false) async -> WordPressAPIResult<HTTPAPIResponse<AnyObject>, WordPressOrgXMLRPCApiFault> {
183+
func call(method: String, parameters: [AnyObject]?, fulfilling progress: Progress? = nil, streaming: Bool = false) async -> WordPressAPIResult<HTTPAPIResponse<AnyObject>, WordPressOrgXMLRPCApiFault> {
184184
let session = streaming ? uploadURLSession : urlSession
185185
let builder = HTTPRequestBuilder(url: endpoint)
186186
.method(.post)

0 commit comments

Comments
 (0)