ControlClient.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  18. internal struct ControlClient {
  19. private let client: GRPCCore.GRPCClient
  20. internal init(wrapping client: GRPCCore.GRPCClient) {
  21. self.client = client
  22. }
  23. internal func unary<R>(
  24. request: GRPCCore.ClientRequest.Single<ControlInput>,
  25. options: GRPCCore.CallOptions = .defaults,
  26. _ body: @Sendable @escaping (GRPCCore.ClientResponse.Single<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(service: "Control", method: "Unary"),
  34. serializer: JSONSerializer(),
  35. deserializer: JSONDeserializer(),
  36. options: options,
  37. handler: body
  38. )
  39. }
  40. internal func serverStream<R>(
  41. request: GRPCCore.ClientRequest.Single<ControlInput>,
  42. options: GRPCCore.CallOptions = .defaults,
  43. _ body: @Sendable @escaping (GRPCCore.ClientResponse.Stream<ControlOutput>) async throws -> R
  44. ) async throws -> R where R: Sendable {
  45. try await self.client.serverStreaming(
  46. request: request,
  47. descriptor: MethodDescriptor(service: "Control", method: "ServerStream"),
  48. serializer: JSONSerializer(),
  49. deserializer: JSONDeserializer(),
  50. options: options,
  51. handler: body
  52. )
  53. }
  54. internal func clientStream<R>(
  55. request: GRPCCore.ClientRequest.Stream<ControlInput>,
  56. options: GRPCCore.CallOptions = .defaults,
  57. _ body: @Sendable @escaping (GRPCCore.ClientResponse.Single<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(service: "Control", method: "ClientStream"),
  65. serializer: JSONSerializer(),
  66. deserializer: JSONDeserializer(),
  67. options: options,
  68. handler: body
  69. )
  70. }
  71. internal func bidiStream<R>(
  72. request: GRPCCore.ClientRequest.Stream<ControlInput>,
  73. options: GRPCCore.CallOptions = .defaults,
  74. _ body: @Sendable @escaping (GRPCCore.ClientResponse.Stream<ControlOutput>) async throws -> R
  75. ) async throws -> R where R: Sendable {
  76. try await self.client.bidirectionalStreaming(
  77. request: request,
  78. descriptor: MethodDescriptor(service: "Control", method: "BidiStream"),
  79. serializer: JSONSerializer(),
  80. deserializer: JSONDeserializer(),
  81. options: options,
  82. handler: body
  83. )
  84. }
  85. }