RequestModifier.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // RequestModifier.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 2016/09/05.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. /// Represents and wraps a method for modifying request before an image download request starts in an asynchronous way.
  28. public protocol AsyncImageDownloadRequestModifier {
  29. /// This method will be called just before the `request` being sent.
  30. /// This is the last chance you can modify the image download request. You can modify the request for some
  31. /// customizing purpose, such as adding auth token to the header, do basic HTTP auth or something like url mapping.
  32. /// When you have done with the modification, call the `reportModified` block with the modified request and the data
  33. /// download will happen with this request.
  34. ///
  35. /// Usually, you pass an `AsyncImageDownloadRequestModifier` as the associated value of
  36. /// `KingfisherOptionsInfoItem.requestModifier` and use it as the `options` parameter in related methods.
  37. ///
  38. /// If you do nothing with the input `request` and return it as is, a downloading process will start with it.
  39. ///
  40. /// - Parameters:
  41. /// - request: The input request contains necessary information like `url`. This request is generated
  42. /// according to your resource url as a GET request.
  43. /// - reportModified: The callback block you need to call after the asynchronous modifying done.
  44. ///
  45. func modified(for request: URLRequest, reportModified: @escaping (URLRequest?) -> Void)
  46. /// A block will be called when the download task started.
  47. ///
  48. /// If an `AsyncImageDownloadRequestModifier` and the asynchronous modification happens before the download, the
  49. /// related download method will not return a valid `DownloadTask` value. Instead, you can get one from this method.
  50. var onDownloadTaskStarted: ((DownloadTask?) -> Void)? { get }
  51. }
  52. /// Represents and wraps a method for modifying request before an image download request starts.
  53. public protocol ImageDownloadRequestModifier: AsyncImageDownloadRequestModifier {
  54. /// This method will be called just before the `request` being sent.
  55. /// This is the last chance you can modify the image download request. You can modify the request for some
  56. /// customizing purpose, such as adding auth token to the header, do basic HTTP auth or something like url mapping.
  57. ///
  58. /// Usually, you pass an `ImageDownloadRequestModifier` as the associated value of
  59. /// `KingfisherOptionsInfoItem.requestModifier` and use it as the `options` parameter in related methods.
  60. ///
  61. /// If you do nothing with the input `request` and return it as is, a downloading process will start with it.
  62. ///
  63. /// - Parameter request: The input request contains necessary information like `url`. This request is generated
  64. /// according to your resource url as a GET request.
  65. /// - Returns: A modified version of request, which you wish to use for downloading an image. If `nil` returned,
  66. /// a `KingfisherError.requestError` with `.emptyRequest` as its reason will occur.
  67. ///
  68. func modified(for request: URLRequest) -> URLRequest?
  69. }
  70. extension ImageDownloadRequestModifier {
  71. public func modified(for request: URLRequest, reportModified: @escaping (URLRequest?) -> Void) {
  72. let request = modified(for: request)
  73. reportModified(request)
  74. }
  75. /// This is `nil` for a sync `ImageDownloadRequestModifier` by default. You can get the `DownloadTask` from the
  76. /// return value of downloader method.
  77. public var onDownloadTaskStarted: ((DownloadTask?) -> Void)? { return nil }
  78. }
  79. /// A wrapper for creating an `ImageDownloadRequestModifier` easier.
  80. /// This type conforms to `ImageDownloadRequestModifier` and wraps an image modify block.
  81. public struct AnyModifier: ImageDownloadRequestModifier {
  82. let block: (URLRequest) -> URLRequest?
  83. /// For `ImageDownloadRequestModifier` conformation.
  84. public func modified(for request: URLRequest) -> URLRequest? {
  85. return block(request)
  86. }
  87. /// Creates a value of `ImageDownloadRequestModifier` which runs `modify` block.
  88. ///
  89. /// - Parameter modify: The request modifying block runs when a request modifying task comes.
  90. /// The return `URLRequest?` value of this block will be used as the image download request.
  91. /// If `nil` returned, a `KingfisherError.requestError` with `.emptyRequest` as its
  92. /// reason will occur.
  93. public init(modify: @escaping (URLRequest) -> URLRequest?) {
  94. block = modify
  95. }
  96. }