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

Commit 6a8e615

Browse files
committed
Convert the Objective-C protocol to a Swift concrete type
1 parent abaf0ec commit 6a8e615

19 files changed

+106
-74
lines changed

Sources/WordPressKit/Exports.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@_exported import WordPressKitModels
2+
@_exported import WordPressKitObjC
3+
@_exported import WordPressKitObjCUtils
4+
5+
extension ServiceRemoteWordPressComREST {
6+
public var wordPressComRestApi: WordPressComRestApi {
7+
self.wordPressComRESTAPI as! WordPressComRestApi
8+
}
9+
}
10+
11+
extension ServiceRemoteWordPressXMLRPC {
12+
public var xmlrpcApi: WordPressOrgXMLRPCApi {
13+
self.api as! WordPressOrgXMLRPCApi
14+
}
15+
}

Sources/WordPressKit/NotificationSettingsServiceRemote.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ import WordPressKitObjC
55
/// Here we'll deal mostly with the Settings / Push Notifications API.
66
///
77
open class NotificationSettingsServiceRemote: ServiceRemoteWordPressComREST {
8-
/// Designated Initializer. Fails if the remoteApi is nil.
9-
///
10-
/// - Parameter wordPressComRestApi: A Reference to the WordPressComRestApi that should be used to interact with WordPress.com
11-
///
12-
public override init(wordPressComRestApi: WordPressComRestApi) {
13-
super.init(wordPressComRestApi: wordPressComRestApi)
14-
}
15-
168
/// Retrieves all of the Notification Settings
179
///
1810
/// - Parameters:

Sources/WordPressKit/PostServiceRemoteREST.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension PostServiceRemoteREST {
3838
parameters["exclude"] = excludeUserIDs
3939
}
4040

41-
wordPressComRestAPI.get(requestUrl,
41+
wordPressComRESTAPI.get(requestUrl,
4242
parameters: parameters,
4343
success: { (responseObject, httpResponse) in
4444
if let success {

Sources/WordPressKit/PostServiceRemoteXMLRPC+Extended.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import WordPressKitObjC
55
extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
66
public func post(withID postID: Int) async throws -> RemotePost {
77
let parameters = xmlrpcArguments(withExtra: postID) as [AnyObject]
8-
let result = await api.call(method: "wp.getPost", parameters: parameters)
8+
let result = await xmlrpcApi.call(method: "wp.getPost", parameters: parameters)
99
switch result {
1010
case .success(let response):
1111
return try await decodePost(from: response.body)
@@ -20,7 +20,7 @@ extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
2020
public func createPost(with parameters: RemotePostCreateParameters) async throws -> RemotePost {
2121
let dictionary = try makeParameters(from: RemotePostCreateParametersXMLRPCEncoder(parameters: parameters))
2222
let parameters = xmlrpcArguments(withExtra: dictionary) as [AnyObject]
23-
let response = try await api.call(method: "wp.newPost", parameters: parameters).get()
23+
let response = try await xmlrpcApi.call(method: "wp.newPost", parameters: parameters).get()
2424
guard let postID = (response.body as? NSObject)?.wpkit_numericValue() else {
2525
throw URLError(.unknown) // Should never happen
2626
}
@@ -30,7 +30,7 @@ extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
3030
public func patchPost(withID postID: Int, parameters: RemotePostUpdateParameters) async throws -> RemotePost {
3131
let dictionary = try makeParameters(from: RemotePostUpdateParametersXMLRPCEncoder(parameters: parameters))
3232
let parameters = xmlrpcArguments(withExtraDefaults: [postID as NSNumber], andExtra: dictionary) as [AnyObject]
33-
let result = await api.call(method: "wp.editPost", parameters: parameters)
33+
let result = await xmlrpcApi.call(method: "wp.editPost", parameters: parameters)
3434
switch result {
3535
case .success:
3636
return try await post(withID: postID)
@@ -48,7 +48,7 @@ extension PostServiceRemoteXMLRPC: PostServiceRemoteExtended {
4848

4949
public func deletePost(withID postID: Int) async throws {
5050
let parameters = xmlrpcArguments(withExtra: postID) as [AnyObject]
51-
let result = await api.call(method: "wp.deletePost", parameters: parameters)
51+
let result = await xmlrpcApi.call(method: "wp.deletePost", parameters: parameters)
5252
switch result {
5353
case .success:
5454
return

Sources/WordPressKit/ReaderTopicServiceRemote+Interests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ extension ReaderTopicServiceRemote {
5050
public func pathForTopic(slug: String) -> String {
5151
let endpoint = path(forEndpoint: "read/tags/\(slug)/posts", withVersion: ._1_2)
5252

53-
return wordPressComRESTAPI.baseURL.appendingPathComponent(endpoint).absoluteString
53+
return wordPressComRestApi.baseURL.appendingPathComponent(endpoint).absoluteString
5454
}
5555
}

Sources/WordPressKit/WordPressComRestApi.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,4 +682,27 @@ extension WordPressComRestApi: WordPressComRESTAPIInterfacing {
682682
failure: failure as FailureReponseBlock
683683
)
684684
}
685+
686+
@objc public func unknownResponseError() -> any Error {
687+
NSError(
688+
domain: WordPressComRestApiEndpointError.errorDomain,
689+
code: WordPressComRestApiErrorCode.unknown.rawValue,
690+
userInfo: [
691+
WordPressComRestApi.ErrorKeyErrorMessage: NSLocalizedString("Unknown error", comment: "Unknown error"),
692+
NSLocalizedDescriptionKey: NSLocalizedString("Unknown error", comment: "Unknown error")
693+
]
694+
)
695+
}
696+
697+
@objc public func uploadFailedErrorCode() -> Int {
698+
WordPressComRestApiErrorCode.uploadFailed.rawValue
699+
}
700+
701+
@objc public func errorCodeKey() -> String {
702+
Self.ErrorKeyErrorCode
703+
}
704+
705+
@objc public func errorMessageKey() -> String {
706+
Self.ErrorKeyErrorMessage
707+
}
685708
}

Sources/WordPressKit/WordPressOrgXMLRPCApi.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Foundation
22
import wpxmlrpc
33

44
/// Class to connect to the XMLRPC API on self hosted sites.
5-
open class WordPressOrgXMLRPCApi: NSObject {
6-
public typealias SuccessResponseBlock = (AnyObject, HTTPURLResponse?) -> Void
7-
public typealias FailureReponseBlock = (_ error: NSError, _ httpResponse: HTTPURLResponse?) -> Void
5+
open class WordPressOrgXMLRPCApi: NSObject, WordPressOrgXMLRPCApiInterfacing {
6+
public typealias SuccessResponseBlock = (Any, HTTPURLResponse?) -> Void
7+
public typealias FailureReponseBlock = (_ error: any Error, _ httpResponse: HTTPURLResponse?) -> Void
88

99
@available(*, deprecated, message: "This property is no longer being used because WordPressKit now sends all HTTP requests using `URLSession` directly.")
1010
public static var useURLSession = true
@@ -126,10 +126,12 @@ open class WordPressOrgXMLRPCApi: NSObject {
126126
returns nil it's because something happened on the request serialization and the network request was not started, but the failure callback
127127
will be invoked with the error specificing the serialization issues.
128128
*/
129-
@objc @discardableResult open func callMethod(_ method: String,
130-
parameters: [AnyObject]?,
131-
success: @escaping SuccessResponseBlock,
132-
failure: @escaping FailureReponseBlock) -> Progress? {
129+
@objc @discardableResult open func callMethod(
130+
_ method: String,
131+
parameters: [Any]?,
132+
success: @escaping (Any, HTTPURLResponse?) -> Void,
133+
failure: @escaping (any Error, HTTPURLResponse?) -> Void
134+
) -> Progress {
133135
let progress = Progress.discreteProgress(totalUnitCount: 100)
134136
Task { @MainActor in
135137
let result = await self.call(method: method, parameters: parameters, fulfilling: progress, streaming: false)
@@ -156,10 +158,11 @@ open class WordPressOrgXMLRPCApi: NSObject {
156158
returns nil it's because something happened on the request serialization and the network request was not started, but the failure callback
157159
will be invoked with the error specificing the serialization issues.
158160
*/
159-
@objc @discardableResult open func streamCallMethod(_ method: String,
160-
parameters: [AnyObject]?,
161-
success: @escaping SuccessResponseBlock,
162-
failure: @escaping FailureReponseBlock) -> Progress? {
161+
@objc @discardableResult open func streamCallMethod(
162+
_ method: String, parameters: [Any]?,
163+
success: @escaping (Any, HTTPURLResponse?) -> Void,
164+
failure: @escaping (any Error, HTTPURLResponse?) -> Void
165+
) -> Progress {
163166
let progress = Progress.discreteProgress(totalUnitCount: 100)
164167
Task { @MainActor in
165168
let result = await self.call(method: method, parameters: parameters, fulfilling: progress, streaming: true)
@@ -184,7 +187,7 @@ open class WordPressOrgXMLRPCApi: NSObject {
184187
/// - Parameters:
185188
/// - streaming: set to `true` if there are large data (i.e. uploading files) in given `parameters`. `false` by default.
186189
/// - Returns: A `Result` type that contains the XMLRPC success or failure result.
187-
func call(method: String, parameters: [AnyObject]?, fulfilling progress: Progress? = nil, streaming: Bool = false) async -> WordPressAPIResult<HTTPAPIResponse<AnyObject>, WordPressOrgXMLRPCApiFault> {
190+
func call(method: String, parameters: [Any]?, fulfilling progress: Progress? = nil, streaming: Bool = false) async -> WordPressAPIResult<HTTPAPIResponse<AnyObject>, WordPressOrgXMLRPCApiFault> {
188191
let session = streaming ? uploadURLSession : urlSession
189192
let builder = HTTPRequestBuilder(url: endpoint)
190193
.method(.post)

Sources/WordPressKit/WordPressOrgXMLRPCValidator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ open class WordPressOrgXMLRPCValidator: NSObject {
230230
}, failure: { (error, httpResponse) in
231231
if httpResponse?.url != url {
232232
// we where redirected, let's check the answer content
233-
if let data = error.userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String] as? Data,
233+
if let data = (error as NSError).userInfo[WordPressOrgXMLRPCApi.WordPressOrgXMLRPCApiErrorKeyData as String] as? Data,
234234
let responseString = String(data: data, encoding: String.Encoding.utf8), responseString.range(of: "<meta name=\"GENERATOR\" content=\"www.dudamobile.com\">") != nil
235235
|| responseString.range(of: "dm404Container") != nil {
236236
failure(WordPressOrgXMLRPCValidatorError.mobilePluginRedirectedError as NSError)
@@ -255,7 +255,7 @@ open class WordPressOrgXMLRPCValidator: NSObject {
255255
case .some(WordPressOrgXMLRPCValidatorError.blocked.rawValue):
256256
failure(WordPressOrgXMLRPCValidatorError.blocked as NSError)
257257
default:
258-
failure(error)
258+
failure(error as NSError)
259259
}
260260
})
261261
}

Sources/WordPressKitObjC/MediaServiceRemoteREST.m

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ - (NSError *)processMediaUploadErrors:(NSArray *)errorList {
249249
}
250250
NSDictionary *errorDictionary = @{NSLocalizedDescriptionKey: errorMessage};
251251
error = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain
252-
code:WordPressComRestApiErrorCodeUploadFailed
252+
code:self.wordPressComRESTAPI.uploadFailedErrorCode
253253
userInfo:errorDictionary];
254254
}
255255
return error;
@@ -302,10 +302,7 @@ - (void)deleteMedia:(RemoteMedia *)media
302302
}
303303
} else {
304304
if (failure) {
305-
NSError *error = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain
306-
code:WordPressComRestApiErrorCodeUnknown
307-
userInfo:nil];
308-
failure(error);
305+
failure(self.wordPressComRESTAPI.unknownResponseError);
309306
}
310307
}
311308
} failure:^(NSError *error, NSHTTPURLResponse *response) {
@@ -375,10 +372,7 @@ -(void)getVideoPressToken:(NSString *)videoPressID
375372
}
376373
} else {
377374
if (failure) {
378-
NSError *error = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain
379-
code:WordPressComRestApiErrorCodeUnknown
380-
userInfo:nil];
381-
failure(error);
375+
failure(self.wordPressComRESTAPI.unknownResponseError);
382376
}
383377
}
384378
} failure:^(NSError *error, NSHTTPURLResponse *response) {

Sources/WordPressKitObjC/PostServiceRemoteXMLRPC.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#import "WPMapFilterReduce.h"
77
#import "DisplayableImageHelper.h"
88

9+
@import WordPressKitModels;
910
@import NSObject_SafeExpectations;
1011

1112
const NSInteger HTTP404ErrorCode = 404;

0 commit comments

Comments
 (0)