Alamofire.swift 12 KB

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