CallResult.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if SWIFT_PACKAGE
  17. import CgRPC
  18. import Dispatch
  19. #endif
  20. import Foundation
  21. public struct CallResult: CustomStringConvertible {
  22. public let success: Bool
  23. public let statusCode: StatusCode
  24. public let statusMessage: String?
  25. public let resultData: Data?
  26. public let initialMetadata: Metadata?
  27. public let trailingMetadata: Metadata?
  28. init(_ op: OperationGroup) {
  29. success = op.success
  30. if let statusCodeRawValue = op.receivedStatusCode(),
  31. let statusCode = StatusCode(rawValue: statusCodeRawValue) {
  32. self.statusCode = statusCode
  33. } else {
  34. statusCode = .unknown
  35. }
  36. statusMessage = op.receivedStatusMessage()
  37. resultData = op.receivedMessage()?.data()
  38. initialMetadata = op.receivedInitialMetadata()
  39. trailingMetadata = op.receivedTrailingMetadata()
  40. }
  41. public var description: String {
  42. var result = "status \(statusCode)"
  43. if let statusMessage = self.statusMessage {
  44. result += ": " + statusMessage
  45. }
  46. if let resultData = self.resultData {
  47. result += "\n"
  48. result += resultData.description
  49. }
  50. if let initialMetadata = self.initialMetadata {
  51. result += "\n"
  52. result += initialMetadata.description
  53. }
  54. if let trailingMetadata = self.trailingMetadata {
  55. result += "\n"
  56. result += trailingMetadata.description
  57. }
  58. return result
  59. }
  60. }