1414 * limitations under the License.
1515 */
1616
17- /// A `ClientInterceptorPipelineOperation` describes to which RPCs a client interceptor should be applied.
17+ /// Describes the conditions under which an interceptor should be applied.
1818///
19- /// You can configure a client interceptor to be applied to:
19+ /// You can configure interceptors to be applied to:
2020/// - all RPCs and services;
2121/// - requests directed only to specific services; or
2222/// - requests directed only to specific methods (of a specific service).
2323///
24- /// - SeeAlso: ``ClientInterceptor`` for more information on client interceptors, and
25- /// ``ServerInterceptorPipelineOperation`` for the server-side version of this type.
26- public struct ClientInterceptorPipelineOperation : Sendable {
27- /// The subject of a ``ClientInterceptorPipelineOperation``.
28- /// The subject of an interceptor can either be all services and methods, only specific services, or only specific methods.
24+ /// - SeeAlso: ``ClientInterceptor`` and ``ServerInterceptor`` for more information on client and
25+ /// server interceptors, respectively.
26+ public struct ConditionalInterceptor < Interceptor: Sendable > : Sendable {
2927 public struct Subject : Sendable {
3028 internal enum Wrapped : Sendable {
3129 case all
@@ -41,21 +39,19 @@ public struct ClientInterceptorPipelineOperation: Sendable {
4139 /// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified services.
4240 /// - Parameters:
4341 /// - services: The list of service names for which this interceptor should intercept RPCs.
44- /// - Returns: A ``ClientInterceptorPipelineOperation``.
4542 public static func services( _ services: Set < ServiceDescriptor > ) -> Self {
4643 Self ( wrapped: . services( services) )
4744 }
4845
4946 /// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified service methods.
5047 /// - Parameters:
5148 /// - methods: The list of method descriptors for which this interceptor should intercept RPCs.
52- /// - Returns: A ``ClientInterceptorPipelineOperation``.
5349 public static func methods( _ methods: Set < MethodDescriptor > ) -> Self {
5450 Self ( wrapped: . methods( methods) )
5551 }
5652
5753 @usableFromInline
58- internal func applies( to descriptor: MethodDescriptor ) -> Bool {
54+ package func applies( to descriptor: MethodDescriptor ) -> Bool {
5955 switch self . wrapped {
6056 case . all:
6157 return true
@@ -69,24 +65,15 @@ public struct ClientInterceptorPipelineOperation: Sendable {
6965 }
7066 }
7167
72- /// The interceptor specified for this operation .
73- public let interceptor : any ClientInterceptor
68+ /// The interceptor.
69+ public let interceptor : Interceptor
7470
7571 @usableFromInline
7672 internal let subject : Subject
7773
78- private init ( interceptor: any ClientInterceptor , appliesTo : Subject ) {
74+ fileprivate init ( interceptor: Interceptor , subject : Subject ) {
7975 self . interceptor = interceptor
80- self . subject = appliesTo
81- }
82-
83- /// Create an operation, specifying which ``ClientInterceptor`` to apply and to which ``Subject``.
84- /// - Parameters:
85- /// - interceptor: The ``ClientInterceptor`` to register with the client.
86- /// - subject: The ``Subject`` to which the `interceptor` applies.
87- /// - Returns: A ``ClientInterceptorPipelineOperation``.
88- public static func apply( _ interceptor: any ClientInterceptor , to subject: Subject ) -> Self {
89- Self ( interceptor: interceptor, appliesTo: subject)
76+ self . subject = subject
9077 }
9178
9279 /// Returns whether this ``ClientInterceptorPipelineOperation`` applies to the given `descriptor`.
@@ -97,3 +84,29 @@ public struct ClientInterceptorPipelineOperation: Sendable {
9784 self . subject. applies ( to: descriptor)
9885 }
9986}
87+
88+ extension ConditionalInterceptor where Interceptor == any ClientInterceptor {
89+ /// Create an operation, specifying which ``ClientInterceptor`` to apply and to which ``Subject``.
90+ /// - Parameters:
91+ /// - interceptor: The ``ClientInterceptor`` to register with the client.
92+ /// - subject: The ``Subject`` to which the `interceptor` applies.
93+ public static func apply(
94+ _ interceptor: any ClientInterceptor ,
95+ to subject: Subject
96+ ) -> Self {
97+ Self ( interceptor: interceptor, subject: subject)
98+ }
99+ }
100+
101+ extension ConditionalInterceptor where Interceptor == any ServerInterceptor {
102+ /// Create an operation, specifying which ``ServerInterceptor`` to apply and to which ``Subject``.
103+ /// - Parameters:
104+ /// - interceptor: The ``ServerInterceptor`` to register with the server.
105+ /// - subject: The ``Subject`` to which the `interceptor` applies.
106+ public static func apply(
107+ _ interceptor: any ServerInterceptor ,
108+ to subject: Subject
109+ ) -> Self {
110+ Self ( interceptor: interceptor, subject: subject)
111+ }
112+ }
0 commit comments