From d17587a310bb41e15b5f0a666d7ae93b156f7242 Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Wed, 10 Apr 2024 07:46:23 +1000 Subject: [PATCH] Remove `PostServiceRemoteExtended` and implementations They will be moved into WordPress and Jetpack. The reason for this is that they are only used in WordPress and Jetpack and that they are Swift extensions of Objective-C types which we are trying to isolate. It's simpler to move these leaf types into the only consumer that uses them than trying to re-architect them. --- .../Services/PostServiceRemoteExtended.swift | 25 ------ .../PostServiceRemoteREST+Extended.swift | 67 ---------------- .../PostServiceRemoteXMLRPC+Extended.swift | 77 ------------------- WordPressKit.xcodeproj/project.pbxproj | 12 --- 4 files changed, 181 deletions(-) delete mode 100644 Sources/WordPressKit/Services/PostServiceRemoteExtended.swift delete mode 100644 Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift delete mode 100644 Sources/WordPressKit/Services/PostServiceRemoteXMLRPC+Extended.swift diff --git a/Sources/WordPressKit/Services/PostServiceRemoteExtended.swift b/Sources/WordPressKit/Services/PostServiceRemoteExtended.swift deleted file mode 100644 index fb7ed17e..00000000 --- a/Sources/WordPressKit/Services/PostServiceRemoteExtended.swift +++ /dev/null @@ -1,25 +0,0 @@ -import Foundation - -public protocol PostServiceRemoteExtended: PostServiceRemote { - /// Creates a new post with the given parameters. - func createPost(with parameters: RemotePostCreateParameters) async throws -> RemotePost - - /// Performs a partial update to the existing post. - /// - /// - 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 { - /// 409 (Conflict) - case conflict - /// 404 (Not Found) - case notFound -} diff --git a/Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift b/Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift deleted file mode 100644 index 6637a785..00000000 --- a/Sources/WordPressKit/Services/PostServiceRemoteREST+Extended.swift +++ /dev/null @@ -1,67 +0,0 @@ -import Foundation - -extension PostServiceRemoteREST: PostServiceRemoteExtended { - public func createPost(with parameters: RemotePostCreateParameters) async throws -> RemotePost { - let path = self.path(forEndpoint: "sites/\(siteID)/posts/new?context=edit", withVersion: ._1_2) - let parameters = try makeParameters(from: RemotePostCreateParametersWordPressComEncoder(parameters: parameters)) - - let response = try await wordPressComRestApi.perform(.post, URLString: path, parameters: parameters).get() - return try await decodePost(from: response.body) - } - - public func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost { - let path = self.path(forEndpoint: "sites/\(siteID)/posts/\(postID)?context=edit", withVersion: ._1_2) - let parameters = try makeParameters(from: RemotePostUpdateParametersWordPressComEncoder(parameters: parameters)) - - let result = await wordPressComRestApi.perform(.post, URLString: path, parameters: parameters) - switch result { - case .success(let response): - return try await decodePost(from: response.body) - case .failure(let error): - guard case .endpointError(let error) = error else { - throw error - } - switch error.apiErrorCode ?? "" { - case "unknown_post": throw PostServiceRemoteUpdatePostError.notFound - case "old-revision": throw PostServiceRemoteUpdatePostError.conflict - default: throw error - } - } - } - - 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. -private func decodePost(from object: AnyObject) async throws -> RemotePost { - guard let dictionary = object as? [AnyHashable: Any] else { - throw WordPressAPIError.unparsableResponse(response: nil, body: nil) - } - return PostServiceRemoteREST.remotePost(fromJSONDictionary: dictionary) -} - -private func makeParameters(from value: T) throws -> [String: AnyObject] { - let encoder = JSONEncoder() - encoder.dateEncodingStrategy = .formatted(.wordPressCom) - let data = try encoder.encode(value) - let object = try JSONSerialization.jsonObject(with: data) - guard let dictionary = object as? [String: AnyObject] else { - throw URLError(.unknown) // This should never happen - } - return dictionary -} diff --git a/Sources/WordPressKit/Services/PostServiceRemoteXMLRPC+Extended.swift b/Sources/WordPressKit/Services/PostServiceRemoteXMLRPC+Extended.swift deleted file mode 100644 index b1bccf1a..00000000 --- a/Sources/WordPressKit/Services/PostServiceRemoteXMLRPC+Extended.swift +++ /dev/null @@ -1,77 +0,0 @@ -import Foundation -import wpxmlrpc - -extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended { - 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: "metaWeblog.newPost", parameters: parameters).get() - guard let postID = (response.body as? NSObject)?.numericValue() else { - throw URLError(.unknown) // Should never happen - } - return try await getPost(withID: postID) - } - - public func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost { - let dictionary = try makeParameters(from: RemotePostUpdateParametersXMLRPCEncoder(parameters: parameters)) - var parameters = xmlrpcArguments(withExtra: dictionary) as [AnyObject] - if parameters.count > 0 { - parameters[0] = postID as NSNumber - } - let result = await api.call(method: "metaWeblog.editPost", parameters: parameters) - switch result { - case .success: - return try await getPost(withID: postID as NSNumber) - case .failure(let error): - guard case .endpointError(let error) = error else { - throw error - } - switch error.code ?? 0 { - case 404: throw PostServiceRemoteUpdatePostError.notFound - case 409: throw PostServiceRemoteUpdatePostError.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): - 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 - guard let post else { - return continuation.resume(throwing: URLError(.unknown)) // Should never happen - } - continuation.resume(returning: post) - } failure: { error in - continuation.resume(throwing: error ?? URLError(.unknown)) - } - } - } -} - -private func makeParameters(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 -} diff --git a/WordPressKit.xcodeproj/project.pbxproj b/WordPressKit.xcodeproj/project.pbxproj index 393460ce..7a663384 100644 --- a/WordPressKit.xcodeproj/project.pbxproj +++ b/WordPressKit.xcodeproj/project.pbxproj @@ -13,9 +13,6 @@ 0152100C28EDA9E400DD6783 /* StatsAnnualAndMostPopularTimeInsightDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0152100B28EDA9E400DD6783 /* StatsAnnualAndMostPopularTimeInsightDecodingTests.swift */; }; 0847B92C2A4442730044D32F /* IPLocationRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0847B92B2A4442730044D32F /* IPLocationRemote.swift */; }; 08C7493E2A45EA11000DA0E2 /* IPLocationRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08C7493D2A45EA11000DA0E2 /* IPLocationRemoteTests.swift */; }; - 0C1C08412B9CD79900E52F8C /* PostServiceRemoteExtended.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1C08402B9CD79900E52F8C /* PostServiceRemoteExtended.swift */; }; - 0C1C08432B9CD8D200E52F8C /* PostServiceRemoteREST+Extended.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1C08422B9CD8D200E52F8C /* PostServiceRemoteREST+Extended.swift */; }; - 0C1C08452B9CDB0B00E52F8C /* PostServiceRemoteXMLRPC+Extended.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1C08442B9CDB0B00E52F8C /* PostServiceRemoteXMLRPC+Extended.swift */; }; 0C9CD7992B9A107E0045BE03 /* RemotePostParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C9CD7982B9A107E0045BE03 /* RemotePostParameters.swift */; }; 0CB1905E2A2A5E83004D3E80 /* BlazeCampaign.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB1905D2A2A5E83004D3E80 /* BlazeCampaign.swift */; }; 0CB190612A2A6A13004D3E80 /* blaze-campaigns-search.json in Resources */ = {isa = PBXBuildFile; fileRef = 0CB1905F2A2A6943004D3E80 /* blaze-campaigns-search.json */; }; @@ -757,9 +754,6 @@ 0152100B28EDA9E400DD6783 /* StatsAnnualAndMostPopularTimeInsightDecodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsAnnualAndMostPopularTimeInsightDecodingTests.swift; sourceTree = ""; }; 0847B92B2A4442730044D32F /* IPLocationRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPLocationRemote.swift; sourceTree = ""; }; 08C7493D2A45EA11000DA0E2 /* IPLocationRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPLocationRemoteTests.swift; sourceTree = ""; }; - 0C1C08402B9CD79900E52F8C /* PostServiceRemoteExtended.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostServiceRemoteExtended.swift; sourceTree = ""; }; - 0C1C08422B9CD8D200E52F8C /* PostServiceRemoteREST+Extended.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PostServiceRemoteREST+Extended.swift"; sourceTree = ""; }; - 0C1C08442B9CDB0B00E52F8C /* PostServiceRemoteXMLRPC+Extended.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PostServiceRemoteXMLRPC+Extended.swift"; sourceTree = ""; }; 0C3A2A412A2E7BA500FD91D6 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = ""; }; 0C9CD7982B9A107E0045BE03 /* RemotePostParameters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemotePostParameters.swift; sourceTree = ""; }; 0CB1905D2A2A5E83004D3E80 /* BlazeCampaign.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlazeCampaign.swift; sourceTree = ""; }; @@ -1779,15 +1773,12 @@ E1BD95141FD5A2B800CD5CE3 /* PluginDirectoryServiceRemote.swift */, E13EE1461F33258E00C15787 /* PluginServiceRemote.swift */, 740B23B21F17EC7300067A2A /* PostServiceRemote.h */, - 0C1C08402B9CD79900E52F8C /* PostServiceRemoteExtended.swift */, 740B23BC1F17ECB500067A2A /* PostServiceRemoteOptions.h */, 740B23B31F17EC7300067A2A /* PostServiceRemoteREST.h */, 740B23B41F17EC7300067A2A /* PostServiceRemoteREST.m */, - 0C1C08422B9CD8D200E52F8C /* PostServiceRemoteREST+Extended.swift */, 9AF4F2FB218331DC00570E4B /* PostServiceRemoteREST+Revisions.swift */, 740B23B51F17EC7300067A2A /* PostServiceRemoteXMLRPC.h */, 740B23B61F17EC7300067A2A /* PostServiceRemoteXMLRPC.m */, - 0C1C08442B9CDB0B00E52F8C /* PostServiceRemoteXMLRPC+Extended.swift */, F181EA0127184D3C00F26141 /* ProductServiceRemote.swift */, 74A44DCA1F13C533006CD8F4 /* PushAuthenticationServiceRemote.swift */, C7A09A4F284104DB003096ED /* QR Login */, @@ -3342,18 +3333,15 @@ 436D56352118D85800CEAA33 /* WPCountry.swift in Sources */, 74A44DCB1F13C533006CD8F4 /* NotificationSettingsServiceRemote.swift in Sources */, FAD1344525908F5F00A8FEB1 /* JetpackBackupServiceRemote.swift in Sources */, - 0C1C08452B9CDB0B00E52F8C /* PostServiceRemoteXMLRPC+Extended.swift in Sources */, F1BB7806240FB90B0030ADDC /* AtomicAuthenticationServiceRemote.swift in Sources */, 404057CE221C38130060250C /* StatsTopVideosTimeIntervalData.swift in Sources */, 7E0D64FF22D855700092AD10 /* EditorServiceRemote.swift in Sources */, - 0C1C08412B9CD79900E52F8C /* PostServiceRemoteExtended.swift in Sources */, 9AF4F2FF2183346B00570E4B /* RemoteRevision.swift in Sources */, FE6C673A2BB739950083ECAB /* Decodable+Dictionary.swift in Sources */, 17D936252475D8AB008B2205 /* RemoteHomepageType.swift in Sources */, 74BA04F41F06DC0A00ED5CD8 /* CommentServiceRemoteREST.m in Sources */, 74C473AC1EF2F75E009918F2 /* SiteManagementServiceRemote.swift in Sources */, 74585B971F0D54B400E7E667 /* RemoteDomain.swift in Sources */, - 0C1C08432B9CD8D200E52F8C /* PostServiceRemoteREST+Extended.swift in Sources */, 3FFCC0492BAB98130051D229 /* DateFormatter+WordPressCom.swift in Sources */, 74A44DD01F13C64B006CD8F4 /* RemoteNotification.swift in Sources */, E1D6B558200E473A00325669 /* TimeZoneServiceRemote.swift in Sources */,