Alamofire.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //
  2. // Alamofire.swift
  3. //
  4. // Copyright (c) 2014-2016 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. // MARK: - URLStringConvertible
  26. /**
  27. Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to
  28. construct URL requests.
  29. */
  30. public protocol URLStringConvertible {
  31. /**
  32. A URL that conforms to RFC 2396.
  33. Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
  34. See https://tools.ietf.org/html/rfc2396
  35. See https://tools.ietf.org/html/rfc1738
  36. See https://tools.ietf.org/html/rfc1808
  37. */
  38. var URLString: String { get }
  39. }
  40. extension String: URLStringConvertible {
  41. public var URLString: String {
  42. return self
  43. }
  44. }
  45. extension NSURL: URLStringConvertible {
  46. public var URLString: String {
  47. return absoluteString
  48. }
  49. }
  50. extension NSURLComponents: URLStringConvertible {
  51. public var URLString: String {
  52. return URL!.URLString
  53. }
  54. }
  55. extension NSURLRequest: URLStringConvertible {
  56. public var URLString: String {
  57. return URL!.URLString
  58. }
  59. }
  60. // MARK: - URLRequestConvertible
  61. /**
  62. Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
  63. */
  64. public protocol URLRequestConvertible {
  65. /// The URL request.
  66. var URLRequest: NSMutableURLRequest { get }
  67. }
  68. extension NSURLRequest: URLRequestConvertible {
  69. public var URLRequest: NSMutableURLRequest {
  70. return self.mutableCopy() as! NSMutableURLRequest
  71. }
  72. }
  73. // MARK: - Convenience
  74. func URLRequest(
  75. method: Method,
  76. _ URLString: URLStringConvertible,
  77. headers: [String: String]? = nil)
  78. -> NSMutableURLRequest
  79. {
  80. let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URLString.URLString)!)
  81. mutableURLRequest.HTTPMethod = method.rawValue
  82. if let headers = headers {
  83. for (headerField, headerValue) in headers {
  84. mutableURLRequest.setValue(headerValue, forHTTPHeaderField: headerField)
  85. }
  86. }
  87. return mutableURLRequest
  88. }
  89. // MARK: - Request Methods
  90. /**
  91. Creates a request using the shared manager instance for the specified method, URL string, parameters, and
  92. parameter encoding.
  93. - parameter method: The HTTP method.
  94. - parameter URLString: The URL string.
  95. - parameter parameters: The parameters. `nil` by default.
  96. - parameter encoding: The parameter encoding. `.URL` by default.
  97. - parameter headers: The HTTP headers. `nil` by default.
  98. - returns: The created request.
  99. */
  100. public func request(
  101. method: Method,
  102. _ URLString: URLStringConvertible,
  103. parameters: [String: AnyObject]? = nil,
  104. encoding: ParameterEncoding = .URL,
  105. headers: [String: String]? = nil)
  106. -> Request
  107. {
  108. return Manager.sharedInstance.request(
  109. method,
  110. URLString,
  111. parameters: parameters,
  112. encoding: encoding,
  113. headers: headers
  114. )
  115. }
  116. /**
  117. Creates a request using the shared manager instance for the specified URL request.
  118. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  119. - parameter URLRequest: The URL request
  120. - returns: The created request.
  121. */
  122. public func request(URLRequest: URLRequestConvertible) -> Request {
  123. return Manager.sharedInstance.request(URLRequest.URLRequest)
  124. }
  125. // MARK: - Upload Methods
  126. // MARK: File
  127. /**
  128. Creates an upload request using the shared manager instance for the specified method, URL string, and file.
  129. - parameter method: The HTTP method.
  130. - parameter URLString: The URL string.
  131. - parameter headers: The HTTP headers. `nil` by default.
  132. - parameter file: The file to upload.
  133. - returns: The created upload request.
  134. */
  135. public func upload(
  136. method: Method,
  137. _ URLString: URLStringConvertible,
  138. headers: [String: String]? = nil,
  139. file: NSURL)
  140. -> Request
  141. {
  142. return Manager.sharedInstance.upload(method, URLString, headers: headers, file: file)
  143. }
  144. /**
  145. Creates an upload request using the shared manager instance for the specified URL request and file.
  146. - parameter URLRequest: The URL request.
  147. - parameter file: The file to upload.
  148. - returns: The created upload request.
  149. */
  150. public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
  151. return Manager.sharedInstance.upload(URLRequest, file: file)
  152. }
  153. // MARK: Data
  154. /**
  155. Creates an upload request using the shared manager instance for the specified method, URL string, and data.
  156. - parameter method: The HTTP method.
  157. - parameter URLString: The URL string.
  158. - parameter headers: The HTTP headers. `nil` by default.
  159. - parameter data: The data to upload.
  160. - returns: The created upload request.
  161. */
  162. public func upload(
  163. method: Method,
  164. _ URLString: URLStringConvertible,
  165. headers: [String: String]? = nil,
  166. data: NSData)
  167. -> Request
  168. {
  169. return Manager.sharedInstance.upload(method, URLString, headers: headers, data: data)
  170. }
  171. /**
  172. Creates an upload request using the shared manager instance for the specified URL request and data.
  173. - parameter URLRequest: The URL request.
  174. - parameter data: The data to upload.
  175. - returns: The created upload request.
  176. */
  177. public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
  178. return Manager.sharedInstance.upload(URLRequest, data: data)
  179. }
  180. // MARK: Stream
  181. /**
  182. Creates an upload request using the shared manager instance for the specified method, URL string, and stream.
  183. - parameter method: The HTTP method.
  184. - parameter URLString: The URL string.
  185. - parameter headers: The HTTP headers. `nil` by default.
  186. - parameter stream: The stream to upload.
  187. - returns: The created upload request.
  188. */
  189. public func upload(
  190. method: Method,
  191. _ URLString: URLStringConvertible,
  192. headers: [String: String]? = nil,
  193. stream: NSInputStream)
  194. -> Request
  195. {
  196. return Manager.sharedInstance.upload(method, URLString, headers: headers, stream: stream)
  197. }
  198. /**
  199. Creates an upload request using the shared manager instance for the specified URL request and stream.
  200. - parameter URLRequest: The URL request.
  201. - parameter stream: The stream to upload.
  202. - returns: The created upload request.
  203. */
  204. public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
  205. return Manager.sharedInstance.upload(URLRequest, stream: stream)
  206. }
  207. // MARK: MultipartFormData
  208. /**
  209. Creates an upload request using the shared manager instance for the specified method and URL string.
  210. - parameter method: The HTTP method.
  211. - parameter URLString: The URL string.
  212. - parameter headers: The HTTP headers. `nil` by default.
  213. - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  214. - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
  215. `MultipartFormDataEncodingMemoryThreshold` by default.
  216. - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
  217. */
  218. public func upload(
  219. method: Method,
  220. _ URLString: URLStringConvertible,
  221. headers: [String: String]? = nil,
  222. multipartFormData: MultipartFormData -> Void,
  223. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  224. encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  225. {
  226. return Manager.sharedInstance.upload(
  227. method,
  228. URLString,
  229. headers: headers,
  230. multipartFormData: multipartFormData,
  231. encodingMemoryThreshold: encodingMemoryThreshold,
  232. encodingCompletion: encodingCompletion
  233. )
  234. }
  235. /**
  236. Creates an upload request using the shared manager instance for the specified method and URL string.
  237. - parameter URLRequest: The URL request.
  238. - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  239. - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
  240. `MultipartFormDataEncodingMemoryThreshold` by default.
  241. - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
  242. */
  243. public func upload(
  244. URLRequest: URLRequestConvertible,
  245. multipartFormData: MultipartFormData -> Void,
  246. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  247. encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  248. {
  249. return Manager.sharedInstance.upload(
  250. URLRequest,
  251. multipartFormData: multipartFormData,
  252. encodingMemoryThreshold: encodingMemoryThreshold,
  253. encodingCompletion: encodingCompletion
  254. )
  255. }
  256. // MARK: - Download Methods
  257. // MARK: URL Request
  258. /**
  259. Creates a download request using the shared manager instance for the specified method and URL string.
  260. - parameter method: The HTTP method.
  261. - parameter URLString: The URL string.
  262. - parameter parameters: The parameters. `nil` by default.
  263. - parameter encoding: The parameter encoding. `.URL` by default.
  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. parameters: [String: AnyObject]? = nil,
  272. encoding: ParameterEncoding = .URL,
  273. headers: [String: String]? = nil,
  274. destination: Request.DownloadFileDestination)
  275. -> Request
  276. {
  277. return Manager.sharedInstance.download(
  278. method,
  279. URLString,
  280. parameters: parameters,
  281. encoding: encoding,
  282. headers: headers,
  283. destination: destination
  284. )
  285. }
  286. /**
  287. Creates a download request using the shared manager instance for the specified URL request.
  288. - parameter URLRequest: The URL request.
  289. - parameter destination: The closure used to determine the destination of the downloaded file.
  290. - returns: The created download request.
  291. */
  292. public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request {
  293. return Manager.sharedInstance.download(URLRequest, destination: destination)
  294. }
  295. // MARK: Resume Data
  296. /**
  297. Creates a request using the shared manager instance for downloading from the resume data produced from a
  298. previous request cancellation.
  299. - parameter resumeData: The resume data. This is an opaque data blob produced by `NSURLSessionDownloadTask`
  300. when a task is cancelled. See `NSURLSession -downloadTaskWithResumeData:` for additional
  301. information.
  302. - parameter destination: The closure used to determine the destination of the downloaded file.
  303. - returns: The created download request.
  304. */
  305. public func download(resumeData data: NSData, destination: Request.DownloadFileDestination) -> Request {
  306. return Manager.sharedInstance.download(data, destination: destination)
  307. }