EchoInterceptorFactories.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // MARK: - Client
  19. internal final class EchoClientInterceptors: Echo_EchoClientInterceptorFactoryProtocol {
  20. internal typealias Factory = () -> ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>
  21. private var factories: [Factory] = []
  22. internal init(_ factories: Factory...) {
  23. self.factories = factories
  24. }
  25. internal func register(_ factory: @escaping Factory) {
  26. self.factories.append(factory)
  27. }
  28. private func makeInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  29. return self.factories.map { $0() }
  30. }
  31. func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  32. return self.makeInterceptors()
  33. }
  34. func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  35. return self.makeInterceptors()
  36. }
  37. func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  38. return self.makeInterceptors()
  39. }
  40. func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  41. return self.makeInterceptors()
  42. }
  43. }
  44. // MARK: - Server
  45. internal final class EchoServerInterceptors: Echo_EchoServerInterceptorFactoryProtocol {
  46. internal typealias Factory = () -> ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>
  47. private var factories: [Factory] = []
  48. internal init(_ factories: Factory...) {
  49. self.factories = factories
  50. }
  51. internal func register(_ factory: @escaping Factory) {
  52. self.factories.append(factory)
  53. }
  54. private func makeInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  55. return self.factories.map { $0() }
  56. }
  57. func makeGetInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  58. return self.makeInterceptors()
  59. }
  60. func makeExpandInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  61. return self.makeInterceptors()
  62. }
  63. func makeCollectInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  64. return self.makeInterceptors()
  65. }
  66. func makeUpdateInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
  67. return self.makeInterceptors()
  68. }
  69. }