CachedResponseHandler.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import Foundation
  25. /// A type that handles whether the data task should store the HTTP response in the cache.
  26. public protocol CachedResponseHandler {
  27. /// Determines whether the HTTP response should be stored in the cache.
  28. ///
  29. /// The `completion` closure should be passed one of three possible options:
  30. ///
  31. /// 1. The cached response provided by the server (this is the most common use case).
  32. /// 2. A modified version of the cached response (you may want to modify it in some way before caching).
  33. /// 3. A `nil` value to prevent the cached response from being stored in the cache.
  34. ///
  35. /// - Parameters:
  36. /// - task: The data task whose request resulted in the cached response.
  37. /// - response: The cached response to potentially store in the cache.
  38. /// - completion: The closure to execute containing cached response, a modified response, or `nil`.
  39. func dataTask(_ task: URLSessionDataTask,
  40. willCacheResponse response: CachedURLResponse,
  41. completion: @escaping (CachedURLResponse?) -> Void)
  42. }
  43. // MARK: -
  44. /// `ResponseCacher` is a convenience `CachedResponseHandler` making it easy to cache, not cache, or modify a cached
  45. /// response.
  46. public struct ResponseCacher {
  47. /// Defines the behavior of the `ResponseCacher` type.
  48. public enum Behavior {
  49. /// Stores the cached response in the cache.
  50. case cache
  51. /// Prevents the cached response from being stored in the cache.
  52. case doNotCache
  53. /// Modifies the cached response before storing it in the cache.
  54. case modify((URLSessionDataTask, CachedURLResponse) -> CachedURLResponse?)
  55. }
  56. /// Returns a `ResponseCacher` with a `.cache` `Behavior`.
  57. public static let cache = ResponseCacher(behavior: .cache)
  58. /// Returns a `ResponseCacher` with a `.doNotCache` `Behavior`.
  59. public static let doNotCache = ResponseCacher(behavior: .doNotCache)
  60. /// The `Behavior` of the `ResponseCacher`.
  61. public let behavior: Behavior
  62. /// Creates a `ResponseCacher` instance from the `Behavior`.
  63. ///
  64. /// - Parameter behavior: The `Behavior`.
  65. public init(behavior: Behavior) {
  66. self.behavior = behavior
  67. }
  68. }
  69. extension ResponseCacher: CachedResponseHandler {
  70. public func dataTask(_ task: URLSessionDataTask,
  71. willCacheResponse response: CachedURLResponse,
  72. completion: @escaping (CachedURLResponse?) -> Void) {
  73. switch behavior {
  74. case .cache:
  75. completion(response)
  76. case .doNotCache:
  77. completion(nil)
  78. case let .modify(closure):
  79. let response = closure(task, response)
  80. completion(response)
  81. }
  82. }
  83. }
  84. extension CachedResponseHandler where Self == ResponseCacher {
  85. /// Provides a `ResponseCacher` which caches the response, if allowed. Equivalent to `ResponseCacher.cache`.
  86. public static var cache: ResponseCacher { .cache }
  87. /// Provides a `ResponseCacher` which does not cache the response. Equivalent to `ResponseCacher.doNotCache`.
  88. public static var doNotCache: ResponseCacher { .doNotCache }
  89. /// Creates a `ResponseCacher` which modifies the proposed `CachedURLResponse` using the provided closure.
  90. ///
  91. /// - Parameter closure: Closure used to modify the `CachedURLResponse`.
  92. /// - Returns: The `ResponseCacher`.
  93. public static func modify(using closure: @escaping ((URLSessionDataTask, CachedURLResponse) -> CachedURLResponse?)) -> ResponseCacher {
  94. ResponseCacher(behavior: .modify(closure))
  95. }
  96. }