Response.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Response.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. /// Used to store all data associated with an non-serialized response of a data or upload request.
  26. public struct DefaultDataResponse {
  27. /// The URL request sent to the server.
  28. public let request: URLRequest?
  29. /// The server's response to the URL request.
  30. public let response: HTTPURLResponse?
  31. /// The data returned by the server.
  32. public let data: Data?
  33. /// The error encountered while executing or validating the request.
  34. public let error: Error?
  35. }
  36. // MARK: -
  37. /// Used to store all data associated with a serialized response of a data or upload request.
  38. public struct DataResponse<Value> {
  39. /// The URL request sent to the server.
  40. public let request: URLRequest?
  41. /// The server's response to the URL request.
  42. public let response: HTTPURLResponse?
  43. /// The data returned by the server.
  44. public let data: Data?
  45. /// The result of response serialization.
  46. public let result: Result<Value>
  47. /// The timeline of the complete lifecycle of the `Request`.
  48. public let timeline: Timeline
  49. /// Creates a `DataResponse` instance with the specified parameters derived from response serialization.
  50. ///
  51. /// - parameter request: The URL request sent to the server.
  52. /// - parameter response: The server's response to the URL request.
  53. /// - parameter data: The data returned by the server.
  54. /// - parameter result: The result of response serialization.
  55. /// - parameter timeline: The timeline of the complete lifecycle of the `Request`. Defaults to `Timeline()`.
  56. ///
  57. /// - returns: The new `DataResponse` instance.
  58. public init(
  59. request: URLRequest?,
  60. response: HTTPURLResponse?,
  61. data: Data?,
  62. result: Result<Value>,
  63. timeline: Timeline = Timeline())
  64. {
  65. self.request = request
  66. self.response = response
  67. self.data = data
  68. self.result = result
  69. self.timeline = timeline
  70. }
  71. }
  72. // MARK: -
  73. extension DataResponse: CustomStringConvertible, CustomDebugStringConvertible {
  74. /// The textual representation used when written to an output stream, which includes whether the result was a
  75. /// success or failure.
  76. public var description: String {
  77. return result.debugDescription
  78. }
  79. /// The debug textual representation used when written to an output stream, which includes the URL request, the URL
  80. /// response, the server data, the response serialization result and the timeline.
  81. public var debugDescription: String {
  82. var output: [String] = []
  83. output.append(request != nil ? "[Request]: \(request!)" : "[Request]: nil")
  84. output.append(response != nil ? "[Response]: \(response!)" : "[Response]: nil")
  85. output.append("[Data]: \(data?.count ?? 0) bytes")
  86. output.append("[Result]: \(result.debugDescription)")
  87. output.append("[Timeline]: \(timeline.debugDescription)")
  88. return output.joined(separator: "\n")
  89. }
  90. }
  91. // MARK: -
  92. /// Used to store all data associated with an non-serialized response of a download request.
  93. public struct DefaultDownloadResponse {
  94. /// The URL request sent to the server.
  95. public let request: URLRequest?
  96. /// The server's response to the URL request.
  97. public let response: HTTPURLResponse?
  98. /// The temporary destination URL of the data returned from the server.
  99. public let temporaryURL: URL?
  100. /// The final destination URL of the data returned from the server if it was moved.
  101. public let destinationURL: URL?
  102. /// The resume data generated if the request was cancelled.
  103. public let resumeData: Data?
  104. /// The error encountered while executing or validating the request.
  105. public let error: Error?
  106. }
  107. // MARK: -
  108. /// Used to store all data associated with a serialized response of a download request.
  109. public struct DownloadResponse<Value> {
  110. /// The URL request sent to the server.
  111. public let request: URLRequest?
  112. /// The server's response to the URL request.
  113. public let response: HTTPURLResponse?
  114. /// The temporary destination URL of the data returned from the server.
  115. public let temporaryURL: URL?
  116. /// The final destination URL of the data returned from the server if it was moved.
  117. public let destinationURL: URL?
  118. /// The resume data generated if the request was cancelled.
  119. public let resumeData: Data?
  120. /// The result of response serialization.
  121. public let result: Result<Value>
  122. /// The timeline of the complete lifecycle of the request.
  123. public let timeline: Timeline
  124. /// Creates a `DownloadResponse` instance with the specified parameters derived from response serialization.
  125. ///
  126. /// - parameter request: The URL request sent to the server.
  127. /// - parameter response: The server's response to the URL request.
  128. /// - parameter temporaryURL: The temporary destination URL of the data returned from the server.
  129. /// - parameter destinationURL: The final destination URL of the data returned from the server if it was moved.
  130. /// - parameter resumeData: The resume data generated if the request was cancelled.
  131. /// - parameter result: The result of response serialization.
  132. /// - parameter timeline: The timeline of the complete lifecycle of the `Request`. Defaults to `Timeline()`.
  133. ///
  134. /// - returns: The new `DownloadResponse` instance.
  135. public init(
  136. request: URLRequest?,
  137. response: HTTPURLResponse?,
  138. temporaryURL: URL?,
  139. destinationURL: URL?,
  140. resumeData: Data?,
  141. result: Result<Value>,
  142. timeline: Timeline = Timeline())
  143. {
  144. self.request = request
  145. self.response = response
  146. self.temporaryURL = temporaryURL
  147. self.destinationURL = destinationURL
  148. self.resumeData = resumeData
  149. self.result = result
  150. self.timeline = timeline
  151. }
  152. }
  153. // MARK: -
  154. extension DownloadResponse: CustomStringConvertible, CustomDebugStringConvertible {
  155. /// The textual representation used when written to an output stream, which includes whether the result was a
  156. /// success or failure.
  157. public var description: String {
  158. return result.debugDescription
  159. }
  160. /// The debug textual representation used when written to an output stream, which includes the URL request, the URL
  161. /// response, the temporary and destination URLs, the resume data, the response serialization result and the
  162. /// timeline.
  163. public var debugDescription: String {
  164. var output: [String] = []
  165. output.append(request != nil ? "[Request]: \(request!)" : "[Request]: nil")
  166. output.append(response != nil ? "[Response]: \(response!)" : "[Response]: nil")
  167. output.append("[TemporaryURL]: \(temporaryURL?.path ?? "nil")")
  168. output.append("[DestinationURL]: \(destinationURL?.path ?? "nil")")
  169. output.append("[ResumeData]: \(resumeData?.count ?? 0) bytes")
  170. output.append("[Result]: \(result.debugDescription)")
  171. output.append("[Timeline]: \(timeline.debugDescription)")
  172. return output.joined(separator: "\n")
  173. }
  174. }