RequestModifier.swift 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. public protocol AsyncImageDownloadRequestModifier {
  28. func modified(for request: URLRequest, reportModified: @escaping (URLRequest?) -> Void)
  29. var onDownloadTaskStarted: ((DownloadTask?) -> Void)? { get }
  30. }
  31. /// Represents and wraps a method for modifying request before an image download request starts.
  32. public protocol ImageDownloadRequestModifier: AsyncImageDownloadRequestModifier {
  33. /// A method will be called just before the `request` being sent.
  34. /// This is the last chance you can modify the image download request. You can modify the request for some
  35. /// customizing purpose, such as adding auth token to the header, do basic HTTP auth or something like url mapping.
  36. ///
  37. /// Usually, you pass an `ImageDownloadRequestModifier` as the associated value of
  38. /// `KingfisherOptionsInfoItem.requestModifier` and use it as the `options` parameter in related methods.
  39. ///
  40. /// If you do nothing with the input `request` and return it as is, a downloading process will start with it.
  41. ///
  42. /// - Parameter request: The input request contains necessary information like `url`. This request is generated
  43. /// according to your resource url as a GET request.
  44. /// - Returns: A modified version of request, which you wish to use for downloading an image. If `nil` returned,
  45. /// a `KingfisherError.requestError` with `.emptyRequest` as its reason will occur.
  46. ///
  47. func modified(for request: URLRequest) -> URLRequest?
  48. }
  49. extension ImageDownloadRequestModifier {
  50. public func modified(for request: URLRequest, reportModified: @escaping (URLRequest?) -> Void) {
  51. let request = modified(for: request)
  52. reportModified(request)
  53. }
  54. public var onDownloadTaskStarted: ((DownloadTask?) -> Void)? { return nil }
  55. }
  56. /// A wrapper for creating an `ImageDownloadRequestModifier` easier.
  57. /// This type conforms to `ImageDownloadRequestModifier` and wraps an image modify block.
  58. public struct AnyModifier: ImageDownloadRequestModifier {
  59. let block: (URLRequest) -> URLRequest?
  60. /// For `ImageDownloadRequestModifier` conformation.
  61. public func modified(for request: URLRequest) -> URLRequest? {
  62. return block(request)
  63. }
  64. /// Creates a value of `ImageDownloadRequestModifier` which runs `modify` block.
  65. ///
  66. /// - Parameter modify: The request modifying block runs when a request modifying task comes.
  67. /// The return `URLRequest?` value of this block will be used as the image download request.
  68. /// If `nil` returned, a `KingfisherError.requestError` with `.emptyRequest` as its
  69. /// reason will occur.
  70. public init(modify: @escaping (URLRequest) -> URLRequest?) {
  71. block = modify
  72. }
  73. }