This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPostServiceRemoteXMLRPC+Extended.swift
More file actions
79 lines (73 loc) · 3.39 KB
/
Copy pathPostServiceRemoteXMLRPC+Extended.swift
File metadata and controls
79 lines (73 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import Foundation
@_implementationOnly import wpxmlrpc
extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
public func post(withID postID: Int) async throws -> RemotePost {
let parameters = xmlrpcArguments(withExtra: postID) as [AnyObject]
let result = await api.call(method: "wp.getPost", parameters: parameters)
switch result {
case .success(let response):
return try await decodePost(from: response.body)
case .failure(let error):
if case .endpointError(let error) = error, error.code == 404 {
throw PostServiceRemoteError.notFound
}
throw error
}
}
public func createPost(with parameters: RemotePostCreateParameters) async throws -> RemotePost {
let dictionary = try makeParameters(from: RemotePostCreateParametersXMLRPCEncoder(parameters: parameters))
let parameters = xmlrpcArguments(withExtra: dictionary) as [AnyObject]
let response = try await api.call(method: "wp.newPost", parameters: parameters).get()
guard let postID = (response.body as? NSObject)?.wpkit_numericValue() else {
throw URLError(.unknown) // Should never happen
}
return try await post(withID: postID.intValue)
}
public func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost {
let dictionary = try makeParameters(from: RemotePostUpdateParametersXMLRPCEncoder(parameters: parameters))
let parameters = xmlrpcArguments(withExtraDefaults: [postID as NSNumber], andExtra: dictionary) as [AnyObject]
let result = await api.call(method: "wp.editPost", parameters: parameters)
switch result {
case .success:
return try await post(withID: postID)
case .failure(let error):
guard case .endpointError(let error) = error else {
throw error
}
switch error.code ?? 0 {
case 404: throw PostServiceRemoteError.notFound
case 409: throw PostServiceRemoteError.conflict
default: throw error
}
}
}
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):
if case .endpointError(let error) = error, error.code == 404 {
throw PostServiceRemoteError.notFound
}
throw error
}
}
}
private func decodePost(from object: AnyObject) async throws -> RemotePost {
guard let dictionary = object as? [AnyHashable: Any] else {
throw WordPressAPIError<WordPressComRestApiEndpointError>.unparsableResponse(response: nil, body: nil)
}
return PostServiceRemoteXMLRPC.remotePost(fromXMLRPCDictionary: dictionary)
}
private func makeParameters<T: Encodable>(from value: T) throws -> [String: AnyObject] {
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let data = try encoder.encode(value)
let object = try PropertyListSerialization.propertyList(from: data, format: nil)
guard let dictionary = object as? [String: AnyObject] else {
throw URLError(.unknown) // This should never happen
}
return dictionary
}