ClientInterceptorPipelineOperation.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2024, 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. /// A `ClientInterceptorPipelineOperation` describes to which RPCs a client interceptor should be applied.
  17. ///
  18. /// You can configure a client interceptor to be applied to:
  19. /// - all RPCs and services;
  20. /// - requests directed only to specific services; or
  21. /// - requests directed only to specific methods (of a specific service).
  22. ///
  23. /// - SeeAlso: ``ClientInterceptor`` for more information on client interceptors, and
  24. /// ``ServerInterceptorPipelineOperation`` for the server-side version of this type.
  25. public struct ClientInterceptorPipelineOperation: Sendable {
  26. /// The subject of a ``ClientInterceptorPipelineOperation``.
  27. /// The subject of an interceptor can either be all services and methods, only specific services, or only specific methods.
  28. public struct Subject: Sendable {
  29. internal enum Wrapped: Sendable {
  30. case all
  31. case services(Set<ServiceDescriptor>)
  32. case methods(Set<MethodDescriptor>)
  33. }
  34. private let wrapped: Wrapped
  35. /// An operation subject specifying an interceptor that applies to all RPCs across all services will be registered with this client.
  36. public static var all: Self { .init(wrapped: .all) }
  37. /// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified services.
  38. /// - Parameters:
  39. /// - services: The list of service names for which this interceptor should intercept RPCs.
  40. /// - Returns: A ``ClientInterceptorPipelineOperation``.
  41. public static func services(_ services: Set<ServiceDescriptor>) -> Self {
  42. Self(wrapped: .services(services))
  43. }
  44. /// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified service methods.
  45. /// - Parameters:
  46. /// - methods: The list of method descriptors for which this interceptor should intercept RPCs.
  47. /// - Returns: A ``ClientInterceptorPipelineOperation``.
  48. public static func methods(_ methods: Set<MethodDescriptor>) -> Self {
  49. Self(wrapped: .methods(methods))
  50. }
  51. @usableFromInline
  52. internal func applies(to descriptor: MethodDescriptor) -> Bool {
  53. switch self.wrapped {
  54. case .all:
  55. return true
  56. case .services(let services):
  57. return services.contains(descriptor.service)
  58. case .methods(let methods):
  59. return methods.contains(descriptor)
  60. }
  61. }
  62. }
  63. /// The interceptor specified for this operation.
  64. public let interceptor: any ClientInterceptor
  65. @usableFromInline
  66. internal let subject: Subject
  67. private init(interceptor: any ClientInterceptor, appliesTo: Subject) {
  68. self.interceptor = interceptor
  69. self.subject = appliesTo
  70. }
  71. /// Create an operation, specifying which ``ClientInterceptor`` to apply and to which ``Subject``.
  72. /// - Parameters:
  73. /// - interceptor: The ``ClientInterceptor`` to register with the client.
  74. /// - subject: The ``Subject`` to which the `interceptor` applies.
  75. /// - Returns: A ``ClientInterceptorPipelineOperation``.
  76. public static func apply(_ interceptor: any ClientInterceptor, to subject: Subject) -> Self {
  77. Self(interceptor: interceptor, appliesTo: subject)
  78. }
  79. /// Returns whether this ``ClientInterceptorPipelineOperation`` applies to the given `descriptor`.
  80. /// - Parameter descriptor: A ``MethodDescriptor`` for which to test whether this interceptor applies.
  81. /// - Returns: `true` if this interceptor applies to the given `descriptor`, or `false` otherwise.
  82. @inlinable
  83. internal func applies(to descriptor: MethodDescriptor) -> Bool {
  84. self.subject.applies(to: descriptor)
  85. }
  86. }