Response.swift 3.7 KB

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