2
0

Response.swift 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 response data returned from a completed `Request`.
  26. public struct Response<Value, Error: ErrorType> {
  27. /// The URL request sent to the server.
  28. public let request: NSURLRequest?
  29. /// The server's response to the URL request.
  30. public let response: NSHTTPURLResponse?
  31. /// The data returned by the server.
  32. public let data: NSData?
  33. /// The result of response serialization.
  34. public let result: Result<Value, Error>
  35. /// The timeline of the complete lifecycle of the `Request`.
  36. public let timeline: Timeline
  37. /**
  38. Initializes the `Response` instance with the specified URL request, URL response, server data and response
  39. serialization result.
  40. - parameter request: The URL request sent to the server.
  41. - parameter response: The server's response to the URL request.
  42. - parameter data: The data returned by the server.
  43. - parameter result: The result of response serialization.
  44. - parameter timeline: The timeline of the complete lifecycle of the `Request`. Defaults to `Timeline()`.
  45. - returns: the new `Response` instance.
  46. */
  47. public init(
  48. request: NSURLRequest?,
  49. response: NSHTTPURLResponse?,
  50. data: NSData?,
  51. result: Result<Value, Error>,
  52. timeline: Timeline = Timeline())
  53. {
  54. self.request = request
  55. self.response = response
  56. self.data = data
  57. self.result = result
  58. self.timeline = timeline
  59. }
  60. }
  61. // MARK: - CustomStringConvertible
  62. extension Response: CustomStringConvertible {
  63. /// The textual representation used when written to an output stream, which includes whether the result was a
  64. /// success or failure.
  65. public var description: String {
  66. return result.debugDescription
  67. }
  68. }
  69. // MARK: - CustomDebugStringConvertible
  70. extension Response: CustomDebugStringConvertible {
  71. /// The debug textual representation used when written to an output stream, which includes the URL request, the URL
  72. /// response, the server data and the response serialization result.
  73. public var debugDescription: String {
  74. var output: [String] = []
  75. output.append(request != nil ? "[Request]: \(request!)" : "[Request]: nil")
  76. output.append(response != nil ? "[Response]: \(response!)" : "[Response]: nil")
  77. output.append("[Data]: \(data?.length ?? 0) bytes")
  78. output.append("[Result]: \(result.debugDescription)")
  79. output.append("[Timeline]: \(timeline.debugDescription)")
  80. return output.joinWithSeparator("\n")
  81. }
  82. }