GetFeature.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 GRPCHTTP2Transport
  18. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  19. struct GetFeature: AsyncParsableCommand {
  20. static let configuration = CommandConfiguration(abstract: "Gets a feature at a given location.")
  21. @Option(help: "The server's listening port")
  22. var port: Int = 31415
  23. @Option(
  24. name: [.customLong("latitude"), .customLong("lat")],
  25. help: "Latitude of the feature to get in E7 format (degrees ⨯ 1e7)"
  26. )
  27. var latitude: Int32 = 407_838_351
  28. @Option(
  29. name: [.customLong("longitude"), .customLong("lon")],
  30. help: "Longitude of the feature to get in E7 format (degrees ⨯ 1e7)"
  31. )
  32. var longitude: Int32 = -746_143_763
  33. func run() async throws {
  34. let transport = try HTTP2ClientTransport.Posix(
  35. target: .ipv4(host: "127.0.0.1", port: self.port),
  36. config: .defaults()
  37. )
  38. let client = GRPCClient(transport: transport)
  39. try await withThrowingDiscardingTaskGroup { group in
  40. group.addTask {
  41. try await client.run()
  42. }
  43. let routeGuide = Routeguide_RouteGuideClient(wrapping: client)
  44. let point = Routeguide_Point.with {
  45. $0.latitude = self.latitude
  46. $0.longitude = self.longitude
  47. }
  48. let feature = try await routeGuide.getFeature(point)
  49. if feature.name.isEmpty {
  50. print("No feature found at (\(self.latitude), \(self.longitude))")
  51. } else {
  52. print("Found '\(feature.name)' at (\(self.latitude), \(self.longitude))")
  53. }
  54. client.beginGracefulShutdown()
  55. }
  56. }
  57. }