Alamofire.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Alamofire.swift
  2. //
  3. // Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. /// Alamofire errors
  24. public let AlamofireErrorDomain = "com.alamofire.error"
  25. public let AlamofireInputStreamReadFailed = -6000
  26. public let AlamofireOutputStreamWriteFailed = -6001
  27. // MARK: - URLStringConvertible
  28. /**
  29. Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to construct URL requests.
  30. */
  31. public protocol URLStringConvertible {
  32. /**
  33. A URL that conforms to RFC 2396.
  34. Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
  35. See http://tools.ietf.org/html/rfc2396
  36. See http://tools.ietf.org/html/rfc1738
  37. See http://tools.ietf.org/html/rfc1808
  38. */
  39. var URLString: String { get }
  40. }
  41. extension String: URLStringConvertible {
  42. public var URLString: String {
  43. return self
  44. }
  45. }
  46. extension NSURL: URLStringConvertible {
  47. public var URLString: String {
  48. return absoluteString!
  49. }
  50. }
  51. extension NSURLComponents: URLStringConvertible {
  52. public var URLString: String {
  53. return URL!.URLString
  54. }
  55. }
  56. extension NSURLRequest: URLStringConvertible {
  57. public var URLString: String {
  58. return URL!.URLString
  59. }
  60. }
  61. // MARK: - URLRequestConvertible
  62. /**
  63. Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
  64. */
  65. public protocol URLRequestConvertible {
  66. /// The URL request.
  67. var URLRequest: NSURLRequest { get }
  68. }
  69. extension NSURLRequest: URLRequestConvertible {
  70. public var URLRequest: NSURLRequest {
  71. return self
  72. }
  73. }
  74. // MARK: - Convenience
  75. func URLRequest(method: Method, URL: URLStringConvertible) -> NSURLRequest {
  76. let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URL.URLString)!)
  77. mutableURLRequest.HTTPMethod = method.rawValue
  78. return mutableURLRequest
  79. }
  80. // MARK: - Request Methods
  81. /**
  82. Creates a request using the shared manager instance for the specified method, URL string, parameters, and parameter encoding.
  83. :param: method The HTTP method.
  84. :param: URLString The URL string.
  85. :param: parameters The parameters. `nil` by default.
  86. :param: encoding The parameter encoding. `.URL` by default.
  87. :returns: The created request.
  88. */
  89. public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request {
  90. return Manager.sharedInstance.request(method, URLString, parameters: parameters, encoding: encoding)
  91. }
  92. /**
  93. Creates a request using the shared manager instance for the specified URL request.
  94. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  95. :param: URLRequest The URL request
  96. :returns: The created request.
  97. */
  98. public func request(URLRequest: URLRequestConvertible) -> Request {
  99. return Manager.sharedInstance.request(URLRequest.URLRequest)
  100. }
  101. // MARK: - Upload Methods
  102. // MARK: File
  103. /**
  104. Creates an upload request using the shared manager instance for the specified method, URL string, and file.
  105. :param: method The HTTP method.
  106. :param: URLString The URL string.
  107. :param: file The file to upload.
  108. :returns: The created upload request.
  109. */
  110. public func upload(method: Method, URLString: URLStringConvertible, file: NSURL) -> Request {
  111. return Manager.sharedInstance.upload(method, URLString, file: file)
  112. }
  113. /**
  114. Creates an upload request using the shared manager instance for the specified URL request and file.
  115. :param: URLRequest The URL request.
  116. :param: file The file to upload.
  117. :returns: The created upload request.
  118. */
  119. public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
  120. return Manager.sharedInstance.upload(URLRequest, file: file)
  121. }
  122. // MARK: Data
  123. /**
  124. Creates an upload request using the shared manager instance for the specified method, URL string, and data.
  125. :param: method The HTTP method.
  126. :param: URLString The URL string.
  127. :param: data The data to upload.
  128. :returns: The created upload request.
  129. */
  130. public func upload(method: Method, URLString: URLStringConvertible, data: NSData) -> Request {
  131. return Manager.sharedInstance.upload(method, URLString, data: data)
  132. }
  133. /**
  134. Creates an upload request using the shared manager instance for the specified URL request and data.
  135. :param: URLRequest The URL request.
  136. :param: data The data to upload.
  137. :returns: The created upload request.
  138. */
  139. public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
  140. return Manager.sharedInstance.upload(URLRequest, data: data)
  141. }
  142. // MARK: Stream
  143. /**
  144. Creates an upload request using the shared manager instance for the specified method, URL string, and stream.
  145. :param: method The HTTP method.
  146. :param: URLString The URL string.
  147. :param: stream The stream to upload.
  148. :returns: The created upload request.
  149. */
  150. public func upload(method: Method, URLString: URLStringConvertible, stream: NSInputStream) -> Request {
  151. return Manager.sharedInstance.upload(method, URLString, stream: stream)
  152. }
  153. /**
  154. Creates an upload request using the shared manager instance for the specified URL request and stream.
  155. :param: URLRequest The URL request.
  156. :param: stream The stream to upload.
  157. :returns: The created upload request.
  158. */
  159. public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
  160. return Manager.sharedInstance.upload(URLRequest, stream: stream)
  161. }
  162. // MARK: - Download Methods
  163. // MARK: URL Request
  164. /**
  165. Creates a download request using the shared manager instance for the specified method and URL string.
  166. :param: method The HTTP method.
  167. :param: URLString The URL string.
  168. :param: destination The closure used to determine the destination of the downloaded file.
  169. :returns: The created download request.
  170. */
  171. public func download(method: Method, URLString: URLStringConvertible, destination: Request.DownloadFileDestination) -> Request {
  172. return Manager.sharedInstance.download(method, URLString, destination: destination)
  173. }
  174. /**
  175. Creates a download request using the shared manager instance for the specified URL request.
  176. :param: URLRequest The URL request.
  177. :param: destination The closure used to determine the destination of the downloaded file.
  178. :returns: The created download request.
  179. */
  180. public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request {
  181. return Manager.sharedInstance.download(URLRequest, destination: destination)
  182. }
  183. // MARK: Resume Data
  184. /**
  185. Creates a request using the shared manager instance for downloading from the resume data produced from a previous request cancellation.
  186. :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.
  187. :param: destination The closure used to determine the destination of the downloaded file.
  188. :returns: The created download request.
  189. */
  190. public func download(resumeData data: NSData, destination: Request.DownloadFileDestination) -> Request {
  191. return Manager.sharedInstance.download(data, destination: destination)
  192. }