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
147 lines (125 loc) · 5.64 KB
/
Copy pathJetpackScanServiceRemote.swift
File metadata and controls
147 lines (125 loc) · 5.64 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
import Foundation
import WordPressShared
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 responseValue = response["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.GETData(path, parameters: nil) { result in
result
.flatMap { data, _ in
Result {
try JSONDecoder.apiDecoder.decode(JetpackScan.self, from: data)
}
}
.invoke(success, or: failure)
}
}
// 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) { result in
let transformed = result.flatMap { data, _ in
Result {
try JSONDecoder.apiDecoder.decode(JetpackThreatFixResponse.self, from: data)
}
}
switch transformed {
case let .success(response):
success(response)
case let .failure(error):
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.GETData(path, parameters: parameters) { result in
result
.flatMap { data, _ in
Result {
try JSONDecoder.apiDecoder.decode(JetpackThreatFixResponse.self, from: data)
}
}
.invoke(success, or: failure)
}
}
// MARK: - History
public func getHistoryForSite(_ siteID: Int, success: @escaping(JetpackScanHistory) -> Void, failure: @escaping(Error) -> Void) {
let path = scanPath(for: siteID, with: "history")
wordPressComRestApi.GETData(path, parameters: nil) { result in
result
.flatMap { data, _ in
Result {
try JSONDecoder.apiDecoder.decode(JetpackScanHistory.self, from: data)
}
}
.invoke(success, or: failure)
}
}
// 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)
}
}