CallInvokerExtensions.cs 7.0 KB

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