Response.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 and the response serialization result.
  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 final destination URL of the data returned from the server.
  99. public let destinationURL: URL?
  100. /// The resume data generated if the request was cancelled.
  101. public let resumeData: Data?
  102. /// The error encountered while executing or validating the request.
  103. public let error: Error?
  104. }
  105. // MARK: -
  106. /// Used to store all data associated with a serialized response of a download request.
  107. public struct DownloadResponse<Value> {
  108. /// The URL request sent to the server.
  109. public let request: URLRequest?
  110. /// The server's response to the URL request.
  111. public let response: HTTPURLResponse?
  112. /// The final destination URL of the data returned from the server.
  113. public let destinationURL: URL?
  114. /// The resume data generated if the request was cancelled.
  115. public let resumeData: Data?
  116. /// The result of response serialization.
  117. public let result: Result<Value>
  118. /// The timeline of the complete lifecycle of the request.
  119. public let timeline: Timeline
  120. /// Creates a `DownloadResponse` instance with the specified parameters derived from response serialization.
  121. ///
  122. /// - parameter request: The URL request sent to the server.
  123. /// - parameter response: The server's response to the URL request.
  124. /// - parameter destinationURL: The final destination URL of the data returned from the server.
  125. /// - parameter resumeData: The resume data generated if the request was cancelled.
  126. /// - parameter result: The result of response serialization.
  127. /// - parameter timeline: The timeline of the complete lifecycle of the `Request`. Defaults to `Timeline()`.
  128. ///
  129. /// - returns: The new `DownloadResponse` instance.
  130. public init(
  131. request: URLRequest?,
  132. response: HTTPURLResponse?,
  133. destinationURL: URL?,
  134. resumeData: Data?,
  135. result: Result<Value>,
  136. timeline: Timeline = Timeline())
  137. {
  138. self.request = request
  139. self.response = response
  140. self.destinationURL = destinationURL
  141. self.resumeData = resumeData
  142. self.result = result
  143. self.timeline = timeline
  144. }
  145. }
  146. // MARK: -
  147. extension DownloadResponse: CustomStringConvertible {
  148. /// The textual representation used when written to an output stream, which includes whether the result was a
  149. /// success or failure.
  150. public var description: String {
  151. return result.debugDescription
  152. }
  153. /// The debug textual representation used when written to an output stream, which includes the URL request, the URL
  154. /// response, the server data and the response serialization result.
  155. public var debugDescription: String {
  156. var output: [String] = []
  157. output.append(request != nil ? "[Request]: \(request!)" : "[Request]: nil")
  158. output.append(response != nil ? "[Response]: \(response!)" : "[Response]: nil")
  159. output.append("[DestinationURL]: \(destinationURL?.path ?? "nil")")
  160. output.append("[ResumeData]: \(resumeData?.count ?? 0) bytes")
  161. output.append("[Result]: \(result.debugDescription)")
  162. output.append("[Timeline]: \(timeline.debugDescription)")
  163. return output.joined(separator: "\n")
  164. }
  165. }