-
Notifications
You must be signed in to change notification settings - Fork 16
/
WordPressOrgXMLRPCApi.swift
453 lines (393 loc) · 22.2 KB
/
WordPressOrgXMLRPCApi.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import Foundation
import wpxmlrpc
import Alamofire
/// Class to connect to the XMLRPC API on self hosted sites.
open class WordPressOrgXMLRPCApi: NSObject {
public typealias SuccessResponseBlock = (AnyObject, HTTPURLResponse?) -> Void
public typealias FailureReponseBlock = (_ error: NSError, _ httpResponse: HTTPURLResponse?) -> Void
public static var useURLSession = false
private let endpoint: URL
private let userAgent: String?
private var backgroundUploads: Bool
private var backgroundSessionIdentifier: String
@objc public static let defaultBackgroundSessionIdentifier = "org.wordpress.wporgxmlrpcapi"
/// onChallenge's Callback Closure Signature. Host Apps should call this method, whenever a proper AuthChallengeDisposition has been
/// picked up (optionally with URLCredentials!).
///
public typealias AuthenticationHandler = (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
/// Closure to be executed whenever we receive a URLSession Authentication Challenge.
///
public static var onChallenge: ((URLAuthenticationChallenge, @escaping AuthenticationHandler) -> Void)?
/// Minimum WordPress.org Supported Version.
///
@objc public static let minimumSupportedVersion = "4.0"
private lazy var urlSession: URLSession = makeSession(configuration: .default)
private lazy var uploadURLSession: URLSession = {
backgroundUploads
? makeSession(configuration: .background(withIdentifier: self.backgroundSessionIdentifier))
: urlSession
}()
private var _sessionManager: Alamofire.SessionManager?
private var sessionManager: Alamofire.SessionManager {
guard let sessionManager = _sessionManager else {
let sessionConfiguration = URLSessionConfiguration.default
let sessionManager = self.makeSessionManager(configuration: sessionConfiguration)
_sessionManager = sessionManager
return sessionManager
}
return sessionManager
}
private var _uploadSessionManager: Alamofire.SessionManager?
private var uploadSessionManager: Alamofire.SessionManager {
if self.backgroundUploads {
guard let uploadSessionManager = _uploadSessionManager else {
let sessionConfiguration = URLSessionConfiguration.background(withIdentifier: self.backgroundSessionIdentifier)
let sessionManager = self.makeSessionManager(configuration: sessionConfiguration)
_uploadSessionManager = sessionManager
return sessionManager
}
return uploadSessionManager
}
return sessionManager
}
private func makeSessionManager(configuration sessionConfiguration: URLSessionConfiguration) -> Alamofire.SessionManager {
var additionalHeaders: [String: AnyObject] = ["Accept-Encoding": "gzip, deflate" as AnyObject]
if let userAgent = self.userAgent {
additionalHeaders["User-Agent"] = userAgent as AnyObject?
}
sessionConfiguration.httpAdditionalHeaders = additionalHeaders
let sessionManager = Alamofire.SessionManager(configuration: sessionConfiguration)
let sessionDidReceiveChallengeWithCompletion: ((URLSession, URLAuthenticationChallenge, @escaping(URLSession.AuthChallengeDisposition, URLCredential?) -> Void) -> Void) = { [sessionDelegate] session, authenticationChallenge, completionHandler in
sessionDelegate.urlSession(session, didReceive: authenticationChallenge, completionHandler: completionHandler)
}
sessionManager.delegate.sessionDidReceiveChallengeWithCompletion = sessionDidReceiveChallengeWithCompletion
let taskDidReceiveChallengeWithCompletion: ((URLSession, URLSessionTask, URLAuthenticationChallenge, @escaping(URLSession.AuthChallengeDisposition, URLCredential?) -> Void) -> Void) = { [sessionDelegate] session, task, authenticationChallenge, completionHandler in
sessionDelegate.urlSession(session, task: task, didReceive: authenticationChallenge, completionHandler: completionHandler)
}
sessionManager.delegate.taskDidReceiveChallengeWithCompletion = taskDidReceiveChallengeWithCompletion
return sessionManager
}
private func makeSession(configuration sessionConfiguration: URLSessionConfiguration) -> URLSession {
var additionalHeaders: [String: AnyObject] = ["Accept-Encoding": "gzip, deflate" as AnyObject]
if let userAgent = self.userAgent {
additionalHeaders["User-Agent"] = userAgent as AnyObject?
}
sessionConfiguration.httpAdditionalHeaders = additionalHeaders
return URLSession(configuration: sessionConfiguration, delegate: sessionDelegate, delegateQueue: nil)
}
// swiftlint:disable weak_delegate
/// `URLSessionDelegate` for the URLSession instances in this class.
private let sessionDelegate = SessionDelegate()
// swiftlint:enable weak_delegate
/// Creates a new API object to connect to the WordPress XMLRPC API for the specified endpoint.
///
/// - Parameters:
/// - endpoint: the endpoint to connect to the xmlrpc api interface.
/// - userAgent: the user agent to use on the connection.
/// - backgroundUploads: If this value is true the API object will use a background session to execute uploads requests when using the `multipartPOST` function. The default value is false.
/// - backgroundSessionIdentifier: The session identifier to use for the background session. This must be unique in the system.
@objc public init(endpoint: URL, userAgent: String? = nil, backgroundUploads: Bool = false, backgroundSessionIdentifier: String) {
self.endpoint = endpoint
self.userAgent = userAgent
self.backgroundUploads = backgroundUploads
self.backgroundSessionIdentifier = backgroundSessionIdentifier
super.init()
}
/// Creates a new API object to connect to the WordPress XMLRPC API for the specified endpoint. The background uploads are disabled when using this initializer.
///
/// - Parameters:
/// - endpoint: the endpoint to connect to the xmlrpc api interface.
/// - userAgent: the user agent to use on the connection.
@objc convenience public init(endpoint: URL, userAgent: String? = nil) {
self.init(endpoint: endpoint, userAgent: userAgent, backgroundUploads: false, backgroundSessionIdentifier: WordPressOrgXMLRPCApi.defaultBackgroundSessionIdentifier + "." + endpoint.absoluteString)
}
deinit {
for session in [urlSession, uploadURLSession, sessionManager.session, uploadSessionManager.session] {
session.finishTasksAndInvalidate()
}
}
/**
Cancels all ongoing and makes the session so the object will not fullfil any more request
*/
@objc open func invalidateAndCancelTasks() {
for session in [urlSession, uploadURLSession, sessionManager.session, uploadSessionManager.session] {
session.invalidateAndCancel()
}
}
// MARK: - Network requests
/**
Check if username and password are valid credentials for the xmlrpc endpoint.
- parameter username: username to check
- parameter password: password to check
- parameter success: callback block to be invoked if credentials are valid, the object returned in the block is the options dictionary for the site.
- parameter failure: callback block to be invoked is credentials fail
*/
@objc open func checkCredentials(_ username: String,
password: String,
success: @escaping SuccessResponseBlock,
failure: @escaping FailureReponseBlock) {
let parameters: [AnyObject] = [0 as AnyObject, username as AnyObject, password as AnyObject]
callMethod("wp.getOptions", parameters: parameters, success: success, failure: failure)
}
/**
Executes a XMLRPC call for the method specificied with the arguments provided.
- parameter method: the xmlrpc method to be invoked
- parameter parameters: the parameters to be encoded on the request
- parameter success: callback to be called on successful request
- parameter failure: callback to be called on failed request
- returns: a NSProgress object that can be used to track the progress of the request and to cancel the request. If the method
returns nil it's because something happened on the request serialization and the network request was not started, but the failure callback
will be invoked with the error specificing the serialization issues.
*/
@objc @discardableResult open func callMethod(_ method: String,
parameters: [AnyObject]?,
success: @escaping SuccessResponseBlock,
failure: @escaping FailureReponseBlock) -> Progress? {
// Encode request
let request: URLRequest
do {
request = try requestWithMethod(method, parameters: parameters)
} catch let encodingError as NSError {
failure(encodingError, nil)
return nil
}
let progress: Progress = Progress.discreteProgress(totalUnitCount: 1)
sessionManager.request(request)
.downloadProgress { (requestProgress) in
progress.totalUnitCount = requestProgress.totalUnitCount + 1
progress.completedUnitCount = requestProgress.completedUnitCount
}.response(queue: DispatchQueue.global()) { (response) in
do {
let responseObject = try self.handleResponseWithData(response.data, urlResponse: response.response, error: response.error as NSError?)
DispatchQueue.main.async {
progress.completedUnitCount = progress.totalUnitCount
success(responseObject, response.response)
}
} catch let error as NSError {
DispatchQueue.main.async {
progress.completedUnitCount = progress.totalUnitCount
failure(error, response.response)
}
return
}
}
return progress
}
/**
Executes a XMLRPC call for the method specificied with the arguments provided, by streaming the request from a file.
This allows to do requests that can use a lot of memory, like media uploads.
- parameter method: the xmlrpc method to be invoked
- parameter parameters: the parameters to be encoded on the request
- parameter success: callback to be called on successful request
- parameter failure: callback to be called on failed request
- returns: a NSProgress object that can be used to track the progress of the request and to cancel the request. If the method
returns nil it's because something happened on the request serialization and the network request was not started, but the failure callback
will be invoked with the error specificing the serialization issues.
*/
@objc @discardableResult open func streamCallMethod(_ method: String,
parameters: [AnyObject]?,
success: @escaping SuccessResponseBlock,
failure: @escaping FailureReponseBlock) -> Progress? {
let progress: Progress = Progress.discreteProgress(totalUnitCount: 1)
progress.isCancellable = true
DispatchQueue.global().async {
let fileURL = self.URLForTemporaryFile()
// Encode request
let request: URLRequest
do {
request = try self.streamingRequestWithMethod(method, parameters: parameters, usingFileURLForCache: fileURL)
} catch let encodingError as NSError {
failure(encodingError, nil)
return
}
let uploadRequest = self.uploadSessionManager.upload(fileURL, with: request)
.uploadProgress { (requestProgress) in
progress.totalUnitCount = requestProgress.totalUnitCount + 1
progress.completedUnitCount = requestProgress.completedUnitCount
}.response(queue: DispatchQueue.global()) { (response) in
do {
let responseObject = try self.handleResponseWithData(response.data, urlResponse: response.response, error: response.error as NSError?)
DispatchQueue.main.async {
progress.completedUnitCount = progress.totalUnitCount
success(responseObject, response.response)
}
} catch let error as NSError {
DispatchQueue.main.async {
progress.completedUnitCount = progress.totalUnitCount
failure(error, response.response)
}
return
}
}
progress.cancellationHandler = {
uploadRequest.cancel()
}
}
return progress
}
// MARK: - Request Building
private func requestWithMethod(_ method: String, parameters: [AnyObject]?) throws -> URLRequest {
let mutableRequest = NSMutableURLRequest(url: endpoint)
mutableRequest.httpMethod = "POST"
mutableRequest.setValue("text/xml", forHTTPHeaderField: "Content-Type")
let encoder = WPXMLRPCEncoder(method: method, andParameters: parameters)
mutableRequest.httpBody = try encoder.dataEncoded()
return mutableRequest as URLRequest
}
private func streamingRequestWithMethod(_ method: String, parameters: [AnyObject]?, usingFileURLForCache fileURL: URL) throws -> URLRequest {
let mutableRequest = NSMutableURLRequest(url: endpoint)
mutableRequest.httpMethod = "POST"
mutableRequest.setValue("text/xml", forHTTPHeaderField: "Content-Type")
let encoder = WPXMLRPCEncoder(method: method, andParameters: parameters)
try encoder.encode(toFile: fileURL.path)
var optionalFileSize: AnyObject?
try (fileURL as NSURL).getResourceValue(&optionalFileSize, forKey: URLResourceKey.fileSizeKey)
if let fileSize = optionalFileSize as? NSNumber {
mutableRequest.setValue(fileSize.stringValue, forHTTPHeaderField: "Content-Length")
}
return mutableRequest as URLRequest
}
private func URLForTemporaryFile() -> URL {
let fileName = "\(ProcessInfo.processInfo.globallyUniqueString)_file.xmlrpc"
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
return fileURL
}
// MARK: - Handling of data
private func handleResponseWithData(_ originalData: Data?, urlResponse: URLResponse?, error: NSError?) throws -> AnyObject {
guard let data = originalData,
let httpResponse = urlResponse as? HTTPURLResponse,
let contentType = httpResponse.allHeaderFields["Content-Type"] as? String, error == nil else {
if let unwrappedError = error {
throw convertError(unwrappedError, data: originalData)
} else {
throw convertError(WordPressOrgXMLRPCApiError.unknown as NSError, data: originalData)
}
}
if (400..<600).contains(httpResponse.statusCode) {
if let decoder = WPXMLRPCDecoder(data: data), decoder.isFault(), let decoderError = decoder.error() {
// when XML-RPC is disabled for authenticated calls (e.g. xmlrpc_enabled is false on WP.org),
// it will return a valid fault payload with a non-200
throw decoderError
} else {
throw convertError(WordPressOrgXMLRPCApiError.httpErrorStatusCode as NSError, data: originalData, statusCode: httpResponse.statusCode)
}
}
if ["application/xml", "text/xml"].filter({ (type) -> Bool in return contentType.hasPrefix(type)}).count == 0 {
throw convertError(WordPressOrgXMLRPCApiError.responseSerializationFailed as NSError, data: originalData)
}
guard let decoder = WPXMLRPCDecoder(data: data) else {
throw WordPressOrgXMLRPCApiError.responseSerializationFailed
}
guard !(decoder.isFault()), let responseXML = decoder.object() else {
if let decoderError = decoder.error() {
throw convertError(decoderError as NSError, data: data)
} else {
throw WordPressOrgXMLRPCApiError.responseSerializationFailed
}
}
return responseXML as AnyObject
}
@objc public static let WordPressOrgXMLRPCApiErrorKeyData: NSError.UserInfoKey = "WordPressOrgXMLRPCApiErrorKeyData"
@objc public static let WordPressOrgXMLRPCApiErrorKeyDataString: NSError.UserInfoKey = "WordPressOrgXMLRPCApiErrorKeyDataString"
@objc public static let WordPressOrgXMLRPCApiErrorKeyStatusCode: NSError.UserInfoKey = "WordPressOrgXMLRPCApiErrorKeyStatusCode"
private func convertError(_ error: NSError, data: Data?, statusCode: Int? = nil) -> NSError {
let responseCode = statusCode == 403 ? 403 : error.code
if let data = data {
var userInfo: [AnyHashable: Any] = error.userInfo
userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyData] = data
userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyDataString] = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
userInfo[type(of: self).WordPressOrgXMLRPCApiErrorKeyStatusCode] = statusCode
userInfo[NSLocalizedFailureErrorKey] = error.localizedDescription
if let statusCode = statusCode, (400..<600).contains(statusCode) {
let formatString = NSLocalizedString("An HTTP error code %i was returned.", comment: "A failure reason for when an error HTTP status code was returned from the site, with the specific error code.")
userInfo[NSLocalizedFailureReasonErrorKey] = String(format: formatString, statusCode)
} else {
userInfo[NSLocalizedFailureReasonErrorKey] = error.localizedFailureReason
}
return NSError(domain: error.domain, code: responseCode, userInfo: userInfo as? [String: Any])
}
return error
}
}
private class SessionDelegate: NSObject, URLSessionDelegate {
@objc func urlSession(
_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
switch challenge.protectionSpace.authenticationMethod {
case NSURLAuthenticationMethodServerTrust:
if let credential = URLCredentialStorage.shared.defaultCredential(for: challenge.protectionSpace), challenge.previousFailureCount == 0 {
completionHandler(.useCredential, credential)
return
}
guard let serverTrust = challenge.protectionSpace.serverTrust else {
completionHandler(.performDefaultHandling, nil)
return
}
_ = SecTrustEvaluateWithError(serverTrust, nil)
var result = SecTrustResultType.invalid
let certificateStatus = SecTrustGetTrustResult(serverTrust, &result)
guard let hostAppHandler = WordPressOrgXMLRPCApi.onChallenge, certificateStatus == 0, result == .recoverableTrustFailure else {
completionHandler(.performDefaultHandling, nil)
return
}
DispatchQueue.main.async {
hostAppHandler(challenge, completionHandler)
}
default:
completionHandler(.performDefaultHandling, nil)
}
}
@objc func urlSession(
_ session: URLSession,
task: URLSessionTask,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
switch challenge.protectionSpace.authenticationMethod {
case NSURLAuthenticationMethodHTTPBasic:
if let credential = URLCredentialStorage.shared.defaultCredential(for: challenge.protectionSpace), challenge.previousFailureCount == 0 {
completionHandler(.useCredential, credential)
return
}
guard let hostAppHandler = WordPressOrgXMLRPCApi.onChallenge else {
completionHandler(.performDefaultHandling, nil)
return
}
DispatchQueue.main.async {
hostAppHandler(challenge, completionHandler)
}
default:
completionHandler(.performDefaultHandling, nil)
}
}
}
/// Error constants for the WordPress XML-RPC API
@objc public enum WordPressOrgXMLRPCApiError: Int, Error {
/// An error HTTP status code was returned.
case httpErrorStatusCode
/// The serialization of the request failed.
case requestSerializationFailed
/// The serialization of the response failed.
case responseSerializationFailed
/// An unknown error occurred.
case unknown
}
extension WordPressOrgXMLRPCApiError: LocalizedError {
public var errorDescription: String? {
return NSLocalizedString("There was a problem communicating with the site.", comment: "A general error message shown to the user when there was an API communication failure.")
}
public var failureReason: String? {
switch self {
case .httpErrorStatusCode:
return NSLocalizedString("An HTTP error code was returned.", comment: "A failure reason for when an error HTTP status code was returned from the site.")
case .requestSerializationFailed:
return NSLocalizedString("The serialization of the request failed.", comment: "A failure reason for when the request couldn't be serialized.")
case .responseSerializationFailed:
return NSLocalizedString("The serialization of the response failed.", comment: "A failure reason for when the response couldn't be serialized.")
case .unknown:
return NSLocalizedString("An unknown error occurred.", comment: "A failure reason for when the error that occured wasn't able to be determined.")
}
}
}