You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
3.4 KiB

  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.
  28. public protocol ImageDownloadRequestModifier {
  29. /// A 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. ///
  33. /// Usually, you pass an `ImageDownloadRequestModifier` as the associated value of
  34. /// `KingfisherOptionsInfoItem.requestModifier` and use it as the `options` parameter in related methods.
  35. ///
  36. /// If you do nothing with the input `request` and return it as is, a downloading process will start with it.
  37. ///
  38. /// - Parameter request: The input request contains necessary information like `url`. This request is generated
  39. /// according to your resource url as a GET request.
  40. /// - Returns: A modified version of request, which you wish to use for downloading an image. If `nil` returned,
  41. /// a `KingfisherError.requestError` with `.emptyRequest` as its reason will occur.
  42. ///
  43. func modified(for request: URLRequest) -> URLRequest?
  44. }
  45. /// A wrapper for creating an `ImageDownloadRequestModifier` easier.
  46. /// This type conforms to `ImageDownloadRequestModifier` and wraps an image modify block.
  47. public struct AnyModifier: ImageDownloadRequestModifier {
  48. let block: (URLRequest) -> URLRequest?
  49. /// For `ImageDownloadRequestModifier` conformation.
  50. public func modified(for request: URLRequest) -> URLRequest? {
  51. return block(request)
  52. }
  53. /// Creates a value of `ImageDownloadRequestModifier` which runs `modify` block.
  54. ///
  55. /// - Parameter modify: The request modifying block runs when a request modifying task comes.
  56. /// The return `URLRequest?` value of this block will be used as the image download request.
  57. /// If `nil` returned, a `KingfisherError.requestError` with `.emptyRequest` as its
  58. /// reason will occur.
  59. public init(modify: @escaping (URLRequest) -> URLRequest?) {
  60. block = modify
  61. }
  62. }