GetFeature.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 GRPCNIOTransportHTTP2
  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. try await withGRPCClient(
  35. transport: .http2NIOPosix(
  36. target: .ipv4(host: "127.0.0.1", port: self.port),
  37. transportSecurity: .plaintext
  38. )
  39. ) { client in
  40. let routeGuide = Routeguide_RouteGuide.Client(wrapping: client)
  41. let point = Routeguide_Point.with {
  42. $0.latitude = self.latitude
  43. $0.longitude = self.longitude
  44. }
  45. let feature = try await routeGuide.getFeature(point)
  46. if feature.name.isEmpty {
  47. print("No feature found at (\(self.latitude), \(self.longitude))")
  48. } else {
  49. print("Found '\(feature.name)' at (\(self.latitude), \(self.longitude))")
  50. }
  51. }
  52. }
  53. }