EchoInterceptorFactories.swift 2.8 KB

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