|
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The gRPC Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#endregion |
| 18 | + |
| 19 | +using Grpc.Core; |
| 20 | + |
| 21 | +namespace Grpc.Net.ClientFactory.Internal |
| 22 | +{ |
| 23 | + internal sealed class CallOptionsConfigurationInvoker : CallInvoker |
| 24 | + { |
| 25 | + private readonly IServiceProvider _serviceProvider; |
| 26 | + private readonly IList<Action<CallOptionsContext>> _callOptionsActions; |
| 27 | + private readonly CallInvoker _innerInvoker; |
| 28 | + |
| 29 | + public CallOptionsConfigurationInvoker(CallInvoker innerInvoker, IList<Action<CallOptionsContext>> callOptionsActions, IServiceProvider serviceProvider) |
| 30 | + { |
| 31 | + _innerInvoker = innerInvoker; |
| 32 | + _callOptionsActions = callOptionsActions; |
| 33 | + _serviceProvider = serviceProvider; |
| 34 | + } |
| 35 | + |
| 36 | + private CallOptions ResolveCallOptions(CallOptions callOptions) |
| 37 | + { |
| 38 | + var context = new CallOptionsContext(callOptions, _serviceProvider); |
| 39 | + |
| 40 | + for (var i = 0; i < _callOptionsActions.Count; i++) |
| 41 | + { |
| 42 | + _callOptionsActions[i](context); |
| 43 | + } |
| 44 | + |
| 45 | + return context.CallOptions; |
| 46 | + } |
| 47 | + |
| 48 | + public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options) |
| 49 | + { |
| 50 | + return _innerInvoker.AsyncClientStreamingCall(method, host, ResolveCallOptions(options)); |
| 51 | + } |
| 52 | + |
| 53 | + public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options) |
| 54 | + { |
| 55 | + return _innerInvoker.AsyncDuplexStreamingCall(method, host, ResolveCallOptions(options)); |
| 56 | + } |
| 57 | + |
| 58 | + public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options, TRequest request) |
| 59 | + { |
| 60 | + return _innerInvoker.AsyncServerStreamingCall(method, host, ResolveCallOptions(options), request); |
| 61 | + } |
| 62 | + |
| 63 | + public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options, TRequest request) |
| 64 | + { |
| 65 | + return _innerInvoker.AsyncUnaryCall(method, host, ResolveCallOptions(options), request); |
| 66 | + } |
| 67 | + |
| 68 | + public override TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options, TRequest request) |
| 69 | + { |
| 70 | + return _innerInvoker.BlockingUnaryCall(method, host, ResolveCallOptions(options), request); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments