DelegatingClientInterceptor.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2021, 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 EchoModel
  17. import GRPC
  18. import NIOCore
  19. import SwiftProtobuf
  20. /// A client interceptor which delegates the implementation of `send` and `receive` to callbacks.
  21. final class DelegatingClientInterceptor<
  22. Request: Message,
  23. Response: Message
  24. >: ClientInterceptor<Request, Response> {
  25. typealias RequestPart = GRPCClientRequestPart<Request>
  26. typealias ResponsePart = GRPCClientResponsePart<Response>
  27. typealias Context = ClientInterceptorContext<Request, Response>
  28. typealias OnSend = (RequestPart, EventLoopPromise<Void>?, Context) -> Void
  29. typealias OnReceive = (ResponsePart, Context) -> Void
  30. private let onSend: OnSend
  31. private let onReceive: OnReceive
  32. init(
  33. onSend: @escaping OnSend = { part, promise, context in context.send(part, promise: promise) },
  34. onReceive: @escaping OnReceive = { part, context in context.receive(part) }
  35. ) {
  36. self.onSend = onSend
  37. self.onReceive = onReceive
  38. }
  39. override func send(
  40. _ part: GRPCClientRequestPart<Request>,
  41. promise: EventLoopPromise<Void>?,
  42. context: ClientInterceptorContext<Request, Response>
  43. ) {
  44. self.onSend(part, promise, context)
  45. }
  46. override func receive(
  47. _ part: GRPCClientResponsePart<Response>,
  48. context: ClientInterceptorContext<Request, Response>
  49. ) {
  50. self.onReceive(part, context)
  51. }
  52. }
  53. final class DelegatingEchoClientInterceptorFactory: Echo_EchoClientInterceptorFactoryProtocol {
  54. typealias OnSend = DelegatingClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>.OnSend
  55. let interceptor: DelegatingClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>
  56. init(onSend: @escaping OnSend) {
  57. self.interceptor = DelegatingClientInterceptor(onSend: onSend)
  58. }
  59. func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  60. return [self.interceptor]
  61. }
  62. func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  63. return [self.interceptor]
  64. }
  65. func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  66. return [self.interceptor]
  67. }
  68. func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  69. return [self.interceptor]
  70. }
  71. }