private static CallOptions ResolveCallOptionsCredentials(CallOptions callOptions, CallCredentials credentials)
        {
            if (callOptions.Credentials != null)
            {
                credentials = CallCredentials.Compose(callOptions.Credentials, credentials);
            }

            return(callOptions.WithCredentials(credentials));
        }
Esempio n. 2
0
        /// <summary>
        /// Construct with an optional access token to be specified on every call to the client class
        /// </summary>
        /// <param name="client">The grpc client class</param>
        /// <param name="accessToken">An optional access token</param>
        public ClientStub(TClient client, string accessToken = null)
        {
            _client      = client;
            _callOptions = new CallOptions();

            if (!string.IsNullOrEmpty(accessToken))
            {
                _accessTokens = new HashSet <string>(new[] { accessToken });
                _callOptions  = _callOptions.WithCredentials(LedgerCallCredentials.MakeCallCredentials(accessToken));
            }
        }
Esempio n. 3
0
        public void WithMethods()
        {
            var options = new CallOptions();

            var metadata = new Metadata();

            Assert.AreSame(metadata, options.WithHeaders(metadata).Headers);

            var deadline = DateTime.UtcNow;

            Assert.AreEqual(deadline, options.WithDeadline(deadline).Deadline.Value);

            var cancellationToken = new CancellationTokenSource().Token;

            Assert.AreEqual(cancellationToken, options.WithCancellationToken(cancellationToken).CancellationToken);

            var writeOptions = new WriteOptions();

            Assert.AreSame(writeOptions, options.WithWriteOptions(writeOptions).WriteOptions);

            var propagationToken = new ContextPropagationToken(CallSafeHandle.NullInstance, DateTime.UtcNow,
                                                               CancellationToken.None, ContextPropagationOptions.Default);

            Assert.AreSame(propagationToken, options.WithPropagationToken(propagationToken).PropagationToken);

            var credentials = new FakeCallCredentials();

            Assert.AreSame(credentials, options.WithCredentials(credentials).Credentials);

            var flags = CallFlags.WaitForReady | CallFlags.CacheableRequest;

            Assert.AreEqual(flags, options.WithFlags(flags).Flags);

            // Check that the original instance is unchanged.
            Assert.IsNull(options.Headers);
            Assert.IsNull(options.Deadline);
            Assert.AreEqual(CancellationToken.None, options.CancellationToken);
            Assert.IsNull(options.WriteOptions);
            Assert.IsNull(options.PropagationToken);
            Assert.IsNull(options.Credentials);
            Assert.AreEqual(default(CallFlags), options.Flags);
        }
        private CallOptions PrepareCallOptions(CancellationToken cancellationToken)
        {
            var callOptions = new CallOptions().WithCancellationToken(cancellationToken);

            if (callCredentials != null)
            {
                callOptions.WithCredentials(callCredentials);
            }

            if (deadlineOnMessages.HasValue)
            {
                var date = clock.GetUtcNow();
                date = date + deadlineOnMessages.Value;
                callOptions.WithDeadline(date);
            }

            if (writeOptions != null)
            {
                callOptions.WithWriteOptions(writeOptions);
            }

            return(callOptions);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a populated call options instance based on the provided parameters
        /// </summary>
        /// <param name="metadata">The metadata to be added for the call.</param>
        /// <param name="credentials">The credentials associated with the call</param>
        /// <param name="timeout">The timespan to add to the current UTC time to act as the call deadline</param>
        /// <param name="cancellationToken">A best effort cancellation token</param>
        /// <returns>A call options instace</returns>
        public static CallOptions GetCallOptions(Metadata metadata = null, CallCredentials credentials = null, TimeSpan?timeout = null, CancellationToken cancellationToken = default)
        {
            var options = new CallOptions();

            if (metadata != null)
            {
                options = options.WithHeaders(metadata);
            }
            if (cancellationToken != default)
            {
                options = options.WithCancellationToken(cancellationToken);
            }
            if (timeout.HasValue)
            {
                options = options.WithDeadline(DateTime.UtcNow.Add(timeout.Value));
            }
            if (credentials != null)
            {
                options = options.WithCredentials(credentials);
            }

            return(options);
        }
Esempio n. 6
0
 /// <summary>
 /// A copy constructor for building a new object with an extra access token
 /// </summary>
 /// <param name="client"></param>
 /// <param name="accessTokens"></param>
 private ClientStub(TClient client, HashSet <string> accessTokens)
 {
     _client       = client;
     _accessTokens = accessTokens;
     _callOptions  = _callOptions.WithCredentials(LedgerCallCredentials.MakeCallCredentials(_accessTokens));
 }