Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPM Prep – Use WordPressComRESTAPIInterfacing in DomainServiceRemote and AccountServiceRemoteREST #766

Merged
merged 4 commits into from
Apr 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension AccountServiceRemoteREST {
oAuthClientID: String,
oAuthClientSecret: String,
success: @escaping (() -> Void),
failure: @escaping ((NSError) -> Void)) {
failure: @escaping ((Error) -> Void)) {
let path = self.path(forEndpoint: "me/social-login/connect", withVersion: ._1_1)

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

wordPressComRestApi.POST(path, parameters: params, success: { (_, _) in
wordPressComRESTAPI.post(path, parameters: params, success: { (_, _) in
success()
}, failure: { (error, _) in
failure(error)
Expand Down Expand Up @@ -65,15 +65,15 @@ extension AccountServiceRemoteREST {
/// - oAuthClientSecret The WPCOM REST API client secret.
/// - success The block that will be executed on success.
/// - failure The block that will be executed on failure.
public func disconnectFromSocialService(_ service: SocialServiceName, oAuthClientID: String, oAuthClientSecret: String, success: @escaping(() -> Void), failure: @escaping((NSError) -> Void)) {
public func disconnectFromSocialService(_ service: SocialServiceName, oAuthClientID: String, oAuthClientSecret: String, success: @escaping(() -> Void), failure: @escaping((Error) -> Void)) {
let path = self.path(forEndpoint: "me/social-login/disconnect", withVersion: ._1_1)
let params = [
"client_id": oAuthClientID,
"client_secret": oAuthClientSecret,
"service": service.rawValue
] as [String: AnyObject]

wordPressComRestApi.POST(path, parameters: params, success: { (_, _) in
wordPressComRESTAPI.post(path, parameters: params, success: { (_, _) in
success()
}, failure: { (error, _) in
failure(error)
Expand Down
16 changes: 8 additions & 8 deletions Sources/WordPressKit/Services/AccountSettingsRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class AccountSettingsRemote: ServiceRemoteWordPressComREST {
let parameters = ["context": "edit"]
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)

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

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

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

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

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

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

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

wordPressComRestApi.POST(path, parameters: nil) { _, _ in
wordPressComRESTAPI.post(path, parameters: nil) { _, _ in
success()
} failure: { error, _ in
failure(error)
}
}

private func settingsFromResponse(_ responseObject: AnyObject) throws -> AccountSettings {
private func settingsFromResponse(_ responseObject: Any) throws -> AccountSettings {
guard let response = responseObject as? [String: AnyObject],
let firstName = response["first_name"] as? String,
let lastName = response["last_name"] as? String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
parameters["types"] = types.toDictionary() as AnyObject
}

wordPressComRestApi.POST(path,
wordPressComRESTAPI.post(path,
parameters: parameters,
success: { response, _ in
guard let restoreID = response["restore_id"] as? Int,
let jobID = response["job_id"] as? Int else {
guard let responseDict = response as? [String: Any],
let restoreID = responseDict["restore_id"] as? Int,
let jobID = responseDict["job_id"] as? Int else {
failure(ResponseError.decodingFailure)
return
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/WordPressKit/Services/Domains/DomainsServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/domains"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)

wordPressComRestApi.GET(path, parameters: nil,
wordPressComRESTAPI.get(path, parameters: nil,
success: {
response, _ in
do {
Expand All @@ -133,7 +133,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {

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

wordPressComRestApi.POST(path, parameters: parameters,
wordPressComRESTAPI.post(path, parameters: parameters,
success: { _, _ in

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

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

wordPressComRestApi.GET(
wordPressComRESTAPI.get(
servicePath,
parameters: nil,
success: { (response, _) in
Expand All @@ -201,7 +201,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {

let parameters: [String: AnyObject] = ["contact_information": contactInformation as AnyObject,
"domain_names": domainNames as AnyObject]
wordPressComRestApi.POST(
wordPressComRESTAPI.post(
servicePath,
parameters: parameters,
success: { response, _ in
Expand Down Expand Up @@ -239,7 +239,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
parameters["quantity"] = quantity as AnyObject
}

wordPressComRestApi.GET(servicePath,
wordPressComRESTAPI.get(servicePath,
parameters: parameters,
success: {
response, _ in
Expand All @@ -257,7 +257,7 @@ public class DomainsServiceRemote: ServiceRemoteWordPressComREST {
}
}

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

private func mapDomainsResponse(_ response: AnyObject) throws -> [RemoteDomain] {
private func mapDomainsResponse(_ response: Any) throws -> [RemoteDomain] {
guard let json = response as? [String: AnyObject],
let domainsJson = json["domains"] as? [[String: AnyObject]] else {
throw DomainsServiceRemote.ResponseError.decodingFailed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

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

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

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

Expand Down
22 changes: 18 additions & 4 deletions Sources/WordPressKit/WordPressAPI/WordPressComRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -615,20 +615,34 @@ extension WordPressComRestApi: WordPressComRESTAPIInterfacing {
// The same applies for the other methods below.
public func get(
_ URLString: String,
parameters: [String: NSObject]?,
parameters: [String: Any]?,
success: @escaping (Any, HTTPURLResponse?) -> Void,
failure: @escaping (any Error, HTTPURLResponse?) -> Void
) -> Progress? {
GET(URLString, parameters: parameters, success: success, failure: failure)
GET(
URLString,
// It's possible `WordPressComRestApi` could be updated to use `[String: Any]` instead.
// But leaving that investigation for later.
parameters: parameters as? [String: AnyObject],
success: success,
failure: failure
)
}

public func post(
_ URLString: String,
parameters: [String: NSObject]?,
parameters: [String: Any]?,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is a breaking change, because [String: NSObject] is a "sub-type" of [String: Any], thus can be passed in here?

success: @escaping (Any, HTTPURLResponse?) -> Void,
failure: @escaping (any Error, HTTPURLResponse?) -> Void
) -> Progress? {
POST(URLString, parameters: parameters, success: success, failure: failure)
POST(
URLString,
// It's possible `WordPressComRestApi` could be updated to use `[String: Any]` instead.
// But leaving that investigation for later.
parameters: parameters as? [String: AnyObject],
success: success,
failure: failure
)
}

public func multipartPOST(
Expand Down