Get.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 client = GRPCClient(
  27. transport: try .http2NIOPosix(
  28. target: self.arguments.target,
  29. config: .defaults()
  30. )
  31. )
  32. try await withThrowingDiscardingTaskGroup { group in
  33. group.addTask {
  34. try await client.run()
  35. }
  36. let echo = Echo_EchoClient(wrapping: client)
  37. for _ in 0 ..< self.arguments.repetitions {
  38. let message = Echo_EchoRequest.with { $0.text = self.arguments.message }
  39. print("get → \(message.text)")
  40. let response = try await echo.get(message)
  41. print("get ← \(response.text)")
  42. }
  43. client.close()
  44. }
  45. }
  46. }