ControlClient.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2024, 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. import GRPCCore
  17. @available(gRPCSwiftNIOTransport 2.0, *)
  18. internal struct ControlClient<Transport> where Transport: ClientTransport {
  19. internal let client: GRPCCore.GRPCClient<Transport>
  20. internal init(wrapping client: GRPCCore.GRPCClient<Transport>) {
  21. self.client = client
  22. }
  23. internal func unary<R>(
  24. request: GRPCCore.ClientRequest<ControlInput>,
  25. options: GRPCCore.CallOptions = .defaults,
  26. _ body: @Sendable @escaping (GRPCCore.ClientResponse<ControlOutput>) async throws -> R =
  27. {
  28. try $0.message
  29. }
  30. ) async throws -> R where R: Sendable {
  31. try await self.client.unary(
  32. request: request,
  33. descriptor: MethodDescriptor(fullyQualifiedService: "Control", method: "Unary"),
  34. serializer: JSONSerializer(),
  35. deserializer: JSONDeserializer(),
  36. options: options,
  37. onResponse: body
  38. )
  39. }
  40. internal func serverStream<R>(
  41. request: GRPCCore.ClientRequest<ControlInput>,
  42. options: GRPCCore.CallOptions = .defaults,
  43. _ body: @Sendable @escaping (GRPCCore.StreamingClientResponse<ControlOutput>) async throws -> R
  44. ) async throws -> R where R: Sendable {
  45. try await self.client.serverStreaming(
  46. request: request,
  47. descriptor: MethodDescriptor(fullyQualifiedService: "Control", method: "ServerStream"),
  48. serializer: JSONSerializer(),
  49. deserializer: JSONDeserializer(),
  50. options: options,
  51. onResponse: body
  52. )
  53. }
  54. internal func clientStream<R>(
  55. request: GRPCCore.StreamingClientRequest<ControlInput>,
  56. options: GRPCCore.CallOptions = .defaults,
  57. _ body: @Sendable @escaping (GRPCCore.ClientResponse<ControlOutput>) async throws -> R =
  58. {
  59. try $0.message
  60. }
  61. ) async throws -> R where R: Sendable {
  62. try await self.client.clientStreaming(
  63. request: request,
  64. descriptor: MethodDescriptor(fullyQualifiedService: "Control", method: "ClientStream"),
  65. serializer: JSONSerializer(),
  66. deserializer: JSONDeserializer(),
  67. options: options,
  68. onResponse: body
  69. )
  70. }
  71. internal func bidiStream<R>(
  72. request: GRPCCore.StreamingClientRequest<ControlInput>,
  73. options: GRPCCore.CallOptions = .defaults,
  74. _ body: @Sendable @escaping (GRPCCore.StreamingClientResponse<ControlOutput>) async throws -> R
  75. ) async throws -> R where R: Sendable {
  76. try await self.client.bidirectionalStreaming(
  77. request: request,
  78. descriptor: MethodDescriptor(fullyQualifiedService: "Control", method: "BidiStream"),
  79. serializer: JSONSerializer(),
  80. deserializer: JSONDeserializer(),
  81. options: options,
  82. onResponse: body
  83. )
  84. }
  85. internal func waitForCancellation<R>(
  86. request: GRPCCore.ClientRequest<CancellationKind>,
  87. options: GRPCCore.CallOptions = .defaults,
  88. _ body: @Sendable @escaping (
  89. _ response: GRPCCore.StreamingClientResponse<CancellationKind>
  90. ) async throws -> R
  91. ) async throws -> R where R: Sendable {
  92. try await self.client.serverStreaming(
  93. request: request,
  94. descriptor: MethodDescriptor(fullyQualifiedService: "Control", method: "WaitForCancellation"),
  95. serializer: JSONSerializer(),
  96. deserializer: JSONDeserializer(),
  97. options: options,
  98. onResponse: body
  99. )
  100. }
  101. internal func peerInfo<R>(
  102. options: GRPCCore.CallOptions = .defaults,
  103. _ body: @Sendable @escaping (
  104. _ response: GRPCCore.ClientResponse<ControlService.PeerInfoResponse>
  105. ) async throws -> R = { try $0.message }
  106. ) async throws -> R where R: Sendable {
  107. try await self.client.unary(
  108. request: ClientRequest(message: ""),
  109. descriptor: MethodDescriptor(fullyQualifiedService: "Control", method: "PeerInfo"),
  110. serializer: JSONSerializer(),
  111. deserializer: JSONDeserializer(),
  112. options: options,
  113. onResponse: body
  114. )
  115. }
  116. }