Skip to content

Commit

Permalink
Migrate onReceiveSessionTaskChallenge and onReceiveSessionTaskChallen…
Browse files Browse the repository at this point in the history
…ge from closure to function (in the past this allowed reuse code for challenge responses between Kingfisher and Alamofire libraries), simplified their signature as the first two parameters were ignored, avoids useless initializers for URLSession and URLSessionDataTask (#736)

Signed-off-by: Dan Cunningham <[email protected]>
Co-authored-by: Dan Cunningham <[email protected]>
  • Loading branch information
timbms and digitaldan authored Oct 29, 2023
1 parent f01f92d commit 95bf809
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
6 changes: 2 additions & 4 deletions OpenHABCore/Sources/OpenHABCore/Util/NetworkConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import Alamofire
import Foundation
import os.log

// https://medium.com/@AladinWay/write-a-networking-layer-in-swift-4-using-alamofire-5-and-codable-part-2-perform-request-and-b5c7ee2e012d

public let onReceiveSessionTaskChallenge = { (_: URLSession, _: URLSessionTask, challenge: URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?) in
public func onReceiveSessionTaskChallenge(with challenge: URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?) {
os_log("onReceiveSessionTaskChallenge host:'%{PUBLIC}@'", log: .default, type: .error, challenge.protectionSpace.host)
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
Expand All @@ -34,7 +32,7 @@ public let onReceiveSessionTaskChallenge = { (_: URLSession, _: URLSessionTask,
return (disposition, credential)
}

public let onReceiveSessionChallenge = { (_: URLSession, challenge: URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?) in
public func onReceiveSessionChallenge(with challenge: URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?) {
os_log("onReceiveSessionChallenge host:'%{PUBLIC}@'", log: .default, type: .error, challenge.protectionSpace.host)
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
Expand Down
4 changes: 2 additions & 2 deletions openHAB/OpenHABSitemapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -811,15 +811,15 @@ extension OpenHABSitemapViewController: AuthenticationChallengeResponsible {
task: URLSessionTask,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let (disposition, credential) = onReceiveSessionTaskChallenge(URLSession(configuration: .default), task, challenge)
let (disposition, credential) = onReceiveSessionTaskChallenge(with: challenge)
completionHandler(disposition, credential)
}

// sessionDelegate.onReceiveSessionChallenge
func downloader(_ downloader: ImageDownloader,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let (disposition, credential) = onReceiveSessionChallenge(URLSession(configuration: .default), challenge)
let (disposition, credential) = onReceiveSessionChallenge(with: challenge)
completionHandler(disposition, credential)
}
}
4 changes: 2 additions & 2 deletions openHAB/OpenHABWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ extension OpenHABWebViewController: WKNavigationDelegate {
var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
var credential: URLCredential?
if challenge.protectionSpace.authenticationMethod.isAny(of: NSURLAuthenticationMethodHTTPBasic, NSURLAuthenticationMethodDefault) {
(disposition, credential) = onReceiveSessionTaskChallenge(URLSession(configuration: .default), URLSessionDataTask(), challenge)
(disposition, credential) = onReceiveSessionTaskChallenge(with: challenge)
} else {
(disposition, credential) = onReceiveSessionChallenge(URLSession(configuration: .default), challenge)
(disposition, credential) = onReceiveSessionChallenge(with: challenge)
}
completionHandler(disposition, credential)
}
Expand Down
2 changes: 1 addition & 1 deletion openHAB/WebUITableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ extension WebUITableViewCell: WKNavigationDelegate {
}

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let (disposition, credential) = onReceiveSessionChallenge(URLSession(configuration: .default), challenge)
let (disposition, credential) = onReceiveSessionChallenge(with: challenge)
completionHandler(disposition, credential)
}
}
Expand Down
4 changes: 2 additions & 2 deletions openHABWatch Extension/ExtensionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ extension ExtensionDelegate: AuthenticationChallengeResponsible {
task: URLSessionTask,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let (disposition, credential) = onReceiveSessionTaskChallenge(URLSession(configuration: .default), task, challenge)
let (disposition, credential) = onReceiveSessionTaskChallenge(with: challenge)
completionHandler(disposition, credential)
}

// sessionDelegate.onReceiveSessionChallenge
func downloader(_ downloader: ImageDownloader,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let (disposition, credential) = onReceiveSessionChallenge(URLSession(configuration: .default), challenge)
let (disposition, credential) = onReceiveSessionChallenge(with: challenge)
completionHandler(disposition, credential)
}
}
Expand Down

0 comments on commit 95bf809

Please sign in to comment.