|
| 1 | +/* |
| 2 | + * Copyright 2025, 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 | + |
| 17 | +import ArgumentParser |
| 18 | +import GRPCCore |
| 19 | +import GRPCNIOTransportHTTP2 |
| 20 | + |
| 21 | +struct Echo: AsyncParsableCommand { |
| 22 | + static let configuration = CommandConfiguration( |
| 23 | + abstract: |
| 24 | + "Makes a unary RPC to the echo-metadata server, followed by a bidirectional-streaming request." |
| 25 | + ) |
| 26 | + |
| 27 | + @OptionGroup |
| 28 | + var arguments: ClientArguments |
| 29 | + |
| 30 | + func run() async throws { |
| 31 | + try await withGRPCClient( |
| 32 | + transport: .http2NIOPosix( |
| 33 | + target: self.arguments.target, |
| 34 | + transportSecurity: .plaintext |
| 35 | + ) |
| 36 | + ) { client in |
| 37 | + let echo = Echo_Echo.Client(wrapping: client) |
| 38 | + |
| 39 | + var requestMetadata: Metadata = [:] |
| 40 | + for metadataPair in arguments.metadata { |
| 41 | + guard metadataPair.starts(with: "echo-") else { |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + let pair = metadataPair.split(separator: "=") |
| 46 | + if pair.count == 2, let key = pair.first.map(String.init), |
| 47 | + let value = pair.last.map(String.init) |
| 48 | + { |
| 49 | + requestMetadata.addString(value, forKey: key) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + print("unary → \(requestMetadata)") |
| 54 | + try await echo.get( |
| 55 | + Echo_EchoRequest(), |
| 56 | + metadata: requestMetadata |
| 57 | + ) { response in |
| 58 | + print("unary ← Initial metadata: \(response.metadata.echoPairs)") |
| 59 | + print("unary ← Trailing metadata: \(response.trailingMetadata)") |
| 60 | + } |
| 61 | + |
| 62 | + print("bidirectional → \(requestMetadata)") |
| 63 | + try await echo.update( |
| 64 | + request: .init( |
| 65 | + metadata: requestMetadata, |
| 66 | + producer: { _ in } |
| 67 | + ) |
| 68 | + ) { response in |
| 69 | + print("bidirectional ← Initial metadata: \(response.metadata.echoPairs)") |
| 70 | + for try await part in try response.accepted.get().bodyParts { |
| 71 | + switch part { |
| 72 | + case .trailingMetadata(let trailingMetadata): |
| 73 | + print("bidirectional ← Trailing metadata: \(trailingMetadata.echoPairs)") |
| 74 | + |
| 75 | + case .message: |
| 76 | + () |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments