如何扩展 URLRequest
How to Extend URLRequest
尝试编写可维护、干净的代码的结果:
func fetchNumbersFromServer(completion: @escaping (NumbersResult) -> Void) {
let urlString = "https://some-site.com/some-file"
var request = URLRequest(url: URL.init(string: urlString)!)
// I want to configure the request, yet make the code extenda
request = URLRequest.configure(request: request)
// create URLSession configuration, delegate and session
...
session.dataTask(with: request) { (data, response, error) -> Void in
// handle response
}.resume()
}
extension URLRequest {
static func configure(request: URLRequest) -> URLRequest {
// request in must be made mutable
var request = request
request.setValue("some user agent", forHTTPHeaderField: "User-Agent")
request.timeoutInterval = 10
return request
}
}
通过使用类型方法,这可以修改请求。但是,这对我来说感觉很尴尬,所以我想知道是否有更好(或更有效)的方法来完成 'configuring' URLRequest。
使用以下方式怎么样?
改变当前实例:
mutating func configure(headers:[String: String], timeOutInterval: TimeInterval = 10) {
for key in headers.keys {
self.setValue(headers[key] ?? "", forHTTPHeaderField: key)
}
self.timeoutInterval = timeoutInterval
}
或者,返回新副本:
func configure(headers:[String: String], timeOutInterval: TimeInterval = 10) -> URLRequest {
var request = self
for key in headers.keys {
request.setValue(headers[key] ?? "", forHTTPHeaderField: key)
}
request.timeoutInterval = timeoutInterval
return request
}
您可以只创建一个 URLRequest 自定义初始化程序并为参数设置一个默认值:
extension URLRequest {
init(_ url: URL, userAgent: String = "your default user agent", cachePolicy: CachePolicy = .useProtocolCachePolicy, timeInterval: TimeInterval = 60) {
self.init(url: url, cachePolicy: cachePolicy, timeoutInterval: timeInterval)
setValue(userAgent, forHTTPHeaderField: "User-Agent")
}
}
struct NumbersResult { }
func fetchNumbersFromServer(request: URLRequest, completion: @escaping (NumbersResult) -> ()) {
URLSession.shared.dataTask(with: request) { data, response, error in
// handle response
}.resume()
}
let url = URL(string: "https://www.google.com")!
let request = URLRequest(url) // you can also pass another userAgent, cachePolicy and timeInterval here if needed
fetchNumbersFromServer(request: request) { numbers in
}
尝试编写可维护、干净的代码的结果:
func fetchNumbersFromServer(completion: @escaping (NumbersResult) -> Void) {
let urlString = "https://some-site.com/some-file"
var request = URLRequest(url: URL.init(string: urlString)!)
// I want to configure the request, yet make the code extenda
request = URLRequest.configure(request: request)
// create URLSession configuration, delegate and session
...
session.dataTask(with: request) { (data, response, error) -> Void in
// handle response
}.resume()
}
extension URLRequest {
static func configure(request: URLRequest) -> URLRequest {
// request in must be made mutable
var request = request
request.setValue("some user agent", forHTTPHeaderField: "User-Agent")
request.timeoutInterval = 10
return request
}
}
通过使用类型方法,这可以修改请求。但是,这对我来说感觉很尴尬,所以我想知道是否有更好(或更有效)的方法来完成 'configuring' URLRequest。
使用以下方式怎么样?
改变当前实例:
mutating func configure(headers:[String: String], timeOutInterval: TimeInterval = 10) {
for key in headers.keys {
self.setValue(headers[key] ?? "", forHTTPHeaderField: key)
}
self.timeoutInterval = timeoutInterval
}
或者,返回新副本:
func configure(headers:[String: String], timeOutInterval: TimeInterval = 10) -> URLRequest {
var request = self
for key in headers.keys {
request.setValue(headers[key] ?? "", forHTTPHeaderField: key)
}
request.timeoutInterval = timeoutInterval
return request
}
您可以只创建一个 URLRequest 自定义初始化程序并为参数设置一个默认值:
extension URLRequest {
init(_ url: URL, userAgent: String = "your default user agent", cachePolicy: CachePolicy = .useProtocolCachePolicy, timeInterval: TimeInterval = 60) {
self.init(url: url, cachePolicy: cachePolicy, timeoutInterval: timeInterval)
setValue(userAgent, forHTTPHeaderField: "User-Agent")
}
}
struct NumbersResult { }
func fetchNumbersFromServer(request: URLRequest, completion: @escaping (NumbersResult) -> ()) {
URLSession.shared.dataTask(with: request) { data, response, error in
// handle response
}.resume()
}
let url = URL(string: "https://www.google.com")!
let request = URLRequest(url) // you can also pass another userAgent, cachePolicy and timeInterval here if needed
fetchNumbersFromServer(request: request) { numbers in
}