This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathJetpackScanServiceRemote.swift
More file actions
160 lines (133 loc) · 6.19 KB
/
Copy pathJetpackScanServiceRemote.swift
File metadata and controls
160 lines (133 loc) · 6.19 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import Foundation
public class JetpackScanServiceRemote: ServiceRemoteWordPressComREST {
// MARK: - Scanning
public func getScanAvailableForSite(_ siteID: Int, success: @escaping(Bool) -> Void, failure: @escaping(Error) -> Void) {
getScanForSite(siteID, success: { (scan) in
success(scan.isEnabled)
}, failure: failure)
}
public func getCurrentScanStatusForSite(_ siteID: Int, success: @escaping(JetpackScanStatus?) -> Void, failure: @escaping(Error) -> Void) {
getScanForSite(siteID, success: { scan in
success(scan.current)
}, failure: failure)
}
/// Starts a scan for a site
public func startScanForSite(_ siteID: Int, success: @escaping(Bool) -> Void, failure: @escaping(Error) -> Void) {
let path = self.scanPath(for: siteID, with: "enqueue")
wordPressComRESTAPI.post(path, parameters: nil, success: { (response, _) in
guard let responseDict = response as? [String: Any],
let responseValue = responseDict["success"] as? Bool else {
success(false)
return
}
success(responseValue)
}, failure: { (error, _) in
failure(error)
})
}
/// Gets the main scan object
public func getScanForSite(_ siteID: Int, success: @escaping(JetpackScan) -> Void, failure: @escaping(Error) -> Void) {
let path = self.scanPath(for: siteID)
wordPressComRESTAPI.get(path, parameters: nil, success: { (response, _) in
do {
let decoder = JSONDecoder.apiDecoder
let data = try JSONSerialization.data(withJSONObject: response, options: [])
let envelope = try decoder.decode(JetpackScan.self, from: data)
success(envelope)
} catch {
failure(error)
}
}, failure: { (error, _) in
failure(error)
})
}
// MARK: - Threats
public enum ThreatError: Swift.Error {
case invalidResponse
}
public func getThreatsForSite(_ siteID: Int, success: @escaping([JetpackScanThreat]?) -> Void, failure: @escaping(Error) -> Void) {
getScanForSite(siteID, success: { scan in
success(scan.threats)
}, failure: failure)
}
/// Begins the fix process for multiple threats
public func fixThreats(_ threats: [JetpackScanThreat], siteID: Int, success: @escaping(JetpackThreatFixResponse) -> Void, failure: @escaping(Error) -> Void) {
let path = self.path(forEndpoint: "sites/\(siteID)/alerts/fix", withVersion: ._2_0)
let parameters = ["threat_ids": threats.map { $0.id as AnyObject }] as [String: AnyObject]
wordPressComRESTAPI.post(path, parameters: parameters, success: { (response, _) in
do {
let decoder = JSONDecoder.apiDecoder
let data = try JSONSerialization.data(withJSONObject: response, options: [])
let envelope = try decoder.decode(JetpackThreatFixResponse.self, from: data)
success(envelope)
} catch {
failure(error)
}
}, failure: { (error, _) in
failure(error)
})
}
/// Begins the fix process for a single threat
public func fixThreat(_ threat: JetpackScanThreat, siteID: Int, success: @escaping(JetpackThreatFixStatus) -> Void, failure: @escaping(Error) -> Void) {
fixThreats([threat], siteID: siteID, success: { response in
guard let status = response.threats.first else {
failure(ThreatError.invalidResponse)
return
}
success(status)
}, failure: { error in
failure(error)
})
}
/// Begins the ignore process for a single threat
public func ignoreThreat(_ threat: JetpackScanThreat, siteID: Int, success: @escaping () -> Void, failure: @escaping(Error) -> Void) {
let path = self.path(forEndpoint: "sites/\(siteID)/alerts/\(threat.id)", withVersion: ._2_0)
let parameters = ["ignore": true] as [String: AnyObject]
wordPressComRESTAPI.post(path, parameters: parameters, success: { (_, _) in
success()
}, failure: { (error, _) in
failure(error)
})
}
/// Returns the fix status for multiple threats
public func getFixStatusForThreats(_ threats: [JetpackScanThreat], siteID: Int, success: @escaping(JetpackThreatFixResponse) -> Void, failure: @escaping(Error) -> Void) {
let path = self.path(forEndpoint: "sites/\(siteID)/alerts/fix", withVersion: ._2_0)
let parameters = ["threat_ids": threats.map { $0.id as AnyObject }] as [String: AnyObject]
wordPressComRESTAPI.get(path, parameters: parameters, success: { (response, _) in
do {
let decoder = JSONDecoder.apiDecoder
let data = try JSONSerialization.data(withJSONObject: response, options: [])
let envelope = try decoder.decode(JetpackThreatFixResponse.self, from: data)
success(envelope)
} catch {
failure(error)
}
}, failure: { (error, _) in
failure(error)
})
}
// MARK: - History
public func getHistoryForSite(_ siteID: Int, success: @escaping(JetpackScanHistory) -> Void, failure: @escaping(Error) -> Void) {
let path = scanPath(for: siteID, with: "history")
wordPressComRESTAPI.get(path, parameters: nil, success: { (response, _) in
do {
let decoder = JSONDecoder.apiDecoder
let data = try JSONSerialization.data(withJSONObject: response, options: [])
let envelope = try decoder.decode(JetpackScanHistory.self, from: data)
success(envelope)
} catch {
failure(error)
}
}, failure: { (error, _) in
failure(error)
})
}
// MARK: - Private
private func scanPath(for siteID: Int, with path: String? = nil) -> String {
var endpoint = "sites/\(siteID)/scan/"
if let path = path {
endpoint = endpoint.appending(path)
}
return self.path(forEndpoint: endpoint, withVersion: ._2_0)
}
}