forked from onevcat/Kingfisher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request onevcat#1072 from devuarpd/Add-redirect-handler
Add redirect handler.
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// RedirectHandler.swift | ||
// Kingfisher | ||
// | ||
// Created by Roman Maidanovych on 2018/12/10. | ||
// | ||
// Copyright (c) 2018 Wei Wang <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
import Foundation | ||
|
||
/// Represents and wraps a method for modifying request during an image download request redirection. | ||
public protocol ImageDownloadRedirectHandler { | ||
|
||
/// The `ImageDownloadRedirectHandler` contained will be used to change the request before redirection. | ||
/// This is the posibility you can modify the image download request during redirection. You can modify the request for | ||
/// some customizing purpose, such as adding auth token to the header, do basic HTTP auth or something like url | ||
/// mapping. | ||
/// | ||
/// Usually, you pass an `ImageDownloadRedirectHandler` as the associated value of | ||
/// `KingfisherOptionsInfoItem.redirectHandler` and use it as the `options` parameter in related methods. | ||
/// | ||
/// If you do nothing with the input `request` and return it as is, a downloading process will redirect with it. | ||
/// | ||
/// - Parameter task: current SessionDataTask. | ||
/// response: response received during redirection. | ||
/// newRequest: request for redirection which can be modified. | ||
/// completionHandler: closure for modifying request | ||
/// | ||
func handleHTTPRedirection(for task: SessionDataTask, response: HTTPURLResponse, newRequest: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) | ||
} | ||
|
||
struct NoHandler: ImageDownloadRedirectHandler { | ||
static let `default` = NoHandler() | ||
private init() {} | ||
|
||
func handleHTTPRedirection(for task: SessionDataTask, response: HTTPURLResponse, newRequest: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) { | ||
completionHandler(newRequest) | ||
} | ||
} | ||
|
||
/// A wrapper for creating an `ImageDownloadRedirectHandler` easier. | ||
/// This type conforms to `ImageDownloadRedirectHandler` and wraps an image modify block. | ||
public struct AnyHandler: ImageDownloadRedirectHandler { | ||
|
||
let block: (SessionDataTask, HTTPURLResponse, URLRequest, (URLRequest?) -> Void) -> Void | ||
|
||
public func handleHTTPRedirection(for task: SessionDataTask, response: HTTPURLResponse, newRequest: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) { | ||
block(task, response, newRequest, completionHandler) | ||
} | ||
|
||
/// Creates a value of `ImageDownloadRedirectHandler` which runs `modify` block. | ||
/// | ||
/// - Parameter modify: The request modifying block runs when a request modifying task comes. | ||
/// | ||
public init(handle: @escaping (SessionDataTask, HTTPURLResponse, URLRequest, (URLRequest?) -> Void) -> Void) { | ||
block = handle | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters