OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Text.RegularExpressions; |
| 36 using System.Threading.Tasks; |
| 37 |
| 38 namespace Grpc.Core |
| 39 { |
| 40 /// <summary> |
| 41 /// Interceptor for call headers. |
| 42 /// </summary> |
| 43 /// <remarks>Header interceptor is no longer to recommented way to perform a
uthentication. |
| 44 /// For header (initial metadata) based auth such as OAuth2 or JWT access to
ken, use <see cref="MetadataCredentials"/>. |
| 45 /// </remarks> |
| 46 public delegate void HeaderInterceptor(IMethod method, Metadata metadata); |
| 47 |
| 48 /// <summary> |
| 49 /// Base class for client-side stubs. |
| 50 /// </summary> |
| 51 public abstract class ClientBase |
| 52 { |
| 53 readonly Channel channel; |
| 54 |
| 55 /// <summary> |
| 56 /// Initializes a new instance of <c>ClientBase</c> class. |
| 57 /// </summary> |
| 58 /// <param name="channel">The channel to use for remote call invocation.
</param> |
| 59 public ClientBase(Channel channel) |
| 60 { |
| 61 this.channel = channel; |
| 62 } |
| 63 |
| 64 /// <summary> |
| 65 /// Can be used to register a custom header interceptor. |
| 66 /// The interceptor is invoked each time a new call on this client is st
arted. |
| 67 /// It is not recommented to use header interceptor to add auth headers
to RPC calls. |
| 68 /// </summary> |
| 69 /// <seealso cref="HeaderInterceptor"/> |
| 70 public HeaderInterceptor HeaderInterceptor |
| 71 { |
| 72 get; |
| 73 set; |
| 74 } |
| 75 |
| 76 /// <summary> |
| 77 /// gRPC supports multiple "hosts" being served by a single server. |
| 78 /// This property can be used to set the target host explicitly. |
| 79 /// By default, this will be set to <c>null</c> with the meaning |
| 80 /// "use default host". |
| 81 /// </summary> |
| 82 public string Host |
| 83 { |
| 84 get; |
| 85 set; |
| 86 } |
| 87 |
| 88 /// <summary> |
| 89 /// Channel associated with this client. |
| 90 /// </summary> |
| 91 public Channel Channel |
| 92 { |
| 93 get |
| 94 { |
| 95 return this.channel; |
| 96 } |
| 97 } |
| 98 |
| 99 /// <summary> |
| 100 /// Creates a new call to given method. |
| 101 /// </summary> |
| 102 /// <param name="method">The method to invoke.</param> |
| 103 /// <param name="options">The call options.</param> |
| 104 /// <typeparam name="TRequest">Request message type.</typeparam> |
| 105 /// <typeparam name="TResponse">Response message type.</typeparam> |
| 106 /// <returns>The call invocation details.</returns> |
| 107 protected CallInvocationDetails<TRequest, TResponse> CreateCall<TRequest
, TResponse>(Method<TRequest, TResponse> method, CallOptions options) |
| 108 where TRequest : class |
| 109 where TResponse : class |
| 110 { |
| 111 var interceptor = HeaderInterceptor; |
| 112 if (interceptor != null) |
| 113 { |
| 114 if (options.Headers == null) |
| 115 { |
| 116 options = options.WithHeaders(new Metadata()); |
| 117 } |
| 118 interceptor(method, options.Headers); |
| 119 } |
| 120 return new CallInvocationDetails<TRequest, TResponse>(channel, metho
d, Host, options); |
| 121 } |
| 122 } |
| 123 } |
OLD | NEW |