Quellcode durchsuchen

Extended request/download/upload methods to support custom headers.

Christian Noon vor 10 Jahren
Ursprung
Commit
84330a9295
4 geänderte Dateien mit 64 neuen und 30 gelöschten Zeilen
  1. 26 12
      Source/Alamofire.swift
  2. 4 2
      Source/Download.swift
  3. 12 2
      Source/Manager.swift
  4. 22 14
      Source/Upload.swift

+ 26 - 12
Source/Alamofire.swift

@@ -87,10 +87,16 @@ extension NSURLRequest: URLRequestConvertible {
 
 // MARK: - Convenience
 
-func URLRequest(method: Method, URL: URLStringConvertible) -> NSMutableURLRequest {
-    let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URL.URLString)!)
+func URLRequest(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil) -> NSMutableURLRequest {
+    let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URLString.URLString)!)
     mutableURLRequest.HTTPMethod = method.rawValue
 
+    if let headers = headers {
+        for (headerField, headerValue) in headers {
+            mutableURLRequest.setValue(headerValue, forHTTPHeaderField: headerField)
+        }
+    }
+
     return mutableURLRequest
 }
 
@@ -103,11 +109,12 @@ func URLRequest(method: Method, URL: URLStringConvertible) -> NSMutableURLReques
     :param: URLString The URL string.
     :param: parameters The parameters. `nil` by default.
     :param: encoding The parameter encoding. `.URL` by default.
+    :param: headers The HTTP headers. `nil` by default.
 
     :returns: The created request.
 */
-public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request {
-    return Manager.sharedInstance.request(method, URLString, parameters: parameters, encoding: encoding)
+public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL, headers: [String: String]? = nil) -> Request {
+    return Manager.sharedInstance.request(method, URLString, parameters: parameters, encoding: encoding, headers: headers)
 }
 
 /**
@@ -132,12 +139,13 @@ public func request(URLRequest: URLRequestConvertible) -> Request {
 
     :param: method The HTTP method.
     :param: URLString The URL string.
+    :param: headers The HTTP headers. `nil` by default.
     :param: file The file to upload.
 
     :returns: The created upload request.
 */
-public func upload(method: Method, URLString: URLStringConvertible, file: NSURL) -> Request {
-    return Manager.sharedInstance.upload(method, URLString, file: file)
+public func upload(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil, file: NSURL) -> Request {
+    return Manager.sharedInstance.upload(method, URLString, headers: headers, file: file)
 }
 
 /**
@@ -159,12 +167,13 @@ public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
 
     :param: method The HTTP method.
     :param: URLString The URL string.
+    :param: headers The HTTP headers. `nil` by default.
     :param: data The data to upload.
 
     :returns: The created upload request.
 */
-public func upload(method: Method, URLString: URLStringConvertible, data: NSData) -> Request {
-    return Manager.sharedInstance.upload(method, URLString, data: data)
+public func upload(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil, data: NSData) -> Request {
+    return Manager.sharedInstance.upload(method, URLString, headers: headers, data: data)
 }
 
 /**
@@ -186,12 +195,13 @@ public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
 
     :param: method The HTTP method.
     :param: URLString The URL string.
+    :param: headers The HTTP headers. `nil` by default.
     :param: stream The stream to upload.
 
     :returns: The created upload request.
 */
-public func upload(method: Method, URLString: URLStringConvertible, stream: NSInputStream) -> Request {
-    return Manager.sharedInstance.upload(method, URLString, stream: stream)
+public func upload(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil, stream: NSInputStream) -> Request {
+    return Manager.sharedInstance.upload(method, URLString, headers: headers, stream: stream)
 }
 
 /**
@@ -213,6 +223,7 @@ public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) ->
 
     :param: method                  The HTTP method.
     :param: URLString               The URL string.
+    :param: headers The HTTP headers. `nil` by default.
     :param: multipartFormData       The closure used to append body parts to the `MultipartFormData`.
     :param: encodingMemoryThreshold The encoding memory threshold in bytes. `MultipartFormDataEncodingMemoryThreshold` 
                                     by default.
@@ -221,6 +232,7 @@ public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) ->
 public func upload(
     method: Method,
     #URLString: URLStringConvertible,
+    headers: [String: String]? = nil,
     #multipartFormData: MultipartFormData -> Void,
     encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
     #encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
@@ -228,6 +240,7 @@ public func upload(
     return Manager.sharedInstance.upload(
         method,
         URLString,
+        headers: headers,
         multipartFormData: multipartFormData,
         encodingMemoryThreshold: encodingMemoryThreshold,
         encodingCompletion: encodingCompletion
@@ -266,12 +279,13 @@ public func upload(
 
     :param: method The HTTP method.
     :param: URLString The URL string.
+    :param: headers The HTTP headers. `nil` by default.
     :param: destination The closure used to determine the destination of the downloaded file.
 
     :returns: The created download request.
 */
-public func download(method: Method, URLString: URLStringConvertible, destination: Request.DownloadFileDestination) -> Request {
-    return Manager.sharedInstance.download(method, URLString, destination: destination)
+public func download(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil, destination: Request.DownloadFileDestination) -> Request {
+    return Manager.sharedInstance.download(method, URLString, headers: headers, destination: destination)
 }
 
 /**

+ 4 - 2
Source/Download.swift

@@ -64,12 +64,14 @@ extension Manager {
 
         :param: method The HTTP method.
         :param: URLString The URL string.
+        :param: headers The HTTP headers. `nil` by default.
         :param: destination The closure used to determine the destination of the downloaded file.
 
         :returns: The created download request.
     */
-    public func download(method: Method, _ URLString: URLStringConvertible, destination: Request.DownloadFileDestination) -> Request {
-        return download(URLRequest(method, URLString), destination: destination)
+    public func download(method: Method, _ URLString: URLStringConvertible, headers: [String: String]? = nil, destination: Request.DownloadFileDestination) -> Request {
+        let mutableURLRequest = URLRequest(method, URLString, headers: headers)
+        return download(mutableURLRequest, destination: destination)
     }
 
     /**

+ 12 - 2
Source/Manager.swift

@@ -130,11 +130,21 @@ public class Manager {
         :param: URLString The URL string.
         :param: parameters The parameters. `nil` by default.
         :param: encoding The parameter encoding. `.URL` by default.
+        :param: headers The HTTP headers. `nil` by default.
 
         :returns: The created request.
     */
-    public func request(method: Method, _ URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request {
-        return request(encoding.encode(URLRequest(method, URLString), parameters: parameters).0)
+    public func request(
+        method: Method,
+        _ URLString: URLStringConvertible,
+        parameters: [String: AnyObject]? = nil,
+        encoding: ParameterEncoding = .URL,
+        headers: [String: String]? = nil)
+        -> Request
+    {
+        let mutableURLRequest = URLRequest(method, URLString, headers: headers)
+        let encodedURLRequest = encoding.encode(mutableURLRequest, parameters: parameters).0
+        return request(encodedURLRequest)
     }
 
     /**

+ 22 - 14
Source/Upload.swift

@@ -87,12 +87,14 @@ extension Manager {
 
         :param: method The HTTP method.
         :param: URLString The URL string.
-        :param: file The file to upload
+        :param: headers The HTTP headers. `nil` by default.
+        :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)
+    public func upload(method: Method, _ URLString: URLStringConvertible, headers: [String: String]? = nil, file: NSURL) -> Request {
+        let mutableURLRequest = URLRequest(method, URLString, headers: headers)
+        return upload(mutableURLRequest, file: file)
     }
 
     // MARK: Data
@@ -102,8 +104,8 @@ extension Manager {
 
         If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
 
-        :param: URLRequest The URL request
-        :param: data The data to upload
+        :param: URLRequest The URL request.
+        :param: data The data to upload.
 
         :returns: The created upload request.
     */
@@ -118,12 +120,14 @@ extension Manager {
 
         :param: method The HTTP method.
         :param: URLString The URL string.
-        :param: data The data to upload
+        :param: headers The HTTP headers. `nil` by default.
+        :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)
+    public func upload(method: Method, _ URLString: URLStringConvertible, headers: [String: String]? = nil, data: NSData) -> Request {
+        let mutableURLRequest = URLRequest(method, URLString, headers: headers)
+        return upload(mutableURLRequest, data: data)
     }
 
     // MARK: Stream
@@ -133,8 +137,8 @@ extension Manager {
 
         If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
 
-        :param: URLRequest The URL request
-        :param: stream The stream to upload
+        :param: URLRequest The URL request.
+        :param: stream The stream to upload.
 
         :returns: The created upload request.
     */
@@ -149,12 +153,14 @@ extension Manager {
 
         :param: method The HTTP method.
         :param: URLString The URL string.
+        :param: headers The HTTP headers. `nil` by default.
         :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)
+    public func upload(method: Method, _ URLString: URLStringConvertible, headers: [String: String]? = nil, stream: NSInputStream) -> Request {
+        let mutableURLRequest = URLRequest(method, URLString, headers: headers)
+        return upload(mutableURLRequest, stream: stream)
     }
 
     // MARK: MultipartFormData
@@ -196,6 +202,7 @@ extension Manager {
 
         :param: method                  The HTTP method.
         :param: URLString               The URL string.
+        :param: headers                 The HTTP headers. `nil` by default.
         :param: multipartFormData       The closure used to append body parts to the `MultipartFormData`.
         :param: encodingMemoryThreshold The encoding memory threshold in bytes. `MultipartFormDataEncodingMemoryThreshold`
                                         by default.
@@ -204,14 +211,15 @@ extension Manager {
     public func upload(
         method: Method,
         _ URLString: URLStringConvertible,
+        headers: [String: String]? = nil,
         multipartFormData: MultipartFormData -> Void,
         encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
         encodingCompletion: (MultipartFormDataEncodingResult -> Void)?)
     {
-        let urlRequest = URLRequest(method, URLString)
+        let mutableURLRequest = URLRequest(method, URLString, headers: headers)
 
         return upload(
-            urlRequest,
+            mutableURLRequest,
             multipartFormData: multipartFormData,
             encodingMemoryThreshold: encodingMemoryThreshold,
             encodingCompletion: encodingCompletion