ClientInterceptor.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2023, 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. // - FIXME: Update example and documentation to show how to register an interceptor.
  17. /// A type that intercepts requests and response for clients.
  18. ///
  19. /// Interceptors allow you to inspect and modify requests and responses. Requests are intercepted
  20. /// before they are handed to a transport and responses are intercepted after they have been
  21. /// received from the transport. They are typically used for cross-cutting concerns like injecting
  22. /// metadata, validating messages, logging additional data, and tracing.
  23. ///
  24. /// Interceptors are registered with the client via ``ConditionalInterceptor``s.
  25. /// You may register them for all services registered with a server, for RPCs directed to specific services, or
  26. /// for RPCs directed to specific methods. If you need to modify the behavior of an interceptor on a
  27. /// per-RPC basis in more detail, then you can use the ``ClientContext/descriptor`` to determine
  28. /// which RPC is being called and conditionalise behavior accordingly.
  29. ///
  30. /// Some examples of simple interceptors follow.
  31. ///
  32. /// ## Metadata injection
  33. ///
  34. /// A common use-case for client interceptors is injecting metadata into a request.
  35. ///
  36. /// ```swift
  37. /// struct MetadataInjectingClientInterceptor: ClientInterceptor {
  38. /// let key: String
  39. /// let fetchMetadata: @Sendable () async -> String
  40. ///
  41. /// func intercept<Input: Sendable, Output: Sendable>(
  42. /// request: StreamingClientRequest<Input>,
  43. /// context: ClientContext,
  44. /// next: @Sendable (
  45. /// _ request: StreamingClientRequest<Input>,
  46. /// _ context: ClientContext
  47. /// ) async throws -> StreamingClientResponse<Output>
  48. /// ) async throws -> StreamingClientResponse<Output> {
  49. /// // Fetch the metadata value and attach it.
  50. /// let value = await self.fetchMetadata()
  51. /// var request = request
  52. /// request.metadata[self.key] = value
  53. ///
  54. /// // Forward the request to the next interceptor.
  55. /// return try await next(request, context)
  56. /// }
  57. /// }
  58. /// ```
  59. ///
  60. /// Interceptors can also be used to print information about RPCs.
  61. ///
  62. /// ## Logging interceptor
  63. ///
  64. /// ```swift
  65. /// struct LoggingClientInterceptor: ClientInterceptor {
  66. /// func intercept<Input: Sendable, Output: Sendable>(
  67. /// request: StreamingClientRequest<Input>,
  68. /// context: ClientContext,
  69. /// next: @Sendable (
  70. /// _ request: StreamingClientRequest<Input>,
  71. /// _ context: ClientContext
  72. /// ) async throws -> StreamingClientResponse<Output>
  73. /// ) async throws -> StreamingClientResponse<Output> {
  74. /// print("Invoking method '\(context.descriptor)'")
  75. /// let response = try await next(request, context)
  76. ///
  77. /// switch response.accepted {
  78. /// case .success:
  79. /// print("Server accepted RPC for processing")
  80. /// case .failure(let error):
  81. /// print("Server rejected RPC with error '\(error)'")
  82. /// }
  83. ///
  84. /// return response
  85. /// }
  86. /// }
  87. /// ```
  88. ///
  89. /// For server-side interceptors see ``ServerInterceptor``.
  90. public protocol ClientInterceptor: Sendable {
  91. /// Intercept a request object.
  92. ///
  93. /// - Parameters:
  94. /// - request: The request object.
  95. /// - context: Additional context about the request, including a descriptor
  96. /// of the method being called.
  97. /// - next: A closure to invoke to hand off the request and context to the next
  98. /// interceptor in the chain.
  99. /// - Returns: A response object.
  100. func intercept<Input: Sendable, Output: Sendable>(
  101. request: StreamingClientRequest<Input>,
  102. context: ClientContext,
  103. next: (
  104. _ request: StreamingClientRequest<Input>,
  105. _ context: ClientContext
  106. ) async throws -> StreamingClientResponse<Output>
  107. ) async throws -> StreamingClientResponse<Output>
  108. }