2
0

Collect.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ArgumentParser
  17. import GRPCCore
  18. import GRPCHTTP2Core
  19. import GRPCHTTP2TransportNIOPosix
  20. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  21. struct Collect: AsyncParsableCommand {
  22. static let configuration = CommandConfiguration(
  23. abstract: "Makes a client streaming RPC to the echo server."
  24. )
  25. @OptionGroup
  26. var arguments: ClientArguments
  27. func run() async throws {
  28. let transport = try HTTP2ClientTransport.Posix(target: self.arguments.target)
  29. let client = GRPCClient(transport: transport)
  30. try await withThrowingDiscardingTaskGroup { group in
  31. group.addTask {
  32. try await client.run()
  33. }
  34. let echo = Echo_EchoClient(client: client)
  35. for _ in 0 ..< self.arguments.repetitions {
  36. let request = ClientRequest.Stream(of: Echo_EchoRequest.self) { writer in
  37. for part in self.arguments.message.split(separator: " ") {
  38. print("collect → \(part)")
  39. try await writer.write(.with { $0.text = String(part) })
  40. }
  41. }
  42. try await echo.collect(request: request) { response in
  43. let message = try response.message
  44. print("collect ← \(message.text)")
  45. }
  46. }
  47. client.close()
  48. }
  49. }
  50. }