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