|
| 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 GRPCCore |
| 18 | + |
| 19 | +struct EchoService: Echo_Echo.ServiceProtocol { |
| 20 | + func get( |
| 21 | + request: ServerRequest<Echo_EchoRequest>, |
| 22 | + context: ServerContext |
| 23 | + ) async throws -> ServerResponse<Echo_EchoResponse> { |
| 24 | + let responseMetadata = Metadata(request.metadata.filter({ $0.key.starts(with: "echo-") })) |
| 25 | + return ServerResponse( |
| 26 | + message: .with { $0.text = request.message.text }, |
| 27 | + metadata: responseMetadata, |
| 28 | + trailingMetadata: responseMetadata |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + func collect( |
| 33 | + request: StreamingServerRequest<Echo_EchoRequest>, |
| 34 | + context: ServerContext |
| 35 | + ) async throws -> ServerResponse<Echo_EchoResponse> { |
| 36 | + let responseMetadata = Metadata(request.metadata.filter({ $0.key.starts(with: "echo-") })) |
| 37 | + let messages = try await request.messages.reduce(into: []) { $0.append($1.text) } |
| 38 | + let joined = messages.joined(separator: " ") |
| 39 | + |
| 40 | + return ServerResponse( |
| 41 | + message: .with { $0.text = joined }, |
| 42 | + metadata: responseMetadata, |
| 43 | + trailingMetadata: responseMetadata |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + func expand( |
| 48 | + request: ServerRequest<Echo_EchoRequest>, |
| 49 | + context: ServerContext |
| 50 | + ) async throws -> StreamingServerResponse<Echo_EchoResponse> { |
| 51 | + let responseMetadata = Metadata(request.metadata.filter({ $0.key.starts(with: "echo-") })) |
| 52 | + let parts = request.message.text.split(separator: " ") |
| 53 | + let messages = parts.map { part in Echo_EchoResponse.with { $0.text = String(part) } } |
| 54 | + |
| 55 | + return StreamingServerResponse(metadata: responseMetadata) { writer in |
| 56 | + try await writer.write(contentsOf: messages) |
| 57 | + return responseMetadata |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + func update( |
| 62 | + request: StreamingServerRequest<Echo_EchoRequest>, |
| 63 | + context: ServerContext |
| 64 | + ) async throws -> StreamingServerResponse<Echo_EchoResponse> { |
| 65 | + let responseMetadata = Metadata(request.metadata.filter({ $0.key.starts(with: "echo-") })) |
| 66 | + return StreamingServerResponse(metadata: responseMetadata) { writer in |
| 67 | + for try await message in request.messages { |
| 68 | + try await writer.write(.with { $0.text = message.text }) |
| 69 | + } |
| 70 | + return responseMetadata |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments