Browse Source

Use async version for handleHTTPRedirection delegate

onevcat 2 years ago
parent
commit
a4fc79a882

+ 11 - 9
Sources/Networking/RedirectHandler.swift

@@ -47,8 +47,8 @@ public protocol ImageDownloadRedirectHandler {
     func handleHTTPRedirection(
         for task: SessionDataTask,
         response: HTTPURLResponse,
-        newRequest: URLRequest,
-        completionHandler: @escaping (URLRequest?) -> Void)
+        newRequest: URLRequest
+    ) async -> URLRequest?
 }
 
 /// A wrapper for creating an `ImageDownloadRedirectHandler` easier.
@@ -56,14 +56,15 @@ public protocol ImageDownloadRedirectHandler {
 public struct AnyRedirectHandler: ImageDownloadRedirectHandler {
     
     let block: (SessionDataTask, HTTPURLResponse, URLRequest, @escaping (URLRequest?) -> Void) -> Void
-
+    
     public func handleHTTPRedirection(
-        for task: SessionDataTask,
-        response: HTTPURLResponse,
-        newRequest: URLRequest,
-        completionHandler: @escaping (URLRequest?) -> Void)
-    {
-        block(task, response, newRequest, completionHandler)
+        for task: SessionDataTask, response: HTTPURLResponse, newRequest: URLRequest
+    ) async -> URLRequest? {
+        return await withCheckedContinuation { continuation in
+            block(task, response, newRequest, { urlRequest in
+                continuation.resume(returning: urlRequest)
+            })
+        }
     }
     
     /// Creates a value of `ImageDownloadRedirectHandler` which runs `modify` block.
@@ -73,4 +74,5 @@ public struct AnyRedirectHandler: ImageDownloadRedirectHandler {
     public init(handle: @escaping (SessionDataTask, HTTPURLResponse, URLRequest, @escaping (URLRequest?) -> Void) -> Void) {
         block = handle
     }
+    
 }

+ 4 - 10
Sources/Networking/SessionDelegate.swift

@@ -244,21 +244,15 @@ extension SessionDelegate: URLSessionDataDelegate {
         _ session: URLSession,
         task: URLSessionTask,
         willPerformHTTPRedirection response: HTTPURLResponse,
-        newRequest request: URLRequest,
-        completionHandler: @escaping (URLRequest?) -> Void)
+        newRequest request: URLRequest
+    ) async -> URLRequest?
     {
         guard let sessionDataTask = self.task(for: task),
               let redirectHandler = Array(sessionDataTask.callbacks).last?.options.redirectHandler else
         {
-            completionHandler(request)
-            return
+            return request
         }
-        
-        redirectHandler.handleHTTPRedirection(
-            for: sessionDataTask,
-            response: response,
-            newRequest: request,
-            completionHandler: completionHandler)
+        return await redirectHandler.handleHTTPRedirection(for: sessionDataTask, response: response, newRequest: request)
     }
 
     private func onCompleted(task: URLSessionTask, result: Result<(Data, URLResponse?), KingfisherError>) {

+ 4 - 2
Tests/KingfisherTests/KingfisherOptionsInfoTests.swift

@@ -153,7 +153,9 @@ class TestModifier: ImageDownloadRequestModifier {
 }
 
 class TestRedirectHandler: ImageDownloadRedirectHandler {
-    func handleHTTPRedirection(for task: SessionDataTask, response: HTTPURLResponse, newRequest: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
-        completionHandler(newRequest)
+    func handleHTTPRedirection(
+        for task: Kingfisher.SessionDataTask, response: HTTPURLResponse, newRequest: URLRequest
+    ) async -> URLRequest? {
+        newRequest
     }
 }