CallResponse.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #if SWIFT_PACKAGE
  34. import CgRPC
  35. #endif
  36. import Foundation
  37. /// Representation of a response to a gRPC call
  38. public class CallResponse {
  39. /// Error code that could be generated when the call is created
  40. public var error: grpc_call_error
  41. /// Result of waiting for call completion
  42. public var completion: grpc_completion_type
  43. /// Status code returned by server
  44. public var status: Int
  45. /// Status message optionally returned by server
  46. public var statusDetails: String
  47. /// Message returned by server
  48. public var messageData: Data?
  49. /// Initial metadata returned by server
  50. public var initialMetadata: Metadata?
  51. /// Trailing metadata returned by server
  52. public var trailingMetadata: Metadata?
  53. /// Initializes a response when error != GRPC_CALL_OK
  54. ///
  55. /// - Parameter error: an error code from when the call was performed
  56. public init(error: grpc_call_error) {
  57. self.error = error
  58. self.completion = GRPC_OP_COMPLETE
  59. self.status = 0
  60. self.statusDetails = ""
  61. }
  62. /// Initializes a response when completion != GRPC_OP_COMPLETE
  63. ///
  64. /// - Parameter completion: a code indicating the result of waiting for the call to complete
  65. public init(completion: grpc_completion_type) {
  66. self.error = GRPC_CALL_OK
  67. self.completion = completion
  68. self.status = 0
  69. self.statusDetails = ""
  70. }
  71. /// Initializes a response when error == GRPC_CALL_OK and completion == GRPC_OP_COMPLETE
  72. ///
  73. /// - Parameter status: a status code returned from the server
  74. /// - Parameter statusDetails: a status string returned from the server
  75. /// - Parameter message: a buffer containing results returned from the server
  76. /// - Parameter initialMetadata: initial metadata returned by the server
  77. /// - Parameter trailingMetadata: trailing metadata returned by the server
  78. init(status:Int,
  79. statusDetails:String,
  80. message:ByteBuffer?,
  81. initialMetadata:Metadata?,
  82. trailingMetadata:Metadata?) {
  83. self.error = GRPC_CALL_OK
  84. self.completion = GRPC_OP_COMPLETE
  85. self.status = status
  86. self.statusDetails = statusDetails
  87. if let message = message {
  88. self.messageData = message.data()
  89. }
  90. self.initialMetadata = initialMetadata
  91. self.trailingMetadata = trailingMetadata
  92. }
  93. }