-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathJetpackServiceRemote.swift
116 lines (103 loc) · 4.49 KB
/
JetpackServiceRemote.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import Foundation
public struct JetpackInstallError: LocalizedError, Equatable {
public enum ErrorType: String {
case invalidCredentials = "INVALID_CREDENTIALS"
case forbidden = "FORBIDDEN"
case installFailure = "INSTALL_FAILURE"
case installResponseError = "INSTALL_RESPONSE_ERROR"
case loginFailure = "LOGIN_FAILURE"
case siteIsJetpack = "SITE_IS_JETPACK"
case activationOnInstallFailure = "ACTIVATION_ON_INSTALL_FAILURE"
case activationResponseError = "ACTIVATION_RESPONSE_ERROR"
case activationFailure = "ACTIVATION_FAILURE"
case unknown
init(error key: String) {
self = ErrorType(rawValue: key) ?? .unknown
}
}
public var title: String?
public var code: Int
public var type: ErrorType
public static var unknown: JetpackInstallError {
return JetpackInstallError(type: .unknown)
}
public init(title: String? = nil, code: Int = 0, key: String? = nil) {
self.init(title: title, code: code, type: ErrorType(error: key ?? ""))
}
public init(title: String? = nil, code: Int = 0, type: ErrorType = .unknown) {
self.title = title
self.code = code
self.type = type
}
}
public class JetpackServiceRemote: ServiceRemoteWordPressComREST {
public enum ResponseError: Error {
case decodingFailed
}
public func checkSiteHasJetpack(_ url: URL,
success: @escaping (Bool) -> Void,
failure: @escaping (Error?) -> Void) {
let path = self.path(forEndpoint: "connect/site-info", withVersion: ._1_0)
let parameters = ["url": url.absoluteString as AnyObject]
wordPressComRESTAPI.get(path,
parameters: parameters,
success: { [weak self] response, _ in
do {
let hasJetpack = try self?.hasJetpackMapping(object: response)
success(hasJetpack ?? false)
} catch {
failure(error)
}
}) { error, _ in
failure(error)
}
}
public func installJetpack(url: String,
username: String,
password: String,
completion: @escaping (Bool, JetpackInstallError?) -> Void) {
guard let escapedURL = url.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else {
completion(false, .unknown)
return
}
let path = String(format: "jetpack-install/%@/", escapedURL)
let requestUrl = self.path(forEndpoint: path, withVersion: ._1_0)
let parameters = ["user": username,
"password": password]
wordPressComRESTAPI.post(requestUrl,
parameters: parameters as [String: AnyObject],
success: { response, _ in
if let response = response as? [String: Bool],
let success = response[Constants.status] {
completion(success, nil)
} else {
completion(false, JetpackInstallError(type: .installResponseError))
}
}) { error, _ in
guard let apiError = error as? WordPressAPIError<WordPressComRestApiEndpointError>,
case .endpointError(let endpointError) = apiError else {
completion(false, nil)
return
}
let jetpackError = JetpackInstallError(
title: endpointError.localizedDescription,
code: endpointError.errorCode,
key: endpointError.apiErrorCode
)
completion(false, jetpackError)
}
}
private enum Constants {
static let hasJetpack = "hasJetpack"
static let status = "status"
}
}
private extension JetpackServiceRemote {
func hasJetpackMapping(object: Any) throws -> Bool {
guard let response = object as? [String: AnyObject],
let hasJetpack = response[Constants.hasJetpack] as? NSNumber else {
throw ResponseError.decodingFailed
}
return hasJetpack.boolValue
}
}