Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions WordPressKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -808,6 +809,7 @@
3FFCC04A2BABA5220051D229 /* DateFormatter+WordPressComTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DateFormatter+WordPressComTests.swift"; sourceTree = "<group>"; };
3FFCC04C2BABA6980051D229 /* NSDate+WordPressComTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSDate+WordPressComTests.swift"; sourceTree = "<group>"; };
3FFCC04E2BABA6E60051D229 /* Date+WordPressCom.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+WordPressCom.swift"; sourceTree = "<group>"; };
3FFCC0552BABC78B0051D229 /* WordPressComRESTAPIInterfacing.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WordPressComRESTAPIInterfacing.h; sourceTree = "<group>"; };
40247DF92120D8E100AE1C3C /* AutomatedTransferService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutomatedTransferService.swift; sourceTree = "<group>"; };
40247DFB2120E69600AE1C3C /* AutomatedTransferStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutomatedTransferStatus.swift; sourceTree = "<group>"; };
404057C4221B30400060250C /* StatsSearchTermTimeIntervalData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsSearchTermTimeIntervalData.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2526,6 +2528,7 @@
93BD27731EE7388E002BB00B /* WordPressAPI */ = {
isa = PBXGroup;
children = (
3FFCC0552BABC78B0051D229 /* WordPressComRESTAPIInterfacing.h */,
4A05E7952B2FCB6400C25E3B /* NonceRetrieval.swift */,
4A05E7992B2FDC3200C25E3B /* WordPressOrgRestApi.swift */,
93BD27741EE73944002BB00B /* HTTPAuthenticationAlertController.swift */,
Expand Down Expand Up @@ -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 */,
Expand Down
16 changes: 8 additions & 8 deletions WordPressKit/AccountServiceRemoteREST.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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]]) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions WordPressKit/ServiceRemoteWordPressComREST.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <Foundation/Foundation.h>
#import <WordPressKit/WordPressComRESTAPIInterfacing.h>

@class WordPressComRestApi;

Expand All @@ -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<WordPressComRESTAPIInterfacing> wordPressComRESTAPI;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aim of this PR is to get feedback on the direction. As such, I haven't gone through the considerable effort of replacing all usages of the concrete WordPressComRestApi *wordPressComRestApi with the abstracted id<WordPressComRESTAPIInterfacing> wordPressComRESTAPI.

Assuming the direction I envisioned resonates, that'll be my next step.


/**
* @brief Designated initializer.
*
Expand Down
1 change: 1 addition & 0 deletions WordPressKit/ServiceRemoteWordPressComREST.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ - (instancetype)initWithWordPressComRestApi:(WordPressComRestApi *)wordPressComR
self = [super init];
if (self) {
_wordPressComRestApi = wordPressComRestApi;
_wordPressComRESTAPI = wordPressComRestApi;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once all the concrete WordPressComRestApi usages will have been replaced, this init will take an id<WordPressComRESTAPIInterfacing> input, effectively decoupling ServiceRemoteWordPressComREST (Objective-C) from WordPressComRestApi (Swift) via WordPressComRESTAPIInterfacing (Objective-C).

This is the model implemented in https://github.com/Automattic/spm-abstraction-layer-demo which should allow us to organize the code in a Swift package that Xcode can compile.

}
return self;
}
Expand Down
15 changes: 15 additions & 0 deletions WordPressKit/WordPressComRESTAPIInterfacing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@import Foundation;

@protocol WordPressComRESTAPIInterfacing

- (void)get:(NSString * _Nonnull)URLString
parameters:(NSDictionary<NSString *, NSObject *> * _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<NSString *, NSObject *> * _Nullable)parameters
success:(void (^ _Nonnull)(id _Nonnull, NSHTTPURLResponse * _Nullable))success
failure:(void (^ _Nonnull)(NSError * _Nonnull, NSHTTPURLResponse * _Nullable))failure;

@end
27 changes: 27 additions & 0 deletions WordPressKit/WordPressComRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,30 @@ extension WordPressAPIError<WordPressComRestApiEndpointError> {
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)
}
}
2 changes: 1 addition & 1 deletion WordPressKit/WordPressKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WordPressKit/PublicHeader.h>
#import <WordPressKit/WordPressComRESTAPIInterfacing.h>
#import <WordPressKit/ServiceRemoteWordPressComREST.h>
#import <WordPressKit/ServiceRemoteWordPressXMLRPC.h>
#import <WordPressKit/SiteServiceRemoteWordPressComREST.h>
Expand Down