@@ -7,6 +7,8 @@ open class WordPressOrgXMLRPCApi: NSObject {
77 public typealias SuccessResponseBlock = ( AnyObject , HTTPURLResponse ? ) -> Void
88 public typealias FailureReponseBlock = ( _ error: NSError , _ httpResponse: HTTPURLResponse ? ) -> Void
99
10+ public static var useURLSession = false
11+
1012 private let endpoint : URL
1113 private let userAgent : String ?
1214 private var backgroundUploads : Bool
@@ -26,6 +28,13 @@ open class WordPressOrgXMLRPCApi: NSObject {
2628 ///
2729 @objc public static let minimumSupportedVersion = " 4.0 "
2830
31+ private lazy var urlSession : URLSession = makeSession ( configuration: . default)
32+ private lazy var uploadURLSession : URLSession = {
33+ backgroundUploads
34+ ? makeSession ( configuration: . background( withIdentifier: self . backgroundSessionIdentifier) )
35+ : urlSession
36+ } ( )
37+
2938 private var _sessionManager : Alamofire . SessionManager ?
3039 private var sessionManager : Alamofire . SessionManager {
3140 guard let sessionManager = _sessionManager else {
@@ -59,18 +68,32 @@ open class WordPressOrgXMLRPCApi: NSObject {
5968 sessionConfiguration. httpAdditionalHeaders = additionalHeaders
6069 let sessionManager = Alamofire . SessionManager ( configuration: sessionConfiguration)
6170
62- let sessionDidReceiveChallengeWithCompletion : ( ( URLSession , URLAuthenticationChallenge , @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) -> Void ) = { [ weak self ] session, authenticationChallenge, completionHandler in
63- self ? . urlSession ( session, didReceive: authenticationChallenge, completionHandler: completionHandler)
71+ let sessionDidReceiveChallengeWithCompletion : ( ( URLSession , URLAuthenticationChallenge , @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) -> Void ) = { [ sessionDelegate ] session, authenticationChallenge, completionHandler in
72+ sessionDelegate . urlSession ( session, didReceive: authenticationChallenge, completionHandler: completionHandler)
6473 }
6574 sessionManager. delegate. sessionDidReceiveChallengeWithCompletion = sessionDidReceiveChallengeWithCompletion
6675
67- let taskDidReceiveChallengeWithCompletion : ( ( URLSession , URLSessionTask , URLAuthenticationChallenge , @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) -> Void ) = { [ weak self ] session, task, authenticationChallenge, completionHandler in
68- self ? . urlSession ( session, task: task, didReceive: authenticationChallenge, completionHandler: completionHandler)
76+ let taskDidReceiveChallengeWithCompletion : ( ( URLSession , URLSessionTask , URLAuthenticationChallenge , @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) -> Void ) = { [ sessionDelegate ] session, task, authenticationChallenge, completionHandler in
77+ sessionDelegate . urlSession ( session, task: task, didReceive: authenticationChallenge, completionHandler: completionHandler)
6978 }
7079 sessionManager. delegate. taskDidReceiveChallengeWithCompletion = taskDidReceiveChallengeWithCompletion
7180 return sessionManager
7281 }
7382
83+ private func makeSession( configuration sessionConfiguration: URLSessionConfiguration ) -> URLSession {
84+ var additionalHeaders : [ String : AnyObject ] = [ " Accept-Encoding " : " gzip, deflate " as AnyObject ]
85+ if let userAgent = self . userAgent {
86+ additionalHeaders [ " User-Agent " ] = userAgent as AnyObject ?
87+ }
88+ sessionConfiguration. httpAdditionalHeaders = additionalHeaders
89+ return URLSession ( configuration: sessionConfiguration, delegate: sessionDelegate, delegateQueue: nil )
90+ }
91+
92+ // swiftlint:disable weak_delegate
93+ /// `URLSessionDelegate` for the URLSession instances in this class.
94+ private let sessionDelegate = SessionDelegate ( )
95+ // swiftlint:enable weak_delegate
96+
7497 /// Creates a new API object to connect to the WordPress XMLRPC API for the specified endpoint.
7598 ///
7699 /// - Parameters:
@@ -96,16 +119,18 @@ open class WordPressOrgXMLRPCApi: NSObject {
96119 }
97120
98121 deinit {
99- _sessionManager? . session. finishTasksAndInvalidate ( )
100- _uploadSessionManager? . session. finishTasksAndInvalidate ( )
122+ for session in [ urlSession, uploadURLSession, sessionManager. session, uploadSessionManager. session] {
123+ session. finishTasksAndInvalidate ( )
124+ }
101125 }
102126
103127 /**
104128 Cancels all ongoing and makes the session so the object will not fullfil any more request
105129 */
106130 @objc open func invalidateAndCancelTasks( ) {
107- sessionManager. session. invalidateAndCancel ( )
108- uploadSessionManager. session. invalidateAndCancel ( )
131+ for session in [ urlSession, uploadURLSession, sessionManager. session, uploadSessionManager. session] {
132+ session. invalidateAndCancel ( )
133+ }
109134 }
110135
111136 // MARK: - Network requests
@@ -155,14 +180,15 @@ open class WordPressOrgXMLRPCApi: NSObject {
155180 progress. totalUnitCount = requestProgress. totalUnitCount + 1
156181 progress. completedUnitCount = requestProgress. completedUnitCount
157182 } . response ( queue: DispatchQueue . global ( ) ) { ( response) in
158- progress. completedUnitCount = progress. totalUnitCount
159183 do {
160184 let responseObject = try self . handleResponseWithData ( response. data, urlResponse: response. response, error: response. error as NSError ? )
161185 DispatchQueue . main. async {
186+ progress. completedUnitCount = progress. totalUnitCount
162187 success ( responseObject, response. response)
163188 }
164189 } catch let error as NSError {
165190 DispatchQueue . main. async {
191+ progress. completedUnitCount = progress. totalUnitCount
166192 failure ( error, response. response)
167193 }
168194 return
@@ -208,12 +234,13 @@ open class WordPressOrgXMLRPCApi: NSObject {
208234 } . response ( queue: DispatchQueue . global ( ) ) { ( response) in
209235 do {
210236 let responseObject = try self . handleResponseWithData ( response. data, urlResponse: response. response, error: response. error as NSError ? )
211- progress. completedUnitCount = progress. totalUnitCount
212237 DispatchQueue . main. async {
238+ progress. completedUnitCount = progress. totalUnitCount
213239 success ( responseObject, response. response)
214240 }
215241 } catch let error as NSError {
216242 DispatchQueue . main. async {
243+ progress. completedUnitCount = progress. totalUnitCount
217244 failure ( error, response. response)
218245 }
219246 return
@@ -327,11 +354,13 @@ open class WordPressOrgXMLRPCApi: NSObject {
327354 }
328355}
329356
330- extension WordPressOrgXMLRPCApi {
357+ private class SessionDelegate : NSObject , URLSessionDelegate {
331358
332- @objc public func urlSession( _ session: URLSession ,
333- didReceive challenge: URLAuthenticationChallenge ,
334- completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) {
359+ @objc func urlSession(
360+ _ session: URLSession ,
361+ didReceive challenge: URLAuthenticationChallenge ,
362+ completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void
363+ ) {
335364
336365 switch challenge. protectionSpace. authenticationMethod {
337366 case NSURLAuthenticationMethodServerTrust:
@@ -363,10 +392,12 @@ extension WordPressOrgXMLRPCApi {
363392 }
364393 }
365394
366- @objc public func urlSession( _ session: URLSession ,
367- task: URLSessionTask ,
368- didReceive challenge: URLAuthenticationChallenge ,
369- completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) {
395+ @objc func urlSession(
396+ _ session: URLSession ,
397+ task: URLSessionTask ,
398+ didReceive challenge: URLAuthenticationChallenge ,
399+ completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void
400+ ) {
370401
371402 switch challenge. protectionSpace. authenticationMethod {
372403 case NSURLAuthenticationMethodHTTPBasic:
0 commit comments