Alamofire.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // MARK: - URLStringConvertible
  26. /**
  27. Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to construct URL requests.
  28. */
  29. public protocol URLStringConvertible {
  30. /**
  31. A URL that conforms to RFC 2396.
  32. Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
  33. See http://tools.ietf.org/html/rfc2396
  34. See http://tools.ietf.org/html/rfc1738
  35. See http://tools.ietf.org/html/rfc1808
  36. */
  37. var URLString: String { get }
  38. }
  39. extension String: URLStringConvertible {
  40. public var URLString: String {
  41. return self
  42. }
  43. }
  44. extension NSURL: URLStringConvertible {
  45. public var URLString: String {
  46. return absoluteString!
  47. }
  48. }
  49. extension NSURLComponents: URLStringConvertible {
  50. public var URLString: String {
  51. return URL!.URLString
  52. }
  53. }
  54. extension NSURLRequest: URLStringConvertible {
  55. public var URLString: String {
  56. return URL!.URLString
  57. }
  58. }
  59. // MARK: - URLRequestConvertible
  60. /**
  61. Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
  62. */
  63. public protocol URLRequestConvertible {
  64. /// The URL request.
  65. var URLRequest: NSURLRequest { get }
  66. }
  67. extension NSURLRequest: URLRequestConvertible {
  68. public var URLRequest: NSURLRequest {
  69. return self
  70. }
  71. }
  72. // MARK: - Convenience -
  73. func URLRequest(method: Method, URL: URLStringConvertible) -> NSURLRequest {
  74. let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URL.URLString)!)
  75. mutableURLRequest.HTTPMethod = method.rawValue
  76. return mutableURLRequest
  77. }
  78. // MARK: - Request
  79. /**
  80. Creates a request using the shared manager instance for the specified method, URL string, parameters, and parameter encoding.
  81. :param: method The HTTP method.
  82. :param: URLString The URL string.
  83. :param: parameters The parameters. `nil` by default.
  84. :param: encoding The parameter encoding. `.URL` by default.
  85. :returns: The created request.
  86. */
  87. public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request {
  88. return Manager.sharedInstance.request(method, URLString, parameters: parameters, encoding: encoding)
  89. }
  90. /**
  91. Creates a request using the shared manager instance for the specified URL request.
  92. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  93. :param: URLRequest The URL request
  94. :returns: The created request.
  95. */
  96. public func request(URLRequest: URLRequestConvertible) -> Request {
  97. return Manager.sharedInstance.request(URLRequest.URLRequest)
  98. }
  99. // MARK: - Upload
  100. // MARK: File
  101. /**
  102. Creates an upload request using the shared manager instance for the specified method, URL string, and file.
  103. :param: method The HTTP method.
  104. :param: URLString The URL string.
  105. :param: file The file to upload.
  106. :returns: The created upload request.
  107. */
  108. public func upload(method: Method, URLString: URLStringConvertible, file: NSURL) -> Request {
  109. return Manager.sharedInstance.upload(method, URLString, file: file)
  110. }
  111. /**
  112. Creates an upload request using the shared manager instance for the specified URL request and file.
  113. :param: URLRequest The URL request.
  114. :param: file The file to upload.
  115. :returns: The created upload request.
  116. */
  117. public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
  118. return Manager.sharedInstance.upload(URLRequest, file: file)
  119. }
  120. // MARK: Data
  121. /**
  122. Creates an upload request using the shared manager instance for the specified method, URL string, and data.
  123. :param: method The HTTP method.
  124. :param: URLString The URL string.
  125. :param: data The data to upload.
  126. :returns: The created upload request.
  127. */
  128. public func upload(method: Method, URLString: URLStringConvertible, data: NSData) -> Request {
  129. return Manager.sharedInstance.upload(method, URLString, data: data)
  130. }
  131. /**
  132. Creates an upload request using the shared manager instance for the specified URL request and data.
  133. :param: URLRequest The URL request.
  134. :param: data The data to upload.
  135. :returns: The created upload request.
  136. */
  137. public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
  138. return Manager.sharedInstance.upload(URLRequest, data: data)
  139. }
  140. // MARK: Stream
  141. /**
  142. Creates an upload request using the shared manager instance for the specified method, URL string, and stream.
  143. :param: method The HTTP method.
  144. :param: URLString The URL string.
  145. :param: stream The stream to upload.
  146. :returns: The created upload request.
  147. */
  148. public func upload(method: Method, URLString: URLStringConvertible, stream: NSInputStream) -> Request {
  149. return Manager.sharedInstance.upload(method, URLString, stream: stream)
  150. }
  151. /**
  152. Creates an upload request using the shared manager instance for the specified URL request and stream.
  153. :param: URLRequest The URL request.
  154. :param: stream The stream to upload.
  155. :returns: The created upload request.
  156. */
  157. public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
  158. return Manager.sharedInstance.upload(URLRequest, stream: stream)
  159. }
  160. // MARK: - Download
  161. // MARK: URL Request
  162. /**
  163. Creates a download request using the shared manager instance for the specified method and URL string.
  164. :param: method The HTTP method.
  165. :param: URLString The URL string.
  166. :param: destination The closure used to determine the destination of the downloaded file.
  167. :returns: The created download request.
  168. */
  169. public func download(method: Method, URLString: URLStringConvertible, destination: Request.DownloadFileDestination) -> Request {
  170. return Manager.sharedInstance.download(method, URLString, destination: destination)
  171. }
  172. /**
  173. Creates a download request using the shared manager instance for the specified URL request.
  174. :param: URLRequest The URL request.
  175. :param: destination The closure used to determine the destination of the downloaded file.
  176. :returns: The created download request.
  177. */
  178. public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request {
  179. return Manager.sharedInstance.download(URLRequest, destination: destination)
  180. }
  181. // MARK: Resume Data
  182. /**
  183. Creates a request using the shared manager instance for downloading from the resume data produced from a previous request cancellation.
  184. :param: resumeData The resume data. This is an opaque data blob produced by `NSURLSessionDownloadTask` when a task is cancelled. See `NSURLSession -downloadTaskWithResumeData:` for additional information.
  185. :param: destination The closure used to determine the destination of the downloaded file.
  186. :returns: The created download request.
  187. */
  188. public func download(resumeData data: NSData, destination: Request.DownloadFileDestination) -> Request {
  189. return Manager.sharedInstance.download(data, destination: destination)
  190. }