| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- // 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
- /**
- 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
- // 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
- // 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)
- }
|