12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import Foundation
- public protocol CachedResponseHandler {
-
-
-
-
-
-
-
-
-
-
-
-
- func dataTask(_ task: URLSessionDataTask,
- willCacheResponse response: CachedURLResponse,
- completion: @escaping (CachedURLResponse?) -> Void)
- }
- public struct ResponseCacher {
-
- public enum Behavior {
-
- case cache
-
- case doNotCache
-
- case modify((URLSessionDataTask, CachedURLResponse) -> CachedURLResponse?)
- }
-
- public static let cache = ResponseCacher(behavior: .cache)
-
- public static let doNotCache = ResponseCacher(behavior: .doNotCache)
-
- public let behavior: Behavior
-
-
-
- public init(behavior: Behavior) {
- self.behavior = behavior
- }
- }
- extension ResponseCacher: CachedResponseHandler {
- public func dataTask(_ task: URLSessionDataTask,
- willCacheResponse response: CachedURLResponse,
- completion: @escaping (CachedURLResponse?) -> Void) {
- switch behavior {
- case .cache:
- completion(response)
- case .doNotCache:
- completion(nil)
- case let .modify(closure):
- let response = closure(task, response)
- completion(response)
- }
- }
- }
|