|
| 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 | + |
| 17 | +/// A `ClientInterceptorPipelineOperation` describes to which RPCs a client interceptor should be applied. |
| 18 | +/// |
| 19 | +/// You can configure a client interceptor to be applied to: |
| 20 | +/// - all RPCs and services; |
| 21 | +/// - requests directed only to specific services; or |
| 22 | +/// - requests directed only to specific methods (of a specific service). |
| 23 | +/// |
| 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. |
| 29 | + public struct Subject: Sendable { |
| 30 | + internal enum Wrapped: Sendable { |
| 31 | + case all |
| 32 | + case services(Set<ServiceDescriptor>) |
| 33 | + case methods(Set<MethodDescriptor>) |
| 34 | + } |
| 35 | + |
| 36 | + private let wrapped: Wrapped |
| 37 | + |
| 38 | + /// An operation subject specifying an interceptor that applies to all RPCs across all services will be registered with this client. |
| 39 | + public static var all: Self { .init(wrapped: .all) } |
| 40 | + |
| 41 | + /// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified services. |
| 42 | + /// - Parameters: |
| 43 | + /// - services: The list of service names for which this interceptor should intercept RPCs. |
| 44 | + /// - Returns: A ``ClientInterceptorPipelineOperation``. |
| 45 | + public static func services(_ services: Set<ServiceDescriptor>) -> Self { |
| 46 | + Self(wrapped: .services(services)) |
| 47 | + } |
| 48 | + |
| 49 | + /// An operation subject specifying an interceptor that will be applied only to RPCs directed to the specified service methods. |
| 50 | + /// - Parameters: |
| 51 | + /// - methods: The list of method descriptors for which this interceptor should intercept RPCs. |
| 52 | + /// - Returns: A ``ClientInterceptorPipelineOperation``. |
| 53 | + public static func methods(_ methods: Set<MethodDescriptor>) -> Self { |
| 54 | + Self(wrapped: .methods(methods)) |
| 55 | + } |
| 56 | + |
| 57 | + @usableFromInline |
| 58 | + internal func applies(to descriptor: MethodDescriptor) -> Bool { |
| 59 | + switch self.wrapped { |
| 60 | + case .all: |
| 61 | + return true |
| 62 | + |
| 63 | + case .services(let services): |
| 64 | + return services.map({ $0.fullyQualifiedService }).contains(descriptor.service) |
| 65 | + |
| 66 | + case .methods(let methods): |
| 67 | + return methods.contains(descriptor) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// The interceptor specified for this operation. |
| 73 | + public let interceptor: any ClientInterceptor |
| 74 | + |
| 75 | + @usableFromInline |
| 76 | + internal let subject: Subject |
| 77 | + |
| 78 | + private init(interceptor: any ClientInterceptor, appliesTo: Subject) { |
| 79 | + 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) |
| 90 | + } |
| 91 | + |
| 92 | + /// Returns whether this ``ClientInterceptorPipelineOperation`` applies to the given `descriptor`. |
| 93 | + /// - Parameter descriptor: A ``MethodDescriptor`` for which to test whether this interceptor applies. |
| 94 | + /// - Returns: `true` if this interceptor applies to the given `descriptor`, or `false` otherwise. |
| 95 | + @inlinable |
| 96 | + internal func applies(to descriptor: MethodDescriptor) -> Bool { |
| 97 | + self.subject.applies(to: descriptor) |
| 98 | + } |
| 99 | +} |
0 commit comments