Get.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  21. struct Get: AsyncParsableCommand {
  22. static let configuration = CommandConfiguration(abstract: "Makes a unary RPC to the echo server.")
  23. @OptionGroup
  24. var arguments: ClientArguments
  25. func run() async throws {
  26. let transport = try HTTP2ClientTransport.Posix(target: self.arguments.target)
  27. let client = GRPCClient(transport: transport)
  28. try await withThrowingDiscardingTaskGroup { group in
  29. group.addTask {
  30. try await client.run()
  31. }
  32. let echo = Echo_EchoClient(wrapping: client)
  33. for _ in 0 ..< self.arguments.repetitions {
  34. let message = Echo_EchoRequest.with { $0.text = self.arguments.message }
  35. print("get → \(message.text)")
  36. let response = try await echo.get(message)
  37. print("get ← \(message.text)")
  38. }
  39. client.close()
  40. }
  41. }
  42. }