1 |
efrain |
1 |
//
|
|
|
2 |
// CachedResponseHandler.swift
|
|
|
3 |
//
|
|
|
4 |
// Copyright (c) 2019 Alamofire Software Foundation (http://alamofire.org/)
|
|
|
5 |
//
|
|
|
6 |
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
7 |
// of this software and associated documentation files (the "Software"), to deal
|
|
|
8 |
// in the Software without restriction, including without limitation the rights
|
|
|
9 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
10 |
// copies of the Software, and to permit persons to whom the Software is
|
|
|
11 |
// furnished to do so, subject to the following conditions:
|
|
|
12 |
//
|
|
|
13 |
// The above copyright notice and this permission notice shall be included in
|
|
|
14 |
// all copies or substantial portions of the Software.
|
|
|
15 |
//
|
|
|
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
17 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
18 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
19 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
20 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
21 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
22 |
// THE SOFTWARE.
|
|
|
23 |
//
|
|
|
24 |
|
|
|
25 |
import Foundation
|
|
|
26 |
|
|
|
27 |
/// A type that handles whether the data task should store the HTTP response in the cache.
|
|
|
28 |
public protocol CachedResponseHandler {
|
|
|
29 |
/// Determines whether the HTTP response should be stored in the cache.
|
|
|
30 |
///
|
|
|
31 |
/// The `completion` closure should be passed one of three possible options:
|
|
|
32 |
///
|
|
|
33 |
/// 1. The cached response provided by the server (this is the most common use case).
|
|
|
34 |
/// 2. A modified version of the cached response (you may want to modify it in some way before caching).
|
|
|
35 |
/// 3. A `nil` value to prevent the cached response from being stored in the cache.
|
|
|
36 |
///
|
|
|
37 |
/// - Parameters:
|
|
|
38 |
/// - task: The data task whose request resulted in the cached response.
|
|
|
39 |
/// - response: The cached response to potentially store in the cache.
|
|
|
40 |
/// - completion: The closure to execute containing cached response, a modified response, or `nil`.
|
|
|
41 |
func dataTask(_ task: URLSessionDataTask,
|
|
|
42 |
willCacheResponse response: CachedURLResponse,
|
|
|
43 |
completion: @escaping (CachedURLResponse?) -> Void)
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
// MARK: -
|
|
|
47 |
|
|
|
48 |
/// `ResponseCacher` is a convenience `CachedResponseHandler` making it easy to cache, not cache, or modify a cached
|
|
|
49 |
/// response.
|
|
|
50 |
public struct ResponseCacher {
|
|
|
51 |
/// Defines the behavior of the `ResponseCacher` type.
|
|
|
52 |
public enum Behavior {
|
|
|
53 |
/// Stores the cached response in the cache.
|
|
|
54 |
case cache
|
|
|
55 |
/// Prevents the cached response from being stored in the cache.
|
|
|
56 |
case doNotCache
|
|
|
57 |
/// Modifies the cached response before storing it in the cache.
|
|
|
58 |
case modify((URLSessionDataTask, CachedURLResponse) -> CachedURLResponse?)
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/// Returns a `ResponseCacher` with a `.cache` `Behavior`.
|
|
|
62 |
public static let cache = ResponseCacher(behavior: .cache)
|
|
|
63 |
/// Returns a `ResponseCacher` with a `.doNotCache` `Behavior`.
|
|
|
64 |
public static let doNotCache = ResponseCacher(behavior: .doNotCache)
|
|
|
65 |
|
|
|
66 |
/// The `Behavior` of the `ResponseCacher`.
|
|
|
67 |
public let behavior: Behavior
|
|
|
68 |
|
|
|
69 |
/// Creates a `ResponseCacher` instance from the `Behavior`.
|
|
|
70 |
///
|
|
|
71 |
/// - Parameter behavior: The `Behavior`.
|
|
|
72 |
public init(behavior: Behavior) {
|
|
|
73 |
self.behavior = behavior
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
extension ResponseCacher: CachedResponseHandler {
|
|
|
78 |
public func dataTask(_ task: URLSessionDataTask,
|
|
|
79 |
willCacheResponse response: CachedURLResponse,
|
|
|
80 |
completion: @escaping (CachedURLResponse?) -> Void) {
|
|
|
81 |
switch behavior {
|
|
|
82 |
case .cache:
|
|
|
83 |
completion(response)
|
|
|
84 |
case .doNotCache:
|
|
|
85 |
completion(nil)
|
|
|
86 |
case let .modify(closure):
|
|
|
87 |
let response = closure(task, response)
|
|
|
88 |
completion(response)
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
#if swift(>=5.5)
|
|
|
94 |
extension CachedResponseHandler where Self == ResponseCacher {
|
|
|
95 |
/// Provides a `ResponseCacher` which caches the response, if allowed. Equivalent to `ResponseCacher.cache`.
|
|
|
96 |
public static var cache: ResponseCacher { .cache }
|
|
|
97 |
|
|
|
98 |
/// Provides a `ResponseCacher` which does not cache the response. Equivalent to `ResponseCacher.doNotCache`.
|
|
|
99 |
public static var doNotCache: ResponseCacher { .doNotCache }
|
|
|
100 |
|
|
|
101 |
/// Creates a `ResponseCacher` which modifies the proposed `CachedURLResponse` using the provided closure.
|
|
|
102 |
///
|
|
|
103 |
/// - Parameter closure: Closure used to modify the `CachedURLResponse`.
|
|
|
104 |
/// - Returns: The `ResponseCacher`.
|
|
|
105 |
public static func modify(using closure: @escaping ((URLSessionDataTask, CachedURLResponse) -> CachedURLResponse?)) -> ResponseCacher {
|
|
|
106 |
ResponseCacher(behavior: .modify(closure))
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
#endif
|