Browse Source

Moved Upload extensions into a separate Swift file.

Christian Noon 10 years ago
parent
commit
4003fb2402
3 changed files with 181 additions and 154 deletions
  1. 6 0
      Alamofire.xcodeproj/project.pbxproj
  2. 0 154
      Source/Alamofire.swift
  3. 175 0
      Source/Upload.swift

+ 6 - 0
Alamofire.xcodeproj/project.pbxproj

@@ -13,6 +13,8 @@
 		4CDE2C3B1AF899EC00BABAE5 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C391AF899EC00BABAE5 /* Request.swift */; };
 		4CDE2C3B1AF899EC00BABAE5 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C391AF899EC00BABAE5 /* Request.swift */; };
 		4CDE2C3D1AF89D4900BABAE5 /* Download.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C3C1AF89D4900BABAE5 /* Download.swift */; };
 		4CDE2C3D1AF89D4900BABAE5 /* Download.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C3C1AF89D4900BABAE5 /* Download.swift */; };
 		4CDE2C3E1AF89D4900BABAE5 /* Download.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C3C1AF89D4900BABAE5 /* Download.swift */; };
 		4CDE2C3E1AF89D4900BABAE5 /* Download.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C3C1AF89D4900BABAE5 /* Download.swift */; };
+		4CDE2C401AF89E0700BABAE5 /* Upload.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C3F1AF89E0700BABAE5 /* Upload.swift */; };
+		4CDE2C411AF89E0700BABAE5 /* Upload.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDE2C3F1AF89E0700BABAE5 /* Upload.swift */; };
 		4CE2724F1AF88FB500F1D59A /* ParameterEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */; };
 		4CE2724F1AF88FB500F1D59A /* ParameterEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */; };
 		4CE272501AF88FB500F1D59A /* ParameterEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */; };
 		4CE272501AF88FB500F1D59A /* ParameterEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */; };
 		4DD67C241A5C58FB00ED2280 /* Alamofire.h in Headers */ = {isa = PBXBuildFile; fileRef = F8111E3819A95C8B0040E7D1 /* Alamofire.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		4DD67C241A5C58FB00ED2280 /* Alamofire.h in Headers */ = {isa = PBXBuildFile; fileRef = F8111E3819A95C8B0040E7D1 /* Alamofire.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -61,6 +63,7 @@
 		4CDE2C361AF8932A00BABAE5 /* Manager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Manager.swift; sourceTree = "<group>"; };
 		4CDE2C361AF8932A00BABAE5 /* Manager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Manager.swift; sourceTree = "<group>"; };
 		4CDE2C391AF899EC00BABAE5 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = "<group>"; };
 		4CDE2C391AF899EC00BABAE5 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = "<group>"; };
 		4CDE2C3C1AF89D4900BABAE5 /* Download.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Download.swift; sourceTree = "<group>"; };
 		4CDE2C3C1AF89D4900BABAE5 /* Download.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Download.swift; sourceTree = "<group>"; };
+		4CDE2C3F1AF89E0700BABAE5 /* Upload.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Upload.swift; sourceTree = "<group>"; };
 		4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParameterEncoding.swift; sourceTree = "<group>"; };
 		4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParameterEncoding.swift; sourceTree = "<group>"; };
 		4DD67C0B1A5C55C900ED2280 /* Alamofire.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Alamofire.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		4DD67C0B1A5C55C900ED2280 /* Alamofire.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Alamofire.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		F8111E3319A95C8B0040E7D1 /* Alamofire.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Alamofire.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		F8111E3319A95C8B0040E7D1 /* Alamofire.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Alamofire.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -142,6 +145,7 @@
 				4CDE2C361AF8932A00BABAE5 /* Manager.swift */,
 				4CDE2C361AF8932A00BABAE5 /* Manager.swift */,
 				4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */,
 				4CE2724E1AF88FB500F1D59A /* ParameterEncoding.swift */,
 				4CDE2C391AF899EC00BABAE5 /* Request.swift */,
 				4CDE2C391AF899EC00BABAE5 /* Request.swift */,
+				4CDE2C3F1AF89E0700BABAE5 /* Upload.swift */,
 				F8111E3619A95C8B0040E7D1 /* Supporting Files */,
 				F8111E3619A95C8B0040E7D1 /* Supporting Files */,
 			);
 			);
 			path = Source;
 			path = Source;
@@ -354,6 +358,7 @@
 			isa = PBXSourcesBuildPhase;
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			buildActionMask = 2147483647;
 			files = (
 			files = (
+				4CDE2C411AF89E0700BABAE5 /* Upload.swift in Sources */,
 				4CE272501AF88FB500F1D59A /* ParameterEncoding.swift in Sources */,
 				4CE272501AF88FB500F1D59A /* ParameterEncoding.swift in Sources */,
 				4CDE2C3B1AF899EC00BABAE5 /* Request.swift in Sources */,
 				4CDE2C3B1AF899EC00BABAE5 /* Request.swift in Sources */,
 				4CDE2C381AF8932A00BABAE5 /* Manager.swift in Sources */,
 				4CDE2C381AF8932A00BABAE5 /* Manager.swift in Sources */,
@@ -366,6 +371,7 @@
 			isa = PBXSourcesBuildPhase;
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			buildActionMask = 2147483647;
 			files = (
 			files = (
+				4CDE2C401AF89E0700BABAE5 /* Upload.swift in Sources */,
 				4CE2724F1AF88FB500F1D59A /* ParameterEncoding.swift in Sources */,
 				4CE2724F1AF88FB500F1D59A /* ParameterEncoding.swift in Sources */,
 				4CDE2C3A1AF899EC00BABAE5 /* Request.swift in Sources */,
 				4CDE2C3A1AF899EC00BABAE5 /* Request.swift in Sources */,
 				4CDE2C371AF8932A00BABAE5 /* Manager.swift in Sources */,
 				4CDE2C371AF8932A00BABAE5 /* Manager.swift in Sources */,

+ 0 - 154
Source/Alamofire.swift

@@ -209,160 +209,6 @@ extension Request {
     }
     }
 }
 }
 
 
-// MARK: - Upload
-
-extension Manager {
-    private enum Uploadable {
-        case Data(NSURLRequest, NSData)
-        case File(NSURLRequest, NSURL)
-        case Stream(NSURLRequest, NSInputStream)
-    }
-
-    private func upload(uploadable: Uploadable) -> Request {
-        var uploadTask: NSURLSessionUploadTask!
-        var HTTPBodyStream: NSInputStream?
-
-        switch uploadable {
-        case .Data(let request, let data):
-            dispatch_sync(queue) {
-                uploadTask = self.session.uploadTaskWithRequest(request, fromData: data)
-            }
-        case .File(let request, let fileURL):
-            dispatch_sync(queue) {
-                uploadTask = self.session.uploadTaskWithRequest(request, fromFile: fileURL)
-            }
-        case .Stream(let request, var stream):
-            dispatch_sync(queue) {
-                uploadTask = self.session.uploadTaskWithStreamedRequest(request)
-            }
-            HTTPBodyStream = stream
-        }
-
-        let request = Request(session: session, task: uploadTask)
-        if HTTPBodyStream != nil {
-            request.delegate.taskNeedNewBodyStream = { _, _ in
-                return HTTPBodyStream
-            }
-        }
-        delegate[request.delegate.task] = request.delegate
-
-        if startRequestsImmediately {
-            request.resume()
-        }
-
-        return request
-    }
-
-    // MARK: File
-
-    /**
-        Creates a request for uploading a file to the specified URL request.
-
-        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
-
-        :param: URLRequest The URL request
-        :param: file The file to upload
-
-        :returns: The created upload request.
-    */
-    public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
-        return upload(.File(URLRequest.URLRequest, file))
-    }
-
-    /**
-        Creates a request for uploading a file to the specified URL request.
-
-        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
-
-        :param: method The HTTP method.
-        :param: URLString The URL string.
-        :param: file The file to upload
-
-        :returns: The created upload request.
-    */
-    public func upload(method: Method, _ URLString: URLStringConvertible, file: NSURL) -> Request {
-        return upload(URLRequest(method, URLString), file: file)
-    }
-
-    // MARK: Data
-
-    /**
-        Creates a request for uploading data to the specified URL request.
-
-        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
-
-        :param: URLRequest The URL request
-        :param: data The data to upload
-
-        :returns: The created upload request.
-    */
-    public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
-        return upload(.Data(URLRequest.URLRequest, data))
-    }
-
-    /**
-        Creates a request for uploading data to the specified URL request.
-
-        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
-
-        :param: method The HTTP method.
-        :param: URLString The URL string.
-        :param: data The data to upload
-
-        :returns: The created upload request.
-    */
-    public func upload(method: Method, _ URLString: URLStringConvertible, data: NSData) -> Request {
-        return upload(URLRequest(method, URLString), data: data)
-    }
-
-    // MARK: Stream
-
-    /**
-        Creates a request for uploading a stream to the specified URL request.
-
-        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
-
-        :param: URLRequest The URL request
-        :param: stream The stream to upload
-
-        :returns: The created upload request.
-    */
-    public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
-        return upload(.Stream(URLRequest.URLRequest, stream))
-    }
-
-    /**
-        Creates a request for uploading a stream to the specified URL request.
-
-        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
-
-        :param: method The HTTP method.
-        :param: URLString The URL string.
-        :param: stream The stream to upload.
-
-        :returns: The created upload request.
-    */
-    public func upload(method: Method, _ URLString: URLStringConvertible, stream: NSInputStream) -> Request {
-        return upload(URLRequest(method, URLString), stream: stream)
-    }
-}
-
-extension Request {
-    class UploadTaskDelegate: DataTaskDelegate {
-        var uploadTask: NSURLSessionUploadTask! { return task as! NSURLSessionUploadTask }
-        var uploadProgress: ((Int64, Int64, Int64) -> Void)!
-
-        // MARK: NSURLSessionTaskDelegate
-
-        func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
-            progress.totalUnitCount = totalBytesExpectedToSend
-            progress.completedUnitCount = totalBytesSent
-
-            uploadProgress?(bytesSent, totalBytesSent, totalBytesExpectedToSend)
-        }
-    }
-}
-
 // MARK: - Response Serializers
 // MARK: - Response Serializers
 
 
 // MARK: String
 // MARK: String

+ 175 - 0
Source/Upload.swift

@@ -0,0 +1,175 @@
+// Alamofire.swift
+//
+// Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
+//
+// 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
+
+extension Manager {
+    private enum Uploadable {
+        case Data(NSURLRequest, NSData)
+        case File(NSURLRequest, NSURL)
+        case Stream(NSURLRequest, NSInputStream)
+    }
+    
+    private func upload(uploadable: Uploadable) -> Request {
+        var uploadTask: NSURLSessionUploadTask!
+        var HTTPBodyStream: NSInputStream?
+        
+        switch uploadable {
+        case .Data(let request, let data):
+            dispatch_sync(queue) {
+                uploadTask = self.session.uploadTaskWithRequest(request, fromData: data)
+            }
+        case .File(let request, let fileURL):
+            dispatch_sync(queue) {
+                uploadTask = self.session.uploadTaskWithRequest(request, fromFile: fileURL)
+            }
+        case .Stream(let request, var stream):
+            dispatch_sync(queue) {
+                uploadTask = self.session.uploadTaskWithStreamedRequest(request)
+            }
+            HTTPBodyStream = stream
+        }
+        
+        let request = Request(session: session, task: uploadTask)
+        if HTTPBodyStream != nil {
+            request.delegate.taskNeedNewBodyStream = { _, _ in
+                return HTTPBodyStream
+            }
+        }
+        delegate[request.delegate.task] = request.delegate
+        
+        if startRequestsImmediately {
+            request.resume()
+        }
+        
+        return request
+    }
+    
+    // MARK: File
+    
+    /**
+        Creates a request for uploading a file to the specified URL request.
+        
+        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
+        
+        :param: URLRequest The URL request
+        :param: file The file to upload
+        
+        :returns: The created upload request.
+    */
+    public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
+        return upload(.File(URLRequest.URLRequest, file))
+    }
+    
+    /**
+        Creates a request for uploading a file to the specified URL request.
+        
+        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
+        
+        :param: method The HTTP method.
+        :param: URLString The URL string.
+        :param: file The file to upload
+        
+        :returns: The created upload request.
+    */
+    public func upload(method: Method, _ URLString: URLStringConvertible, file: NSURL) -> Request {
+        return upload(URLRequest(method, URLString), file: file)
+    }
+    
+    // MARK: Data
+    
+    /**
+        Creates a request for uploading data to the specified URL request.
+        
+        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
+        
+        :param: URLRequest The URL request
+        :param: data The data to upload
+        
+        :returns: The created upload request.
+    */
+    public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
+        return upload(.Data(URLRequest.URLRequest, data))
+    }
+    
+    /**
+        Creates a request for uploading data to the specified URL request.
+        
+        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
+        
+        :param: method The HTTP method.
+        :param: URLString The URL string.
+        :param: data The data to upload
+        
+        :returns: The created upload request.
+    */
+    public func upload(method: Method, _ URLString: URLStringConvertible, data: NSData) -> Request {
+        return upload(URLRequest(method, URLString), data: data)
+    }
+    
+    // MARK: Stream
+    
+    /**
+        Creates a request for uploading a stream to the specified URL request.
+        
+        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
+        
+        :param: URLRequest The URL request
+        :param: stream The stream to upload
+        
+        :returns: The created upload request.
+    */
+    public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
+        return upload(.Stream(URLRequest.URLRequest, stream))
+    }
+    
+    /**
+        Creates a request for uploading a stream to the specified URL request.
+        
+        If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
+        
+        :param: method The HTTP method.
+        :param: URLString The URL string.
+        :param: stream The stream to upload.
+        
+        :returns: The created upload request.
+    */
+    public func upload(method: Method, _ URLString: URLStringConvertible, stream: NSInputStream) -> Request {
+        return upload(URLRequest(method, URLString), stream: stream)
+    }
+}
+
+extension Request {
+    class UploadTaskDelegate: DataTaskDelegate {
+        var uploadTask: NSURLSessionUploadTask! { return task as! NSURLSessionUploadTask }
+        var uploadProgress: ((Int64, Int64, Int64) -> Void)!
+        
+        // MARK: NSURLSessionTaskDelegate
+        
+        func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
+            progress.totalUnitCount = totalBytesExpectedToSend
+            progress.completedUnitCount = totalBytesSent
+            
+            uploadProgress?(bytesSent, totalBytesSent, totalBytesExpectedToSend)
+        }
+    }
+}