Skip to content

Commit

Permalink
Use WordPressComRESTAPIInterfacing for all Swift sync requests (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio authored Apr 4, 2024
2 parents 0d50348 + cba707d commit 9930ab2
Show file tree
Hide file tree
Showing 42 changed files with 209 additions and 204 deletions.
2 changes: 1 addition & 1 deletion Sources/WordPressKit/Models/EditorSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct EditorSettings {
}

extension EditorSettings {
init(with response: AnyObject) throws {
init(with response: Any) throws {
guard let response = response as? [String: AnyObject] else {
throw NSError(domain: NSURLErrorDomain, code: NSURLErrorBadServerResponse, userInfo: nil)
}
Expand Down
23 changes: 14 additions & 9 deletions Sources/WordPressKit/Services/ActivityServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ open class ActivityServiceRemote: ServiceRemoteWordPressComREST {

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

wordPressComRestApi.GET(finalPath,
wordPressComRESTAPI.get(finalPath,
parameters: nil,
success: { response, _ in
do {
Expand Down Expand Up @@ -105,7 +105,7 @@ open class ActivityServiceRemote: ServiceRemoteWordPressComREST {
parameters["on"] = formatter.string(from: on) as AnyObject
}

wordPressComRestApi.GET(path,
wordPressComRESTAPI.get(path,
parameters: parameters,
success: { response, _ in
do {
Expand Down Expand Up @@ -135,7 +135,7 @@ open class ActivityServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/rewind"
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)

wordPressComRestApi.GET(path,
wordPressComRESTAPI.get(path,
parameters: nil,
success: { response, _ in
guard let rewindStatus = response as? [String: AnyObject] else {
Expand All @@ -154,20 +154,25 @@ open class ActivityServiceRemote: ServiceRemoteWordPressComREST {
}, failure: { error, _ in
// FIXME: A hack to support free WPCom sites and Rewind. Should be obsolote as soon as the backend
// stops returning 412's for those sites.
if error.domain == WordPressComRestApiEndpointError.errorDomain, error.code == WordPressComRestApiErrorCode.preconditionFailure.rawValue {
let status = RewindStatus(state: .unavailable)
success(status)
let nsError = error as NSError

guard nsError.domain == WordPressComRestApiEndpointError.errorDomain,
nsError.code == WordPressComRestApiErrorCode.preconditionFailure.rawValue else {
failure(error)
return
}
failure(error)

let status = RewindStatus(state: .unavailable)
success(status)
return
})
}

}

private extension ActivityServiceRemote {

func mapActivitiesResponse(_ response: AnyObject) throws -> ([Activity], Int) {
func mapActivitiesResponse(_ response: Any) throws -> ([Activity], Int) {

guard let json = response as? [String: AnyObject],
let totalItems = json["totalItems"] as? Int else {
Expand Down Expand Up @@ -196,7 +201,7 @@ private extension ActivityServiceRemote {
}
}

func mapActivityGroupsResponse(_ response: AnyObject) throws -> ([ActivityGroup]) {
func mapActivityGroupsResponse(_ response: Any) throws -> ([ActivityGroup]) {
guard let json = response as? [String: AnyObject],
let totalItems = json["totalItems"] as? Int, totalItems > 0 else {
return []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public class AtomicAuthenticationServiceRemote: ServiceRemoteWordPressComREST {

public enum ResponseError: Error {
case responseIsNotADictionary(response: AnyObject)
case responseIsNotADictionary(response: Any)
case decodingFailure(response: [String: AnyObject])
case couldNotInstantiateCookie(name: String, value: String, domain: String, path: String, expires: Date)
}
Expand All @@ -16,11 +16,9 @@ public class AtomicAuthenticationServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/atomic-auth-proxy/read-access-cookies"
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)

wordPressComRestApi.GET(path,
wordPressComRESTAPI.get(path,
parameters: nil,
success: {
responseObject, _ in

success: { responseObject, _ in
do {
let settings = try self.cookie(from: responseObject)
success(settings)
Expand All @@ -39,7 +37,7 @@ public class AtomicAuthenticationServiceRemote: ServiceRemoteWordPressComREST {
return Date(timeIntervalSince1970: TimeInterval(expiration))
}

private func cookie(from responseObject: AnyObject) throws -> HTTPCookie {
private func cookie(from responseObject: Any) throws -> HTTPCookie {
guard let response = responseObject as? [String: AnyObject] else {
let error = ResponseError.responseIsNotADictionary(response: responseObject)
WPKitLogError("❗️Error: \(error)")
Expand Down
4 changes: 2 additions & 2 deletions Sources/WordPressKit/Services/AtomicSiteServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class AtomicSiteServiceRemote: ServiceRemoteWordPressComREST {
if let scrollID {
parameters["scroll_id"] = scrollID
}
wordPressComRestApi.GET(path, parameters: parameters as [String: AnyObject]) { responseObject, httpResponse in
wordPressComRESTAPI.get(path, parameters: parameters as [String: AnyObject]) { responseObject, httpResponse in
guard (200..<300).contains(httpResponse?.statusCode ?? 0),
let data = (responseObject as? [String: AnyObject])?["data"],
JSONSerialization.isValidJSONObject(data) else {
Expand Down Expand Up @@ -67,7 +67,7 @@ public final class AtomicSiteServiceRemote: ServiceRemoteWordPressComREST {
if let scrollID {
parameters["scroll_id"] = scrollID
}
wordPressComRestApi.GET(path, parameters: parameters as [String: AnyObject]) { responseObject, httpResponse in
wordPressComRESTAPI.get(path, parameters: parameters as [String: AnyObject]) { responseObject, httpResponse in
guard (200..<300).contains(httpResponse?.statusCode ?? 0),
let data = (responseObject as? [String: AnyObject])?["data"],
JSONSerialization.isValidJSONObject(data) else {
Expand Down
6 changes: 3 additions & 3 deletions Sources/WordPressKit/Services/AutomatedTransferService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AutomatedTransferService: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/automated-transfers/eligibility"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)

wordPressComRestApi.GET(path, parameters: nil, success: { (responseObject, _) in
wordPressComRESTAPI.get(path, parameters: nil, success: { (responseObject, _) in
guard let response = responseObject as? [String: AnyObject] else {
failure(.unknown)
return
Expand All @@ -53,7 +53,7 @@ public class AutomatedTransferService: ServiceRemoteWordPressComREST {
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let payload = ["plugin": pluginSlug] as [String: AnyObject]

wordPressComRestApi.POST(path, parameters: payload, success: { (responseObject, _) in
wordPressComRESTAPI.post(path, parameters: payload, success: { (responseObject, _) in
guard let response = responseObject as? [String: AnyObject] else {
failure(ResponseError.decodingFailure)
return
Expand All @@ -80,7 +80,7 @@ public class AutomatedTransferService: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/automated-transfers/status"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)

wordPressComRestApi.GET(path, parameters: nil, success: { (responseObject, _) in
wordPressComRESTAPI.get(path, parameters: nil, success: { (responseObject, _) in
guard let response = responseObject as? [String: AnyObject] else {
failure(ResponseError.decodingFailure)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public class BlogJetpackSettingsServiceRemote: ServiceRemoteWordPressComREST {

let endpoint = "jetpack-blogs/\(siteID)/rest-api"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters = ["path": "/jetpack/v4/settings"]
let parameters: [String: Any] = ["path": "/jetpack/v4/settings"]

wordPressComRestApi.GET(path,
parameters: parameters as [String: AnyObject]?,
success: {
response, _ in
guard let results = response["data"] as? [String: AnyObject],
wordPressComRESTAPI.get(path,
parameters: parameters,
success: { response, _ in
guard let responseDict = response as? [String: Any],
let results = responseDict["data"] as? [String: AnyObject],
let remoteSettings = try? self.remoteJetpackSettingsFromDictionary(results) else {
failure(ResponseError.decodingFailure)
return
Expand All @@ -38,11 +38,11 @@ public class BlogJetpackSettingsServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "jetpack-blogs/\(siteID)"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)

wordPressComRestApi.GET(path,
wordPressComRESTAPI.get(path,
parameters: nil,
success: {
response, _ in
guard let results = response["settings"] as? [String: AnyObject],
success: { response, _ in
guard let responseDict = response as? [String: Any],
let results = responseDict["settings"] as? [String: AnyObject],
let remoteMonitorSettings = try? self.remoteJetpackMonitorSettingsFromDictionary(results) else {
failure(ResponseError.decodingFailure)
return
Expand All @@ -61,11 +61,11 @@ public class BlogJetpackSettingsServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/jetpack/modules"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)

wordPressComRestApi.GET(path,
wordPressComRESTAPI.get(path,
parameters: nil,
success: {
response, _ in
guard let modules = response["modules"] as? [[String: AnyObject]],
success: { response, _ in
guard let responseDict = response as? [String: Any],
let modules = responseDict["modules"] as? [[String: AnyObject]],
let remoteModulesSettings = try? self.remoteJetpackModulesSettingsFromArray(modules) else {
failure(ResponseError.decodingFailure)
return
Expand Down Expand Up @@ -94,7 +94,7 @@ public class BlogJetpackSettingsServiceRemote: ServiceRemoteWordPressComREST {
"body": jSONBody,
"json": true] as [String: AnyObject]

wordPressComRestApi.POST(path,
wordPressComRESTAPI.post(path,
parameters: parameters,
success: {
_, _ in
Expand All @@ -113,7 +113,7 @@ public class BlogJetpackSettingsServiceRemote: ServiceRemoteWordPressComREST {
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters = dictionaryFromJetpackMonitorSettings(settings)

wordPressComRestApi.POST(path,
wordPressComRESTAPI.post(path,
parameters: parameters,
success: {
_, _ in
Expand All @@ -131,7 +131,7 @@ public class BlogJetpackSettingsServiceRemote: ServiceRemoteWordPressComREST {
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters = [ModuleOptionKeys.active: active]

wordPressComRestApi.POST(path,
wordPressComRESTAPI.post(path,
parameters: parameters as [String: AnyObject],
success: {
_, _ in
Expand All @@ -148,7 +148,7 @@ public class BlogJetpackSettingsServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "jetpack-blogs/\(siteID)/mine/delete"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)

wordPressComRestApi.POST(path,
wordPressComRESTAPI.post(path,
parameters: nil,
success: {
_, _ in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ open class BloggingPromptsServiceRemote: ServiceRemoteWordPressComREST {
return
}

wordPressComRestApi.POST(path, parameters: parameters) { responseObject, _ in
wordPressComRESTAPI.post(path, parameters: parameters) { responseObject, _ in
do {
let data = try JSONSerialization.data(withJSONObject: responseObject)
let response = try JSONDecoder().decode(UpdateBloggingPromptsSettingsResponse.self, from: data)
Expand Down
2 changes: 1 addition & 1 deletion Sources/WordPressKit/Services/DashboardServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open class DashboardServiceRemote: ServiceRemoteWordPressComREST {
failure(error)
}

wordPressComRestApi.GET(requestUrl,
wordPressComRESTAPI.get(requestUrl,
parameters: params,
success: { response, _ in
guard let cards = response as? NSDictionary else {
Expand Down
6 changes: 3 additions & 3 deletions Sources/WordPressKit/Services/EditorServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EditorServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/gutenberg?platform=mobile&editor=\(editor.rawValue)"
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)

wordPressComRestApi.POST(path, parameters: nil, success: { (responseObject, _) in
wordPressComRESTAPI.post(path, parameters: nil, success: { (responseObject, _) in
do {
let settings = try EditorSettings(with: responseObject)
success(settings)
Expand All @@ -28,7 +28,7 @@ public class EditorServiceRemote: ServiceRemoteWordPressComREST {
"set_only_if_empty": setOnlyIfEmpty
] as [String: AnyObject]

wordPressComRestApi.POST(path, parameters: parameters, success: { (responseObject, _) in
wordPressComRESTAPI.post(path, parameters: parameters, success: { (responseObject, _) in
guard let response = responseObject as? [String: String] else {
if let boolResponse = responseObject as? Bool, boolResponse == false {
return failure(EditorSettings.Error.badRequest)
Expand All @@ -51,7 +51,7 @@ public class EditorServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/gutenberg"
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)

wordPressComRestApi.GET(path, parameters: nil, success: { (responseObject, _) in
wordPressComRESTAPI.get(path, parameters: nil, success: { (responseObject, _) in
do {
let settings = try EditorSettings(with: responseObject)
success(settings)
Expand Down
2 changes: 1 addition & 1 deletion Sources/WordPressKit/Services/FeatureFlagRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class FeatureFlagRemote: ServiceRemoteWordPressComREST {
return
}

wordPressComRestApi.GET(path,
wordPressComRESTAPI.get(path,
parameters: dictionary,
success: { response, _ in

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class HomepageSettingsServiceRemote: ServiceRemoteWordPressComREST {
parameters[Keys.pageForPostsID] = postsPageID as AnyObject
}

wordPressComRestApi.POST(path, parameters: parameters,
wordPressComRESTAPI.post(path, parameters: parameters,
success: { _, _ in
success()
}, failure: { error, _ in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ open class JetpackBackupServiceRemote: ServiceRemoteWordPressComREST {
parameters["types"] = types.toDictionary() as AnyObject
}

wordPressComRestApi.POST(path, parameters: parameters, success: { response, _ in
wordPressComRESTAPI.post(path, parameters: parameters, success: { response, _ in
do {
let decoder = JSONDecoder.apiDecoder
let data = try JSONSerialization.data(withJSONObject: response, options: [])
Expand Down Expand Up @@ -88,7 +88,7 @@ open class JetpackBackupServiceRemote: ServiceRemoteWordPressComREST {

let parameters = ["dismissed": true] as [String: AnyObject]

wordPressComRestApi.POST(path, parameters: parameters, success: { _, _ in
wordPressComRESTAPI.post(path, parameters: parameters, success: { _, _ in
success()
}, failure: { error, _ in
failure(error)
Expand All @@ -109,7 +109,7 @@ open class JetpackBackupServiceRemote: ServiceRemoteWordPressComREST {
path = backupPath(for: siteID)
}

wordPressComRestApi.GET(path, parameters: nil, success: { response, _ in
wordPressComRESTAPI.get(path, parameters: nil, success: { response, _ in
do {
let decoder = JSONDecoder.apiDecoder
let data = try JSONSerialization.data(withJSONObject: response, options: [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open class JetpackCapabilitiesServiceRemote: ServiceRemoteWordPressComREST {
let endpoint = "sites/\(siteID)/rewind/capabilities"
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)

self.wordPressComRestApi.GET(path,
self.wordPressComRESTAPI.get(path,
parameters: nil,
success: { response, _ in
if let capabilities = (response as? [String: AnyObject])?["capabilities"] as? [String] {
Expand Down
4 changes: 2 additions & 2 deletions Sources/WordPressKit/Services/JetpackProxyServiceRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class JetpackProxyServiceRemote: ServiceRemoteWordPressComREST {
method: DotComMethod,
parameters: [String: AnyHashable] = [:],
locale: String? = nil,
completion: @escaping (Result<AnyObject, Error>) -> Void) -> Progress? {
completion: @escaping (Result<Any, Error>) -> Void) -> Progress? {
let urlString = self.path(forEndpoint: "jetpack-blogs/\(siteID)/rest-api", withVersion: ._1_1)

// Construct the request parameters to be forwarded to the actual endpoint.
Expand All @@ -49,7 +49,7 @@ public class JetpackProxyServiceRemote: ServiceRemoteWordPressComREST {
requestParams["locale"] = locale
}

return wordPressComRestApi.POST(urlString, parameters: requestParams as [String: AnyObject]) { response, _ in
return wordPressComRESTAPI.post(urlString, parameters: requestParams) { response, _ in
completion(.success(response))
} failure: { error, _ in
completion(.failure(error))
Expand Down
Loading

0 comments on commit 9930ab2

Please sign in to comment.