Alamofire.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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) -> NSMutableURLRequest {
  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: MultipartFormData
  163. /**
  164. Creates an upload request using the shared manager instance for the specified method and URL string.
  165. :param: method The HTTP method.
  166. :param: URLString The URL string.
  167. :param: multipartFormData The closure used to append body parts to the `MultipartFormData`.
  168. :param: encodingMemoryThreshold The encoding memory threshold in bytes. `MultipartFormDataEncodingMemoryThreshold`
  169. by default.
  170. :param: encodingCompletion The closure called when the `MultipartFormData` encoding is complete.
  171. */
  172. public func upload(
  173. method: Method,
  174. #URLString: URLStringConvertible,
  175. #multipartFormData: MultipartFormData -> Void,
  176. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  177. #encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  178. {
  179. return Manager.sharedInstance.upload(
  180. method,
  181. URLString,
  182. multipartFormData: multipartFormData,
  183. encodingMemoryThreshold: encodingMemoryThreshold,
  184. encodingCompletion: encodingCompletion
  185. )
  186. }
  187. /**
  188. Creates an upload request using the shared manager instance for the specified method and URL string.
  189. :param: URLRequest The URL request.
  190. :param: multipartFormData The closure used to append body parts to the `MultipartFormData`.
  191. :param: encodingMemoryThreshold The encoding memory threshold in bytes. `MultipartFormDataEncodingMemoryThreshold`
  192. by default.
  193. :param: encodingCompletion The closure called when the `MultipartFormData` encoding is complete.
  194. */
  195. public func upload(
  196. URLRequest: URLRequestConvertible,
  197. #multipartFormData: MultipartFormData -> Void,
  198. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  199. #encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  200. {
  201. return Manager.sharedInstance.upload(
  202. URLRequest,
  203. multipartFormData: multipartFormData,
  204. encodingMemoryThreshold: encodingMemoryThreshold,
  205. encodingCompletion: encodingCompletion
  206. )
  207. }
  208. // MARK: - Download Methods
  209. // MARK: URL Request
  210. /**
  211. Creates a download request using the shared manager instance for the specified method and URL string.
  212. :param: method The HTTP method.
  213. :param: URLString The URL string.
  214. :param: destination The closure used to determine the destination of the downloaded file.
  215. :returns: The created download request.
  216. */
  217. public func download(method: Method, URLString: URLStringConvertible, destination: Request.DownloadFileDestination) -> Request {
  218. return Manager.sharedInstance.download(method, URLString, destination: destination)
  219. }
  220. /**
  221. Creates a download request using the shared manager instance for the specified URL request.
  222. :param: URLRequest The URL request.
  223. :param: destination The closure used to determine the destination of the downloaded file.
  224. :returns: The created download request.
  225. */
  226. public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request {
  227. return Manager.sharedInstance.download(URLRequest, destination: destination)
  228. }
  229. // MARK: Resume Data
  230. /**
  231. Creates a request using the shared manager instance for downloading from the resume data produced from a previous request cancellation.
  232. :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.
  233. :param: destination The closure used to determine the destination of the downloaded file.
  234. :returns: The created download request.
  235. */
  236. public func download(resumeData data: NSData, destination: Request.DownloadFileDestination) -> Request {
  237. return Manager.sharedInstance.download(data, destination: destination)
  238. }