EchoService.swift 2.6 KB

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