diff --git a/WordPressKit.xcodeproj/project.pbxproj b/WordPressKit.xcodeproj/project.pbxproj index c5e75da7..d84fd093 100644 --- a/WordPressKit.xcodeproj/project.pbxproj +++ b/WordPressKit.xcodeproj/project.pbxproj @@ -77,6 +77,7 @@ 3FFCC04B2BABA5220051D229 /* DateFormatter+WordPressComTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FFCC04A2BABA5220051D229 /* DateFormatter+WordPressComTests.swift */; }; 3FFCC04D2BABA6980051D229 /* NSDate+WordPressComTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FFCC04C2BABA6980051D229 /* NSDate+WordPressComTests.swift */; }; 3FFCC04F2BABA6E60051D229 /* Date+WordPressCom.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FFCC04E2BABA6E60051D229 /* Date+WordPressCom.swift */; }; + 3FFCC0572BABC7E00051D229 /* WordPressComRESTAPIInterfacing.h in Headers */ = {isa = PBXBuildFile; fileRef = 3FFCC0552BABC78B0051D229 /* WordPressComRESTAPIInterfacing.h */; settings = {ATTRIBUTES = (Public, ); }; }; 40247DFA2120D8E100AE1C3C /* AutomatedTransferService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40247DF92120D8E100AE1C3C /* AutomatedTransferService.swift */; }; 40247DFC2120E69600AE1C3C /* AutomatedTransferStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40247DFB2120E69600AE1C3C /* AutomatedTransferStatus.swift */; }; 404057C5221B30400060250C /* StatsSearchTermTimeIntervalData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 404057C4221B30400060250C /* StatsSearchTermTimeIntervalData.swift */; }; @@ -808,6 +809,7 @@ 3FFCC04A2BABA5220051D229 /* DateFormatter+WordPressComTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DateFormatter+WordPressComTests.swift"; sourceTree = ""; }; 3FFCC04C2BABA6980051D229 /* NSDate+WordPressComTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSDate+WordPressComTests.swift"; sourceTree = ""; }; 3FFCC04E2BABA6E60051D229 /* Date+WordPressCom.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+WordPressCom.swift"; sourceTree = ""; }; + 3FFCC0552BABC78B0051D229 /* WordPressComRESTAPIInterfacing.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WordPressComRESTAPIInterfacing.h; sourceTree = ""; }; 40247DF92120D8E100AE1C3C /* AutomatedTransferService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutomatedTransferService.swift; sourceTree = ""; }; 40247DFB2120E69600AE1C3C /* AutomatedTransferStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutomatedTransferStatus.swift; sourceTree = ""; }; 404057C4221B30400060250C /* StatsSearchTermTimeIntervalData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsSearchTermTimeIntervalData.swift; sourceTree = ""; }; @@ -2526,6 +2528,7 @@ 93BD27731EE7388E002BB00B /* WordPressAPI */ = { isa = PBXGroup; children = ( + 3FFCC0552BABC78B0051D229 /* WordPressComRESTAPIInterfacing.h */, 4A05E7952B2FCB6400C25E3B /* NonceRetrieval.swift */, 4A05E7992B2FDC3200C25E3B /* WordPressOrgRestApi.swift */, 93BD27741EE73944002BB00B /* HTTPAuthenticationAlertController.swift */, @@ -2811,6 +2814,7 @@ 93BD276F1EE737A8002BB00B /* ServiceRemoteWordPressComREST.h in Headers */, 93BD273B1EE73282002BB00B /* AccountServiceRemote.h in Headers */, 93BD27691EE736A8002BB00B /* RemoteUser.h in Headers */, + 3FFCC0572BABC7E00051D229 /* WordPressComRESTAPIInterfacing.h in Headers */, 74B5F0DC1EF829B800B411E7 /* BlogServiceRemote.h in Headers */, 74B5F0D71EF8299B00B411E7 /* BlogServiceRemoteREST.h in Headers */, 74B5F0E61EF8699C00B411E7 /* RemotePostType.h in Headers */, diff --git a/WordPressKit/AccountServiceRemoteREST.m b/WordPressKit/AccountServiceRemoteREST.m index a4567c5f..cb76d930 100644 --- a/WordPressKit/AccountServiceRemoteREST.m +++ b/WordPressKit/AccountServiceRemoteREST.m @@ -56,7 +56,7 @@ - (void)getAccountDetailsWithSuccess:(void (^)(RemoteUser *remoteUser))success NSString *requestUrl = [self pathForEndpoint:@"me" withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1]; - [self.wordPressComRestApi GET:requestUrl + [self.wordPressComRESTAPI get:requestUrl parameters:nil success:^(id responseObject, NSHTTPURLResponse *httpResponse) { if (!success) { @@ -108,7 +108,7 @@ - (void)updateBlogsVisibility:(NSDictionary *)blogs }; NSString *path = [self pathForEndpoint:@"me/sites" withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1]; - [self.wordPressComRestApi POST:path + [self.wordPressComRESTAPI post:path parameters:parameters success:^(id responseObject, NSHTTPURLResponse *httpResponse) { if (success) { @@ -127,7 +127,7 @@ - (void)isPasswordlessAccount:(NSString *)identifier success:(void (^)(BOOL pass NSString *path = [self pathForEndpoint:[NSString stringWithFormat:@"users/%@/auth-options", encodedIdentifier] withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1]; - [self.wordPressComRestApi GET:path + [self.wordPressComRESTAPI get:path parameters:nil success:^(id responseObject, NSHTTPURLResponse *httpResponse) { if (!success) { @@ -149,7 +149,7 @@ - (void)isEmailAvailable:(NSString *)email success:(void (^)(BOOL available))suc static NSString * const errorEmailAddressInvalid = @"invalid"; static NSString * const errorEmailAddressTaken = @"taken"; - [self.wordPressComRestApi GET:@"is-available/email" + [self.wordPressComRESTAPI get:@"is-available/email" parameters:@{ @"q": email, @"format": @"json"} success:^(id responseObject, NSHTTPURLResponse *httpResponse) { if ([responseObject isKindOfClass:[NSDictionary class]]) { @@ -211,7 +211,7 @@ - (void)isUsernameAvailable:(NSString *)username success:(void (^)(BOOL available))success failure:(void (^)(NSError *error))failure { - [self.wordPressComRestApi GET:@"is-available/username" + [self.wordPressComRESTAPI get:@"is-available/username" parameters:@{ @"q": username, @"format": @"json"} success:^(id responseObject, NSHTTPURLResponse *httpResponse) { if (!success) { @@ -317,7 +317,7 @@ - (void)requestWPComMagicLinkForEmail:(NSString *)email [params addEntriesFromDictionary:extraParams]; } - [self.wordPressComRestApi POST:path + [self.wordPressComRESTAPI post:path parameters:[NSDictionary dictionaryWithDictionary:params] success:^(id responseObject, NSHTTPURLResponse *httpResponse) { if (success) { @@ -336,7 +336,7 @@ - (void)requestVerificationEmailWithSucccess:(void (^)(void))success NSString *path = [self pathForEndpoint:@"me/send-verification-email" withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1]; - [self.wordPressComRestApi POST:path parameters:nil success:^(id _Nonnull responseObject, NSHTTPURLResponse * _Nullable httpResponse) { + [self.wordPressComRESTAPI post:path parameters:nil success:^(id _Nonnull responseObject, NSHTTPURLResponse * _Nullable httpResponse) { if (success) { success(); } @@ -355,7 +355,7 @@ - (void)getBlogsWithParameters:(NSDictionary *)parameters { NSString *requestUrl = [self pathForEndpoint:@"me/sites" withVersion:ServiceRemoteWordPressComRESTApiVersion_1_2]; - [self.wordPressComRestApi GET:requestUrl + [self.wordPressComRESTAPI get:requestUrl parameters:parameters success:^(id responseObject, NSHTTPURLResponse *httpResponse) { if (success) { diff --git a/WordPressKit/ServiceRemoteWordPressComREST.h b/WordPressKit/ServiceRemoteWordPressComREST.h index 7d7499ff..b214d4d1 100644 --- a/WordPressKit/ServiceRemoteWordPressComREST.h +++ b/WordPressKit/ServiceRemoteWordPressComREST.h @@ -1,4 +1,5 @@ #import +#import @class WordPressComRestApi; @@ -23,6 +24,12 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, strong, readonly) WordPressComRestApi *wordPressComRestApi; +/** + * @brief The interface to the WordPress.com API to use for performing REST requests. + * This is meant to gradually replace `wordPressComRestApi`. + */ +@property (nonatomic, strong, readonly) id wordPressComRESTAPI; + /** * @brief Designated initializer. * diff --git a/WordPressKit/ServiceRemoteWordPressComREST.m b/WordPressKit/ServiceRemoteWordPressComREST.m index 94c052e0..73c3e835 100644 --- a/WordPressKit/ServiceRemoteWordPressComREST.m +++ b/WordPressKit/ServiceRemoteWordPressComREST.m @@ -26,6 +26,7 @@ - (instancetype)initWithWordPressComRestApi:(WordPressComRestApi *)wordPressComR self = [super init]; if (self) { _wordPressComRestApi = wordPressComRestApi; + _wordPressComRESTAPI = wordPressComRestApi; } return self; } diff --git a/WordPressKit/WordPressComRESTAPIInterfacing.h b/WordPressKit/WordPressComRESTAPIInterfacing.h new file mode 100644 index 00000000..8a68cb04 --- /dev/null +++ b/WordPressKit/WordPressComRESTAPIInterfacing.h @@ -0,0 +1,15 @@ +@import Foundation; + +@protocol WordPressComRESTAPIInterfacing + +- (void)get:(NSString * _Nonnull)URLString + parameters:(NSDictionary * _Nullable)parameters + success:(void (^ _Nonnull)(id _Nonnull, NSHTTPURLResponse * _Nullable))success + failure:(void (^ _Nonnull)(NSError * _Nonnull, NSHTTPURLResponse * _Nullable))failure; + +- (void)post:(NSString * _Nonnull)URLString + parameters:(NSDictionary * _Nullable)parameters + success:(void (^ _Nonnull)(id _Nonnull, NSHTTPURLResponse * _Nullable))success + failure:(void (^ _Nonnull)(NSError * _Nonnull, NSHTTPURLResponse * _Nullable))failure; + +@end diff --git a/WordPressKit/WordPressComRestApi.swift b/WordPressKit/WordPressComRestApi.swift index 248341ac..0d4f6ae9 100644 --- a/WordPressKit/WordPressComRestApi.swift +++ b/WordPressKit/WordPressComRestApi.swift @@ -620,3 +620,30 @@ extension WordPressAPIError { return self as NSError } } + +extension WordPressComRestApi: WordPressComRESTAPIInterfacing { + + // A note on the naming: Even if defined as `GET` in Objective-C, then method gets converted to Swift as `get`. + // + // Also, there is no Objective-C direct equivalent of `AnyObject`, which here is used in `parameters: [String: AnyObject]?`. + // + // For those reasons, we can't immediately conform to `WordPressComRESTAPIInterfacing` and need instead to use this kind of wrapping. + // The same applies for the other methods below. + public func get( + _ URLString: String, + parameters: [String: NSObject]?, + success: @escaping (Any, HTTPURLResponse?) -> Void, + failure: @escaping (any Error, HTTPURLResponse?) -> Void + ) { + GET(URLString, parameters: parameters, success: success, failure: failure) + } + + public func post( + _ URLString: String, + parameters: [String: NSObject]?, + success: @escaping (Any, HTTPURLResponse?) -> Void, + failure: @escaping (any Error, HTTPURLResponse?) -> Void + ) { + POST(URLString, parameters: parameters, success: success, failure: failure) + } +} diff --git a/WordPressKit/WordPressKit.h b/WordPressKit/WordPressKit.h index f498488e..657e44fc 100644 --- a/WordPressKit/WordPressKit.h +++ b/WordPressKit/WordPressKit.h @@ -6,7 +6,7 @@ FOUNDATION_EXPORT double WordPressKitVersionNumber; //! Project version string for WordPressKit. FOUNDATION_EXPORT const unsigned char WordPressKitVersionString[]; -// In this header, you should import all the public headers of your framework using statements like #import +#import #import #import #import