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 pathPluginServiceRemote.swift
More file actions
254 lines (227 loc) · 10.5 KB
/
Copy pathPluginServiceRemote.swift
File metadata and controls
254 lines (227 loc) · 10.5 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import Foundation
public class PluginServiceRemote: ServiceRemoteWordPressComREST {
public enum ResponseError: Error {
case decodingFailure
case invalidInputError
case unauthorized
case unknownError
}
public func getFeaturedPlugins(success: @escaping ([PluginDirectoryEntry]) -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "wpcom/v2/plugins/featured"
wordPressComRESTAPI.get(endpoint, parameters: nil, success: { (responseObject, _) in
guard let response = responseObject as? [[String: AnyObject]] else {
failure(ResponseError.decodingFailure)
return
}
do {
let pluginEntries = try response.map { try PluginDirectoryEntry(responseObject: $0) }
success(pluginEntries)
} catch {
failure(ResponseError.decodingFailure)
}
}, failure: { (error, _) in
WPKitLogError("[PluginServiceRemoteError] Error fetching featured plugins: \(error)")
failure(error)
})
}
public func getPlugins(siteID: Int, success: @escaping (SitePlugins) -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "sites/\(siteID)/plugins"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_2)
let parameters = [String: AnyObject]()
wordPressComRESTAPI.get(path, parameters: parameters, success: { (responseObject, _) in
guard let response = responseObject as? [String: AnyObject] else {
failure(ResponseError.decodingFailure)
return
}
do {
let pluginStates = try self.pluginStates(response: response)
let capabilities = try self.pluginCapabilities(response: response)
success(SitePlugins(plugins: pluginStates, capabilities: capabilities))
} catch {
failure(self.errorFromResponse(response))
}
}, failure: { (error, _) in
WPKitLogError("[PluginServiceRemoteError] Error fetching site plugins: \(error)")
failure(error)
})
}
public func updatePlugin(pluginID: String, siteID: Int, success: @escaping (PluginState) -> Void, failure: @escaping (Error) -> Void) {
guard let escapedPluginID = encoded(pluginID: pluginID) else {
return
}
let endpoint = "sites/\(siteID)/plugins/\(escapedPluginID)/update"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_2)
let parameters = [String: AnyObject]()
wordPressComRESTAPI.post(
path,
parameters: parameters,
success: { (responseObject, _) in
guard let response = responseObject as? [String: AnyObject] else {
failure(ResponseError.decodingFailure)
return
}
do {
let pluginState = try self.pluginState(response: response)
success(pluginState)
} catch {
failure(self.errorFromResponse(response))
}
},
failure: { (error, _) in
WPKitLogError("[PluginServiceRemoteError] Error updating plugin: \(error)")
failure(error)
})
}
public func activatePlugin(pluginID: String, siteID: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let parameters = [
"active": "true"
] as [String: AnyObject]
modifyPlugin(parameters: parameters, pluginID: pluginID, siteID: siteID, success: success, failure: failure)
}
public func deactivatePlugin(pluginID: String, siteID: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let parameters = [
"active": "false"
] as [String: AnyObject]
modifyPlugin(parameters: parameters, pluginID: pluginID, siteID: siteID, success: success, failure: failure)
}
public func enableAutoupdates(pluginID: String, siteID: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let parameters = [
"autoupdate": "true"
] as [String: AnyObject]
modifyPlugin(parameters: parameters, pluginID: pluginID, siteID: siteID, success: success, failure: failure)
}
public func disableAutoupdates(pluginID: String, siteID: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let parameters = [
"autoupdate": "false"
] as [String: AnyObject]
modifyPlugin(parameters: parameters, pluginID: pluginID, siteID: siteID, success: success, failure: failure)
}
public func activateAndEnableAutoupdates(pluginID: String, siteID: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let parameters = [
"active": "true",
"autoupdate": "true"
] as [String: AnyObject]
modifyPlugin(parameters: parameters, pluginID: pluginID, siteID: siteID, success: success, failure: failure)
}
public func install(pluginSlug: String, siteID: Int, success: @escaping (PluginState) -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "sites/\(siteID)/plugins/\(pluginSlug)/install"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_2)
wordPressComRESTAPI.post(
path,
parameters: nil,
success: { responseObject, _ in
guard let response = responseObject as? [String: AnyObject] else {
failure(ResponseError.decodingFailure)
return
}
do {
let pluginState = try self.pluginState(response: response)
success(pluginState)
} catch {
failure(self.errorFromResponse(response))
}
}, failure: { (error, _) in
WPKitLogError("[PluginServiceRemoteError] Error installing plugin: \(error)")
failure(error)
}
)
}
public func remove(pluginID: String, siteID: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
guard let escapedPluginID = encoded(pluginID: pluginID) else {
return
}
let endpoint = "sites/\(siteID)/plugins/\(escapedPluginID)/delete"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_2)
wordPressComRESTAPI.post(
path,
parameters: nil,
success: { _, _ in
success()
}, failure: { (error, _) in
WPKitLogError("[PluginServiceRemoteError] Error removing plugin: \(error)")
failure(error)
}
)
}
private func modifyPlugin(parameters: [String: AnyObject], pluginID: String, siteID: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
guard let escapedPluginID = encoded(pluginID: pluginID) else {
return
}
let endpoint = "sites/\(siteID)/plugins/\(escapedPluginID)"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_2)
wordPressComRESTAPI.post(
path,
parameters: parameters,
success: { _, _ in
success()
},
failure: { (error, _) in
WPKitLogError("[PluginServiceRemoteError] Error modifying plugin: \(error)")
failure(error)
})
}
}
internal extension PluginServiceRemote {
func encoded(pluginID: String) -> String? {
let allowedCharacters = CharacterSet.urlPathAllowed.subtracting(CharacterSet(charactersIn: "/"))
guard let escapedPluginID = pluginID.addingPercentEncoding(withAllowedCharacters: allowedCharacters) else {
assertionFailure("Can't escape plugin ID: \(pluginID)")
return nil
}
return escapedPluginID
}
func pluginStates(response: [String: AnyObject]) throws -> [PluginState] {
guard let plugins = response["plugins"] as? [[String: AnyObject]] else {
throw ResponseError.decodingFailure
}
return try plugins.map { (plugin) -> PluginState in
return try pluginState(response: plugin)
}
}
func pluginState(response: [String: AnyObject]) throws -> PluginState {
guard let id = response["name"] as? String,
let slug = response["slug"] as? String,
let active = response["active"] as? Bool,
let autoupdate = response["autoupdate"] as? Bool,
let name = response["display_name"] as? String,
let author = response["author"] as? String else {
throw ResponseError.decodingFailure
}
let version = (response["version"] as? String)?.nonEmptyString()
let url = (response["plugin_url"] as? String).flatMap(URL.init(string:))
let availableUpdate = (response["update"] as? [String: String])?["new_version"]
let updateState: PluginState.UpdateState = availableUpdate.map({ .available($0) }) ?? .updated
let actions = response["action_links"] as? [String: String]
let settingsURL = (actions?["Settings"]).flatMap(URL.init(string:))
return PluginState(id: id,
slug: slug,
active: active,
name: name,
author: author,
version: version,
updateState: updateState,
autoupdate: autoupdate,
automanaged: false,
url: url,
settingsURL: settingsURL)
}
func pluginCapabilities(response: [String: AnyObject]) throws -> SitePluginCapabilities {
guard let capabilities = response["file_mod_capabilities"] as? [String: AnyObject],
let modify = capabilities["modify_files"] as? Bool,
let autoupdate = capabilities["autoupdate_files"] as? Bool else {
throw ResponseError.decodingFailure
}
return SitePluginCapabilities(modify: modify, autoupdate: autoupdate)
}
func errorFromResponse(_ response: [String: AnyObject]) -> ResponseError {
guard let code = response["error"] as? String else {
return .decodingFailure
}
switch code {
case "unauthorized":
return .unauthorized
default:
return .unknownError
}
}
}