CallInvokerExtensions.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #region Copyright notice and license
  2. // Copyright 2018 gRPC authors.
  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. #endregion
  16. using System;
  17. using System.Linq;
  18. using Grpc.Core.Utils;
  19. namespace Grpc.Core.Interceptors
  20. {
  21. /// <summary>
  22. /// Extends the CallInvoker class to provide the interceptor facility on the client side.
  23. /// This is an EXPERIMENTAL API.
  24. /// </summary>
  25. public static class CallInvokerExtensions
  26. {
  27. /// <summary>
  28. /// Returns a <see cref="Grpc.Core.CallInvoker" /> instance that intercepts
  29. /// the invoker with the given interceptor.
  30. /// </summary>
  31. /// <param name="invoker">The underlying invoker to intercept.</param>
  32. /// <param name="interceptor">The interceptor to intercept calls to the invoker with.</param>
  33. /// <remarks>
  34. /// Multiple interceptors can be added on top of each other by calling
  35. /// "invoker.Intercept(a, b, c)". The order of invocation will be "a", "b", and then "c".
  36. /// Interceptors can be later added to an existing intercepted CallInvoker, effectively
  37. /// building a chain like "invoker.Intercept(c).Intercept(b).Intercept(a)". Note that
  38. /// in this case, the last interceptor added will be the first to take control.
  39. /// </remarks>
  40. public static CallInvoker Intercept(this CallInvoker invoker, Interceptor interceptor)
  41. {
  42. return new InterceptingCallInvoker(invoker, interceptor);
  43. }
  44. /// <summary>
  45. /// Returns a <see cref="Grpc.Core.CallInvoker" /> instance that intercepts
  46. /// the invoker with the given interceptors.
  47. /// </summary>
  48. /// <param name="invoker">The channel to intercept.</param>
  49. /// <param name="interceptors">
  50. /// An array of interceptors to intercept the calls to the invoker with.
  51. /// Control is passed to the interceptors in the order specified.
  52. /// </param>
  53. /// <remarks>
  54. /// Multiple interceptors can be added on top of each other by calling
  55. /// "invoker.Intercept(a, b, c)". The order of invocation will be "a", "b", and then "c".
  56. /// Interceptors can be later added to an existing intercepted CallInvoker, effectively
  57. /// building a chain like "invoker.Intercept(c).Intercept(b).Intercept(a)". Note that
  58. /// in this case, the last interceptor added will be the first to take control.
  59. /// </remarks>
  60. public static CallInvoker Intercept(this CallInvoker invoker, params Interceptor[] interceptors)
  61. {
  62. GrpcPreconditions.CheckNotNull(invoker, nameof(invoker));
  63. GrpcPreconditions.CheckNotNull(interceptors, nameof(interceptors));
  64. foreach (var interceptor in interceptors.Reverse())
  65. {
  66. invoker = Intercept(invoker, interceptor);
  67. }
  68. return invoker;
  69. }
  70. /// <summary>
  71. /// Returns a <see cref="Grpc.Core.CallInvoker" /> instance that intercepts
  72. /// the invoker with the given interceptor.
  73. /// </summary>
  74. /// <param name="invoker">The underlying invoker to intercept.</param>
  75. /// <param name="interceptor">
  76. /// An interceptor delegate that takes the request metadata to be sent with an outgoing call
  77. /// and returns a <see cref="Grpc.Core.Metadata" /> instance that will replace the existing
  78. /// invocation metadata.
  79. /// </param>
  80. /// <remarks>
  81. /// Multiple interceptors can be added on top of each other by
  82. /// building a chain like "invoker.Intercept(c).Intercept(b).Intercept(a)". Note that
  83. /// in this case, the last interceptor added will be the first to take control.
  84. /// </remarks>
  85. public static CallInvoker Intercept(this CallInvoker invoker, Func<Metadata, Metadata> interceptor)
  86. {
  87. return new InterceptingCallInvoker(invoker, new MetadataInterceptor(interceptor));
  88. }
  89. private class MetadataInterceptor : Interceptor
  90. {
  91. readonly Func<Metadata, Metadata> interceptor;
  92. /// <summary>
  93. /// Creates a new instance of MetadataInterceptor given the specified interceptor function.
  94. /// </summary>
  95. public MetadataInterceptor(Func<Metadata, Metadata> interceptor)
  96. {
  97. this.interceptor = GrpcPreconditions.CheckNotNull(interceptor, nameof(interceptor));
  98. }
  99. private ClientInterceptorContext<TRequest, TResponse> GetNewContext<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context)
  100. where TRequest : class
  101. where TResponse : class
  102. {
  103. var metadata = context.Options.Headers ?? new Metadata();
  104. return new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, context.Options.WithHeaders(interceptor(metadata)));
  105. }
  106. public override TResponse BlockingUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, BlockingUnaryCallContinuation<TRequest, TResponse> continuation)
  107. {
  108. return continuation(request, GetNewContext(context));
  109. }
  110. public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
  111. {
  112. return continuation(request, GetNewContext(context));
  113. }
  114. public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
  115. {
  116. return continuation(request, GetNewContext(context));
  117. }
  118. public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
  119. {
  120. return continuation(GetNewContext(context));
  121. }
  122. public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation)
  123. {
  124. return continuation(GetNewContext(context));
  125. }
  126. }
  127. }
  128. }