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

Commit 009a5a4

Browse files
authored
Use WordPressComRESTAPIInterfacing in two Swift services (#766)
The services are `DomainServiceRemote` and `AccountServiceRemoteREST`
2 parents 7032612 + 52a7ea4 commit 009a5a4

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

Sources/WordPressKit/Services/AccountServiceRemoteREST+SocialService.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension AccountServiceRemoteREST {
2323
oAuthClientID: String,
2424
oAuthClientSecret: String,
2525
success: @escaping (() -> Void),
26-
failure: @escaping ((NSError) -> Void)) {
26+
failure: @escaping ((Error) -> Void)) {
2727
let path = self.path(forEndpoint: "me/social-login/connect", withVersion: ._1_1)
2828

2929
var params = [
@@ -37,7 +37,7 @@ extension AccountServiceRemoteREST {
3737
params.merge(connectParameters, uniquingKeysWith: { (current, _) in current })
3838
}
3939

40-
wordPressComRestApi.POST(path, parameters: params, success: { (_, _) in
40+
wordPressComRESTAPI.post(path, parameters: params, success: { (_, _) in
4141
success()
4242
}, failure: { (error, _) in
4343
failure(error)
@@ -65,15 +65,15 @@ extension AccountServiceRemoteREST {
6565
/// - oAuthClientSecret The WPCOM REST API client secret.
6666
/// - success The block that will be executed on success.
6767
/// - failure The block that will be executed on failure.
68-
public func disconnectFromSocialService(_ service: SocialServiceName, oAuthClientID: String, oAuthClientSecret: String, success: @escaping(() -> Void), failure: @escaping((NSError) -> Void)) {
68+
public func disconnectFromSocialService(_ service: SocialServiceName, oAuthClientID: String, oAuthClientSecret: String, success: @escaping(() -> Void), failure: @escaping((Error) -> Void)) {
6969
let path = self.path(forEndpoint: "me/social-login/disconnect", withVersion: ._1_1)
7070
let params = [
7171
"client_id": oAuthClientID,
7272
"client_secret": oAuthClientSecret,
7373
"service": service.rawValue
7474
] as [String: AnyObject]
7575

76-
wordPressComRestApi.POST(path, parameters: params, success: { (_, _) in
76+
wordPressComRESTAPI.post(path, parameters: params, success: { (_, _) in
7777
success()
7878
}, failure: { (error, _) in
7979
failure(error)

Sources/WordPressKit/Services/AccountSettingsRemote.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
3232
let parameters = ["context": "edit"]
3333
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
3434

35-
wordPressComRestApi.GET(path,
35+
wordPressComRESTAPI.get(path,
3636
parameters: parameters as [String: AnyObject]?,
3737
success: {
3838
responseObject, _ in
@@ -54,7 +54,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
5454
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
5555
let parameters = [fieldNameForChange(change): change.stringValue]
5656

57-
wordPressComRestApi.POST(path,
57+
wordPressComRESTAPI.post(path,
5858
parameters: parameters as [String: AnyObject]?,
5959
success: {
6060
_, _ in
@@ -79,7 +79,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
7979

8080
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
8181

82-
wordPressComRestApi.POST(path,
82+
wordPressComRESTAPI.post(path,
8383
parameters: parameters as [String: AnyObject]?,
8484
success: { _, _ in
8585
success()
@@ -99,7 +99,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
9999
let endpoint = "me/username/validate/\(username)"
100100
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
101101

102-
wordPressComRestApi.GET(path,
102+
wordPressComRESTAPI.get(path,
103103
parameters: nil,
104104
success: { _, _ in
105105
// The success block needs to be changed if
@@ -116,7 +116,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
116116
let endpoint = "wpcom/v2/users/username/suggestions"
117117
let parameters = ["name": base]
118118

119-
wordPressComRestApi.GET(endpoint, parameters: parameters as [String: AnyObject]?, success: { (responseObject, _) in
119+
wordPressComRESTAPI.get(endpoint, parameters: parameters as [String: AnyObject]?, success: { (responseObject, _) in
120120
guard let response = responseObject as? [String: AnyObject],
121121
let suggestions = response["suggestions"] as? [String] else {
122122
finished([])
@@ -134,7 +134,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
134134
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
135135
let parameters = ["password": password]
136136

137-
wordPressComRestApi.POST(path,
137+
wordPressComRESTAPI.post(path,
138138
parameters: parameters as [String: AnyObject]?,
139139
success: {
140140
_, _ in
@@ -149,14 +149,14 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
149149
let endpoint = "me/account/close"
150150
let path = path(forEndpoint: endpoint, withVersion: ._1_1)
151151

152-
wordPressComRestApi.POST(path, parameters: nil) { _, _ in
152+
wordPressComRESTAPI.post(path, parameters: nil) { _, _ in
153153
success()
154154
} failure: { error, _ in
155155
failure(error)
156156
}
157157
}
158158

159-
private func settingsFromResponse(_ responseObject: AnyObject) throws -> AccountSettings {
159+
private func settingsFromResponse(_ responseObject: Any) throws -> AccountSettings {
160160
guard let response = responseObject as? [String: AnyObject],
161161
let firstName = response["first_name"] as? String,
162162
let lastName = response["last_name"] as? String,

Sources/WordPressKit/Services/ActivityServiceRemote_ApiVersion1_0.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
parameters["types"] = types.toDictionary() as AnyObject
2929
}
3030

31-
wordPressComRestApi.POST(path,
31+
wordPressComRESTAPI.post(path,
3232
parameters: parameters,
3333
success: { response, _ in
34-
guard let restoreID = response["restore_id"] as? Int,
35-
let jobID = response["job_id"] as? Int else {
34+
guard let responseDict = response as? [String: Any],
35+
let restoreID = responseDict["restore_id"] as? Int,
36+
let jobID = responseDict["job_id"] as? Int else {
3637
failure(ResponseError.decodingFailure)
3738
return
3839
}

Sources/WordPressKit/Services/Domains/DomainsServiceRemote.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
112112
let endpoint = "sites/\(siteID)/domains"
113113
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
114114

115-
wordPressComRestApi.GET(path, parameters: nil,
115+
wordPressComRESTAPI.get(path, parameters: nil,
116116
success: {
117117
response, _ in
118118
do {
@@ -133,7 +133,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
133133

134134
let parameters: [String: AnyObject] = ["domain": domain as AnyObject]
135135

136-
wordPressComRestApi.POST(path, parameters: parameters,
136+
wordPressComRESTAPI.post(path, parameters: parameters,
137137
success: { _, _ in
138138

139139
success()
@@ -149,7 +149,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
149149
let endPoint = "domains/supported-states/\(countryCode)"
150150
let servicePath = path(forEndpoint: endPoint, withVersion: ._1_1)
151151

152-
wordPressComRestApi.GET(
152+
wordPressComRESTAPI.get(
153153
servicePath,
154154
parameters: nil,
155155
success: {
@@ -175,7 +175,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
175175
let endPoint = "me/domain-contact-information"
176176
let servicePath = path(forEndpoint: endPoint, withVersion: ._1_1)
177177

178-
wordPressComRestApi.GET(
178+
wordPressComRESTAPI.get(
179179
servicePath,
180180
parameters: nil,
181181
success: { (response, _) in
@@ -201,7 +201,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
201201

202202
let parameters: [String: AnyObject] = ["contact_information": contactInformation as AnyObject,
203203
"domain_names": domainNames as AnyObject]
204-
wordPressComRestApi.POST(
204+
wordPressComRESTAPI.post(
205205
servicePath,
206206
parameters: parameters,
207207
success: { response, _ in
@@ -239,7 +239,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
239239
parameters["quantity"] = quantity as AnyObject
240240
}
241241

242-
wordPressComRestApi.GET(servicePath,
242+
wordPressComRESTAPI.get(servicePath,
243243
parameters: parameters,
244244
success: {
245245
response, _ in
@@ -257,7 +257,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
257257
}
258258
}
259259

260-
private func map(suggestions response: AnyObject) throws -> [DomainSuggestion] {
260+
private func map(suggestions response: Any) throws -> [DomainSuggestion] {
261261
guard let jsonSuggestions = response as? [[String: AnyObject]] else {
262262
throw DomainsServiceRemote.ResponseError.decodingFailed
263263
}
@@ -272,7 +272,7 @@ private func map(suggestions response: AnyObject) throws -> [DomainSuggestion] {
272272
return suggestions
273273
}
274274

275-
private func mapDomainsResponse(_ response: AnyObject) throws -> [RemoteDomain] {
275+
private func mapDomainsResponse(_ response: Any) throws -> [RemoteDomain] {
276276
guard let json = response as? [String: AnyObject],
277277
let domainsJson = json["domains"] as? [[String: AnyObject]] else {
278278
throw DomainsServiceRemote.ResponseError.decodingFailed

Sources/WordPressKit/WordPressAPI/WordPressComRESTAPIInterfacing.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
@property (strong, nonatomic, readonly) NSURL * _Nonnull baseURL;
88

9+
/// - Note: `parameters` has `id` instead of the more common `NSObject *` as its value type so it will convert to `AnyObject` in Swift.
10+
/// In Swift, it's simpler to work with `AnyObject` than with `NSObject`. For example `"abc" as AnyObject` over `"abc" as NSObject`.
911
- (NSProgress * _Nullable)get:(NSString * _Nonnull)URLString
10-
parameters:(NSDictionary<NSString *, NSObject *> * _Nullable)parameters
12+
parameters:(NSDictionary<NSString *, id> * _Nullable)parameters
1113
success:(void (^ _Nonnull)(id _Nonnull, NSHTTPURLResponse * _Nullable))success
1214
failure:(void (^ _Nonnull)(NSError * _Nonnull, NSHTTPURLResponse * _Nullable))failure;
1315

16+
/// - Note: `parameters` has `id` instead of the more common `NSObject *` as its value type so it will convert to `AnyObject` in Swift.
17+
/// In Swift, it's simpler to work with `AnyObject` than with `NSObject`. For example `"abc" as AnyObject` over `"abc" as NSObject`.
1418
- (NSProgress * _Nullable)post:(NSString * _Nonnull)URLString
15-
parameters:(NSDictionary<NSString *, NSObject *> * _Nullable)parameters
19+
parameters:(NSDictionary<NSString *, id> * _Nullable)parameters
1620
success:(void (^ _Nonnull)(id _Nonnull, NSHTTPURLResponse * _Nullable))success
1721
failure:(void (^ _Nonnull)(NSError * _Nonnull, NSHTTPURLResponse * _Nullable))failure;
1822

Sources/WordPressKit/WordPressAPI/WordPressComRestApi.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,20 +615,34 @@ extension WordPressComRestApi: WordPressComRESTAPIInterfacing {
615615
// The same applies for the other methods below.
616616
public func get(
617617
_ URLString: String,
618-
parameters: [String: NSObject]?,
618+
parameters: [String: Any]?,
619619
success: @escaping (Any, HTTPURLResponse?) -> Void,
620620
failure: @escaping (any Error, HTTPURLResponse?) -> Void
621621
) -> Progress? {
622-
GET(URLString, parameters: parameters, success: success, failure: failure)
622+
GET(
623+
URLString,
624+
// It's possible `WordPressComRestApi` could be updated to use `[String: Any]` instead.
625+
// But leaving that investigation for later.
626+
parameters: parameters as? [String: AnyObject],
627+
success: success,
628+
failure: failure
629+
)
623630
}
624631

625632
public func post(
626633
_ URLString: String,
627-
parameters: [String: NSObject]?,
634+
parameters: [String: Any]?,
628635
success: @escaping (Any, HTTPURLResponse?) -> Void,
629636
failure: @escaping (any Error, HTTPURLResponse?) -> Void
630637
) -> Progress? {
631-
POST(URLString, parameters: parameters, success: success, failure: failure)
638+
POST(
639+
URLString,
640+
// It's possible `WordPressComRestApi` could be updated to use `[String: Any]` instead.
641+
// But leaving that investigation for later.
642+
parameters: parameters as? [String: AnyObject],
643+
success: success,
644+
failure: failure
645+
)
632646
}
633647

634648
public func multipartPOST(

0 commit comments

Comments
 (0)