2
0

Response.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Result.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. /// 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. /**
  34. Initializes the `Response` instance with the specified URL request, URL response, server data and response
  35. serialization result.
  36. - parameter request: The URL request sent to the server.
  37. - parameter response: The server's response to the URL request.
  38. - parameter data: The data returned by the server.
  39. - parameter result: The result of response serialization.
  40. - returns: the new `Response` instance.
  41. */
  42. public init(request: NSURLRequest?, response: NSHTTPURLResponse?, data: NSData?, result: Result<Value, Error>) {
  43. self.request = request
  44. self.response = response
  45. self.data = data
  46. self.result = result
  47. }
  48. }
  49. // MARK: - CustomStringConvertible
  50. extension Response: CustomStringConvertible {
  51. /// The textual representation used when written to an output stream, which includes whether the result was a
  52. /// success or failure.
  53. public var description: String {
  54. return result.debugDescription
  55. }
  56. }
  57. // MARK: - CustomDebugStringConvertible
  58. extension Response: CustomDebugStringConvertible {
  59. /// The debug textual representation used when written to an output stream, which includes the URL request, the URL
  60. /// response, the server data and the response serialization result.
  61. public var debugDescription: String {
  62. var output: [String] = []
  63. output.append(request != nil ? "[Request]: \(request!)" : "[Request]: nil")
  64. output.append(response != nil ? "[Response]: \(response!)" : "[Response]: nil")
  65. output.append("[Data]: \(data?.length ?? 0) bytes")
  66. output.append("[Result]: \(result.debugDescription)")
  67. return output.joinWithSeparator("\n")
  68. }
  69. }