Alamofire.swift 12 KB

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