Skip to content

Swift 3 API Parity: Use URLRequest rather than NSURLRequest in APIs #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@
D3E8D6D11C367AB600295652 /* NSSpecialValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E8D6D01C367AB600295652 /* NSSpecialValue.swift */; };
D3E8D6D31C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D3E8D6D21C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist */; };
D3E8D6D51C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D3E8D6D41C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist */; };
D4FE895B1D703D1100DA7986 /* TestURLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FE895A1D703D1100DA7986 /* TestURLRequest.swift */; };
D51239DF1CD9DA0800D433EE /* CFSocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88E01BBC9B0300234F36 /* CFSocket.c */; };
D512D17C1CD883F00032E6A5 /* TestNSFileHandle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */; };
D5C40F331CDA1D460005690C /* TestNSOperationQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */; };
Expand Down Expand Up @@ -756,6 +757,7 @@
D3E8D6D01C367AB600295652 /* NSSpecialValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSSpecialValue.swift; sourceTree = "<group>"; };
D3E8D6D21C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NSKeyedUnarchiver-EdgeInsetsTest.plist"; sourceTree = "<group>"; };
D3E8D6D41C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NSKeyedUnarchiver-RectTest.plist"; sourceTree = "<group>"; };
D4FE895A1D703D1100DA7986 /* TestURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestURLRequest.swift; sourceTree = "<group>"; };
D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSFileHandle.swift; sourceTree = "<group>"; };
D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOperationQueue.swift; sourceTree = "<group>"; };
D834F9931C31C4060023812A /* TestNSOrderedSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOrderedSet.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1300,6 +1302,7 @@
EA66F65A1BF1976100136161 /* Tests */ = {
isa = PBXGroup;
children = (
D4FE895A1D703D1100DA7986 /* TestURLRequest.swift */,
C93559281C12C49F009FD6A9 /* TestNSAffineTransform.swift */,
EA66F63C1BF1619600136161 /* TestNSArray.swift */,
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */,
Expand Down Expand Up @@ -2229,6 +2232,7 @@
5B13B3251C582D4700651CE2 /* main.swift in Sources */,
5B1FD9E31D6D17B80080E83C /* TestNSURLSession.swift in Sources */,
D512D17C1CD883F00032E6A5 /* TestNSFileHandle.swift in Sources */,
D4FE895B1D703D1100DA7986 /* TestURLRequest.swift in Sources */,
5B13B33A1C582D4C00651CE2 /* TestNSNumber.swift in Sources */,
5B13B3521C582D4C00651CE2 /* TestNSValue.swift in Sources */,
5B13B3311C582D4C00651CE2 /* TestNSIndexPath.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions Foundation/NSURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ open class NSMutableURLRequest : NSURLRequest {
super.init(url: url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
}

open override func copy(with zone: NSZone? = nil) -> Any {
return mutableCopy(with: zone)
}

/*@NSCopying */ open override var url: URL? {
get { return super.url }
//TODO: set { super.URL = newValue.map{ $0.copy() as! NSURL } }
Expand Down
11 changes: 6 additions & 5 deletions Foundation/NSURLSession/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal extension URLSession {
let identifier: String?

/// default cache policy for requests
let requestCachePolicy: NSURLRequest.CachePolicy
let requestCachePolicy: URLRequest.CachePolicy

/// default timeout for requests. This will cause a timeout if no data is transmitted for the given timeout value, and is reset whenever data is transmitted.
let timeoutIntervalForRequest: TimeInterval
Expand All @@ -33,7 +33,7 @@ internal extension URLSession {
let timeoutIntervalForResource: TimeInterval

/// type of service for requests.
let networkServiceType: NSURLRequest.NetworkServiceType
let networkServiceType: URLRequest.NetworkServiceType

/// allow request to route over cellular.
let allowsCellularAccess: Bool
Expand Down Expand Up @@ -100,21 +100,22 @@ internal extension URLSession._Configuration {

// Configure NSURLRequests
internal extension URLSession._Configuration {
func configure(request: NSMutableURLRequest) {
func configure(request: URLRequest) {
var request = request
httpAdditionalHeaders?.forEach {
guard request.value(forHTTPHeaderField: $0.0) == nil else { return }
request.setValue($0.1, forHTTPHeaderField: $0.0)
}
}
func setCookies(on request: NSMutableURLRequest) {
func setCookies(on request: URLRequest) {
if httpShouldSetCookies {
//TODO: Ask the cookie storage what cookie to set.
}
}
}
// Cache Management
private extension URLSession._Configuration {
func cachedResponse(forRequest request: NSURLRequest) -> CachedURLResponse? {
func cachedResponse(forRequest request: URLRequest) -> CachedURLResponse? {
//TODO: Check the policy & consult the cache.
// There's more detail on how this should work here:
// <https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/swift/enum/c:@E@URLRequestCachePolicy>
Expand Down
30 changes: 15 additions & 15 deletions Foundation/NSURLSession/NSURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ open class URLSession : NSObject {
*/

/* Creates a data task with the given request. The request may have a body stream. */
open func dataTask(with request: NSURLRequest) -> URLSessionDataTask {
open func dataTask(with request: URLRequest) -> URLSessionDataTask {
return dataTask(with: _Request(request), behaviour: .callDelegate)
}

Expand All @@ -291,22 +291,22 @@ open class URLSession : NSObject {
}

/* Creates an upload task with the given request. The body of the request will be created from the file referenced by fileURL */
open func uploadTask(with request: NSURLRequest, fromFile fileURL: URL) -> URLSessionUploadTask {
open func uploadTask(with request: URLRequest, fromFile fileURL: URL) -> URLSessionUploadTask {
let r = URLSession._Request(request)
return uploadTask(with: r, body: .file(fileURL), behaviour: .callDelegate)
}

/* Creates an upload task with the given request. The body of the request is provided from the bodyData. */
open func uploadTask(with request: NSURLRequest, fromData bodyData: Data) -> URLSessionUploadTask {
open func uploadTask(with request: URLRequest, fromData bodyData: Data) -> URLSessionUploadTask {
let r = URLSession._Request(request)
return uploadTask(with: r, body: .data(createDispatchData(bodyData)), behaviour: .callDelegate)
}

/* Creates an upload task with the given request. The previously set body stream of the request (if any) is ignored and the URLSession:task:needNewBodyStream: delegate will be called when the body payload is required. */
open func uploadTask(withStreamedRequest request: NSURLRequest) -> URLSessionUploadTask { NSUnimplemented() }
open func uploadTask(withStreamedRequest request: URLRequest) -> URLSessionUploadTask { NSUnimplemented() }

/* Creates a download task with the given request. */
open func downloadTask(with request: NSURLRequest) -> URLSessionDownloadTask {
open func downloadTask(with request: URLRequest) -> URLSessionDownloadTask {
let r = URLSession._Request(request)
return downloadTask(with: r, behavior: .callDelegate)
}
Expand All @@ -328,10 +328,10 @@ open class URLSession : NSObject {
// Helpers
fileprivate extension URLSession {
enum _Request {
case request(NSURLRequest)
case request(URLRequest)
case url(URL)
}
func createConfiguredRequest(from request: URLSession._Request) -> NSURLRequest {
func createConfiguredRequest(from request: URLSession._Request) -> URLRequest {
let r = request.createMutableURLRequest()
_configuration.configure(request: r)
return r
Expand All @@ -341,15 +341,15 @@ extension URLSession._Request {
init(_ url: URL) {
self = .url(url)
}
init(_ request: NSURLRequest) {
init(_ request: URLRequest) {
self = .request(request)
}
}
extension URLSession._Request {
func createMutableURLRequest() -> NSMutableURLRequest {
func createMutableURLRequest() -> URLRequest {
switch self {
case .url(let url): return NSMutableURLRequest(url: url)
case .request(let r): return r.mutableCopy() as! NSMutableURLRequest
case .url(let url): return URLRequest(url: url)
case .request(let r): return r
}
}
}
Expand Down Expand Up @@ -420,7 +420,7 @@ extension URLSession {
* see <Foundation/NSURLError.h>. The delegate, if any, will still be
* called for authentication challenges.
*/
open func dataTask(with request: NSURLRequest, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionDataTask {
open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionDataTask {
return dataTask(with: _Request(request), behaviour: .dataCompletionHandler(completionHandler))
}

Expand All @@ -431,12 +431,12 @@ extension URLSession {
/*
* upload convenience method.
*/
open func uploadTask(with request: NSURLRequest, fromFile fileURL: URL, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
open func uploadTask(with request: URLRequest, fromFile fileURL: URL, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
let fileData = try! Data(contentsOf: fileURL)
return uploadTask(with: request, fromData: fileData, completionHandler: completionHandler)
}

open func uploadTask(with request: NSURLRequest, fromData bodyData: Data?, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
open func uploadTask(with request: URLRequest, fromData bodyData: Data?, completionHandler: @escaping (Data?, URLResponse?, NSError?) -> Void) -> URLSessionUploadTask {
return uploadTask(with: _Request(request), body: .data(createDispatchData(bodyData!)), behaviour: .dataCompletionHandler(completionHandler))
}

Expand All @@ -446,7 +446,7 @@ extension URLSession {
* copied during the invocation of the completion routine. The file
* will be removed automatically.
*/
open func downloadTask(with request: NSURLRequest, completionHandler: @escaping (URL?, URLResponse?, NSError?) -> Void) -> URLSessionDownloadTask {
open func downloadTask(with request: URLRequest, completionHandler: @escaping (URL?, URLResponse?, NSError?) -> Void) -> URLSessionDownloadTask {
return downloadTask(with: _Request(request), behavior: .downloadCompletionHandler(completionHandler))
}

Expand Down
10 changes: 5 additions & 5 deletions Foundation/NSURLSession/NSURLSessionConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/// on behalf of a suspended application, within certain constraints.
open class URLSessionConfiguration : NSObject, NSCopying {
public override init() {
self.requestCachePolicy = NSURLRequest.CachePolicy.useProtocolCachePolicy
self.requestCachePolicy = URLRequest.CachePolicy.useProtocolCachePolicy
self.timeoutIntervalForRequest = 60
self.timeoutIntervalForResource = 604800
self.networkServiceType = .default
Expand All @@ -51,10 +51,10 @@ open class URLSessionConfiguration : NSObject, NSCopying {
}

private init(identifier: String?,
requestCachePolicy: NSURLRequest.CachePolicy,
requestCachePolicy: URLRequest.CachePolicy,
timeoutIntervalForRequest: TimeInterval,
timeoutIntervalForResource: TimeInterval,
networkServiceType: NSURLRequest.NetworkServiceType,
networkServiceType: URLRequest.NetworkServiceType,
allowsCellularAccess: Bool,
discretionary: Bool,
connectionProxyDictionary: [AnyHashable:Any]?,
Expand Down Expand Up @@ -126,7 +126,7 @@ open class URLSessionConfiguration : NSObject, NSCopying {
open var identifier: String?

/* default cache policy for requests */
open var requestCachePolicy: NSURLRequest.CachePolicy
open var requestCachePolicy: URLRequest.CachePolicy

/* default timeout for requests. This will cause a timeout if no data is transmitted for the given timeout value, and is reset whenever data is transmitted. */
open var timeoutIntervalForRequest: TimeInterval
Expand All @@ -135,7 +135,7 @@ open class URLSessionConfiguration : NSObject, NSCopying {
open var timeoutIntervalForResource: TimeInterval

/* type of service for requests. */
open var networkServiceType: NSURLRequest.NetworkServiceType
open var networkServiceType: URLRequest.NetworkServiceType

/* allow request to route over cellular. */
open var allowsCellularAccess: Bool
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSURLSession/NSURLSessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public protocol URLSessionTaskDelegate : URLSessionDelegate {
*
* For tasks in background sessions, redirections will always be followed and this method will not be called.
*/
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: NSURLRequest, completionHandler: @escaping (NSURLRequest?) -> Void)
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void)

/* The task has received a request specific authentication challenge.
* If this delegate is not implemented, the session specific authentication challenge
Expand All @@ -122,7 +122,7 @@ public protocol URLSessionTaskDelegate : URLSessionDelegate {
}

extension URLSessionTaskDelegate {
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: NSURLRequest, completionHandler: @escaping (NSURLRequest?) -> Void) {
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
completionHandler(request)
}

Expand Down
Loading