Alamofire.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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
  30. construct URL requests.
  31. */
  32. public protocol URLStringConvertible {
  33. /**
  34. A URL that conforms to RFC 2396.
  35. Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
  36. See https://tools.ietf.org/html/rfc2396
  37. See https://tools.ietf.org/html/rfc1738
  38. See https://tools.ietf.org/html/rfc1808
  39. */
  40. var URLString: String { get }
  41. }
  42. extension String: URLStringConvertible {
  43. public var URLString: String {
  44. return self
  45. }
  46. }
  47. extension NSURL: URLStringConvertible {
  48. public var URLString: String {
  49. return absoluteString
  50. }
  51. }
  52. extension NSURLComponents: URLStringConvertible {
  53. public var URLString: String {
  54. return URL!.URLString
  55. }
  56. }
  57. extension NSURLRequest: URLStringConvertible {
  58. public var URLString: String {
  59. return URL!.URLString
  60. }
  61. }
  62. // MARK: - URLRequestConvertible
  63. /**
  64. Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
  65. */
  66. public protocol URLRequestConvertible {
  67. /// The URL request.
  68. var URLRequest: NSMutableURLRequest { get }
  69. }
  70. extension NSURLRequest: URLRequestConvertible {
  71. public var URLRequest: NSMutableURLRequest {
  72. return self.mutableCopy() as! NSMutableURLRequest
  73. }
  74. }
  75. // MARK: - Convenience
  76. func URLRequest(
  77. method: Method,
  78. _ URLString: URLStringConvertible,
  79. headers: [String: String]? = nil)
  80. -> NSMutableURLRequest
  81. {
  82. let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URLString.URLString)!)
  83. mutableURLRequest.HTTPMethod = method.rawValue
  84. if let headers = headers {
  85. for (headerField, headerValue) in headers {
  86. mutableURLRequest.setValue(headerValue, forHTTPHeaderField: headerField)
  87. }
  88. }
  89. return mutableURLRequest
  90. }
  91. // MARK: - Request Methods
  92. /**
  93. Creates a request using the shared manager instance for the specified method, URL string, parameters, and
  94. parameter encoding.
  95. - parameter method: The HTTP method.
  96. - parameter URLString: The URL string.
  97. - parameter parameters: The parameters. `nil` by default.
  98. - parameter encoding: The parameter encoding. `.URL` by default.
  99. - parameter headers: The HTTP headers. `nil` by default.
  100. - returns: The created request.
  101. */
  102. public func request(
  103. method: Method,
  104. _ URLString: URLStringConvertible,
  105. parameters: [String: AnyObject]? = nil,
  106. encoding: ParameterEncoding = .URL,
  107. headers: [String: String]? = nil)
  108. -> Request
  109. {
  110. return Manager.sharedInstance.request(
  111. method,
  112. URLString,
  113. parameters: parameters,
  114. encoding: encoding,
  115. headers: headers
  116. )
  117. }
  118. /**
  119. Creates a request using the shared manager instance for the specified URL request.
  120. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  121. - parameter URLRequest: The URL request
  122. - returns: The created request.
  123. */
  124. public func request(URLRequest: URLRequestConvertible) -> Request {
  125. return Manager.sharedInstance.request(URLRequest.URLRequest)
  126. }
  127. // MARK: - Upload Methods
  128. // MARK: File
  129. /**
  130. Creates an upload request using the shared manager instance for the specified method, URL string, and file.
  131. - parameter method: The HTTP method.
  132. - parameter URLString: The URL string.
  133. - parameter headers: The HTTP headers. `nil` by default.
  134. - parameter file: The file to upload.
  135. - returns: The created upload request.
  136. */
  137. public func upload(
  138. method: Method,
  139. _ URLString: URLStringConvertible,
  140. headers: [String: String]? = nil,
  141. file: NSURL)
  142. -> Request
  143. {
  144. return Manager.sharedInstance.upload(method, URLString, headers: headers, file: file)
  145. }
  146. /**
  147. Creates an upload request using the shared manager instance for the specified URL request and file.
  148. - parameter URLRequest: The URL request.
  149. - parameter file: The file to upload.
  150. - returns: The created upload request.
  151. */
  152. public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
  153. return Manager.sharedInstance.upload(URLRequest, file: file)
  154. }
  155. // MARK: Data
  156. /**
  157. Creates an upload request using the shared manager instance for the specified method, URL string, and data.
  158. - parameter method: The HTTP method.
  159. - parameter URLString: The URL string.
  160. - parameter headers: The HTTP headers. `nil` by default.
  161. - parameter data: The data to upload.
  162. - returns: The created upload request.
  163. */
  164. public func upload(
  165. method: Method,
  166. _ URLString: URLStringConvertible,
  167. headers: [String: String]? = nil,
  168. data: NSData)
  169. -> Request
  170. {
  171. return Manager.sharedInstance.upload(method, URLString, headers: headers, data: data)
  172. }
  173. /**
  174. Creates an upload request using the shared manager instance for the specified URL request and data.
  175. - parameter URLRequest: The URL request.
  176. - parameter data: The data to upload.
  177. - returns: The created upload request.
  178. */
  179. public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
  180. return Manager.sharedInstance.upload(URLRequest, data: data)
  181. }
  182. // MARK: Stream
  183. /**
  184. Creates an upload request using the shared manager instance for the specified method, URL string, and stream.
  185. - parameter method: The HTTP method.
  186. - parameter URLString: The URL string.
  187. - parameter headers: The HTTP headers. `nil` by default.
  188. - parameter stream: The stream to upload.
  189. - returns: The created upload request.
  190. */
  191. public func upload(
  192. method: Method,
  193. _ URLString: URLStringConvertible,
  194. headers: [String: String]? = nil,
  195. stream: NSInputStream)
  196. -> Request
  197. {
  198. return Manager.sharedInstance.upload(method, URLString, headers: headers, stream: stream)
  199. }
  200. /**
  201. Creates an upload request using the shared manager instance for the specified URL request and stream.
  202. - parameter URLRequest: The URL request.
  203. - parameter stream: The stream to upload.
  204. - returns: The created upload request.
  205. */
  206. public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
  207. return Manager.sharedInstance.upload(URLRequest, stream: stream)
  208. }
  209. // MARK: MultipartFormData
  210. /**
  211. Creates an upload request using the shared manager instance for the specified method and URL string.
  212. - parameter method: The HTTP method.
  213. - parameter URLString: The URL string.
  214. - parameter headers: The HTTP headers. `nil` by default.
  215. - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  216. - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
  217. `MultipartFormDataEncodingMemoryThreshold` by default.
  218. - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
  219. */
  220. public func upload(
  221. method: Method,
  222. _ URLString: URLStringConvertible,
  223. headers: [String: String]? = nil,
  224. multipartFormData: MultipartFormData -> Void,
  225. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  226. encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  227. {
  228. return Manager.sharedInstance.upload(
  229. method,
  230. URLString,
  231. headers: headers,
  232. multipartFormData: multipartFormData,
  233. encodingMemoryThreshold: encodingMemoryThreshold,
  234. encodingCompletion: encodingCompletion
  235. )
  236. }
  237. /**
  238. Creates an upload request using the shared manager instance for the specified method and URL string.
  239. - parameter URLRequest: The URL request.
  240. - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  241. - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
  242. `MultipartFormDataEncodingMemoryThreshold` by default.
  243. - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
  244. */
  245. public func upload(
  246. URLRequest: URLRequestConvertible,
  247. multipartFormData: MultipartFormData -> Void,
  248. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  249. encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  250. {
  251. return Manager.sharedInstance.upload(
  252. URLRequest,
  253. multipartFormData: multipartFormData,
  254. encodingMemoryThreshold: encodingMemoryThreshold,
  255. encodingCompletion: encodingCompletion
  256. )
  257. }
  258. // MARK: - Download Methods
  259. // MARK: URL Request
  260. /**
  261. Creates a download request using the shared manager instance for the specified method and URL string.
  262. - parameter method: The HTTP method.
  263. - parameter URLString: The URL string.
  264. - parameter headers: The HTTP headers. `nil` by default.
  265. - parameter destination: The closure used to determine the destination of the downloaded file.
  266. - returns: The created download request.
  267. */
  268. public func download(
  269. method: Method,
  270. _ URLString: URLStringConvertible,
  271. headers: [String: String]? = nil,
  272. destination: Request.DownloadFileDestination)
  273. -> Request
  274. {
  275. return Manager.sharedInstance.download(method, URLString, headers: headers, destination: destination)
  276. }
  277. /**
  278. Creates a download request using the shared manager instance for the specified URL request.
  279. - parameter URLRequest: The URL request.
  280. - parameter destination: The closure used to determine the destination of the downloaded file.
  281. - returns: The created download request.
  282. */
  283. public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request {
  284. return Manager.sharedInstance.download(URLRequest, destination: destination)
  285. }
  286. // MARK: Resume Data
  287. /**
  288. Creates a request using the shared manager instance for downloading from the resume data produced from a
  289. previous request cancellation.
  290. - parameter resumeData: The resume data. This is an opaque data blob produced by `NSURLSessionDownloadTask`
  291. when a task is cancelled. See `NSURLSession -downloadTaskWithResumeData:` for additional
  292. information.
  293. - parameter destination: The closure used to determine the destination of the downloaded file.
  294. - returns: The created download request.
  295. */
  296. public func download(resumeData data: NSData, destination: Request.DownloadFileDestination) -> Request {
  297. return Manager.sharedInstance.download(data, destination: destination)
  298. }