ControlClient.swift 3.6 KB

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