// 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 /// Alamofire errors public let AlamofireErrorDomain = "com.alamofire.error" // MARK: - URLStringConvertible /** Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to construct URL requests. */ public protocol URLStringConvertible { /** A URL that conforms to RFC 2396. Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808. See http://tools.ietf.org/html/rfc2396 See http://tools.ietf.org/html/rfc1738 See http://tools.ietf.org/html/rfc1808 */ var URLString: String { get } } extension String: URLStringConvertible { public var URLString: String { return self } } extension NSURL: URLStringConvertible { public var URLString: String { return absoluteString! } } extension NSURLComponents: URLStringConvertible { public var URLString: String { return URL!.URLString } } extension NSURLRequest: URLStringConvertible { public var URLString: String { return URL!.URLString } } // MARK: - URLRequestConvertible /** Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests. */ public protocol URLRequestConvertible { /// The URL request. var URLRequest: NSURLRequest { get } } extension NSURLRequest: URLRequestConvertible { public var URLRequest: NSURLRequest { return self } } // MARK: - Convenience func URLRequest(method: Method, URL: URLStringConvertible) -> NSURLRequest { let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URL.URLString)!) mutableURLRequest.HTTPMethod = method.rawValue return mutableURLRequest } // MARK: - Request Methods /** Creates a request using the shared manager instance for the specified method, URL string, parameters, and parameter encoding. :param: method The HTTP method. :param: URLString The URL string. :param: parameters The parameters. `nil` by default. :param: encoding The parameter encoding. `.URL` 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) } /** Creates a request using the shared manager instance for the specified URL request. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned. :param: URLRequest The URL request :returns: The created request. */ public func request(URLRequest: URLRequestConvertible) -> Request { return Manager.sharedInstance.request(URLRequest.URLRequest) } // MARK: - Upload Methods // MARK: File /** Creates an upload request using the shared manager instance for the specified method, URL string, and file. :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 Manager.sharedInstance.upload(method, URLString, file: file) } /** Creates an upload request using the shared manager instance for the specified URL request and file. :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 Manager.sharedInstance.upload(URLRequest, file: file) } // MARK: Data /** Creates an upload request using the shared manager instance for the specified method, URL string, and data. :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 Manager.sharedInstance.upload(method, URLString, data: data) } /** Creates an upload request using the shared manager instance for the specified URL request and data. :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 Manager.sharedInstance.upload(URLRequest, data: data) } // MARK: Stream /** Creates an upload request using the shared manager instance for the specified method, URL string, and stream. :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 Manager.sharedInstance.upload(method, URLString, stream: stream) } /** Creates an upload request using the shared manager instance for the specified URL request and stream. :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 Manager.sharedInstance.upload(URLRequest, stream: stream) } // MARK: - Download Methods // MARK: URL Request /** Creates a download request using the shared manager instance for the specified method and URL string. :param: method The HTTP method. :param: URLString The URL string. :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) } /** Creates a download request using the shared manager instance for the specified URL request. :param: URLRequest The URL request. :param: destination The closure used to determine the destination of the downloaded file. :returns: The created download request. */ public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request { return Manager.sharedInstance.download(URLRequest, destination: destination) } // MARK: Resume Data /** Creates a request using the shared manager instance for downloading from the resume data produced from a previous request cancellation. :param: resumeData The resume data. This is an opaque data blob produced by `NSURLSessionDownloadTask` when a task is cancelled. See `NSURLSession -downloadTaskWithResumeData:` for additional information. :param: destination The closure used to determine the destination of the downloaded file. :returns: The created download request. */ public func download(resumeData data: NSData, destination: Request.DownloadFileDestination) -> Request { return Manager.sharedInstance.download(data, destination: destination) }