Alamofire.swift 12 KB

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