public override AsyncClientStreamingCall <TRequest, TResponse> AsyncClientStreamingCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options) { options = options.Headers == null?options.WithHeaders(new Metadata()) : options; var _context = new ClientInterceptorContext <TRequest, TResponse>(method, host, options); var rspCnt = Calls.AsyncClientStreamingCall(CreateCall(method, host, options)); var tracingRequestStream = new TracingClientStreamWriter <TRequest, TResponse>(rspCnt.RequestStream, _context, _tracer.Request); var rspAsync = rspCnt.ResponseAsync.ContinueWith(rspTask => { try { var response = rspTask.Result; _tracer.Response(response, _context); _tracer.Finish(_context); return(response); } catch (AggregateException ex) { _tracer.Exception(_context, ex.InnerException, null); throw ex.InnerException; } }); return(new AsyncClientStreamingCall <TRequest, TResponse>(tracingRequestStream, rspAsync, rspCnt.ResponseHeadersAsync, rspCnt.GetStatus, rspCnt.GetTrailers, rspCnt.Dispose)); }
public MogmogConnection(string hostname, int channelId, bool saveAccessCode) { this.ChannelId = channelId; this.SaveAccessCode = saveAccessCode; this.tokenSource = new CancellationTokenSource(); this.channel = GrpcChannel.ForAddress(hostname); this.chatClient = new ChatServiceClient(channel); var serverInfo = this.chatClient.GetChatServerInfo(new ReqChatServerInfo()); var flags = (ServerFlags)serverInfo.Flags; Mogger.Log($"Server flags for {hostname}: {flags}"); var callOptions = new CallOptions() .WithCancellationToken(this.tokenSource.Token) .WithDeadline(DateTime.UtcNow.AddMinutes(1)) .WithWaitForReady(); if (flags.HasFlag(ServerFlags.RequiresDiscordOAuth2)) { oAuth2 = new DiscordOAuth2(); oAuth2.Authenticate(serverInfo.ServerId); var headers = new Metadata { new Entry("code", oAuth2.OAuth2Code), }; callOptions = callOptions.WithHeaders(headers); } this.chatStream = this.chatClient.Chat(callOptions); _ = ChatLoop(this.tokenSource.Token); }
private static CallOptions ProcessOptions(CallOptions options) { Metadata headers = options.Headers ?? new Metadata(); CallOptions updatedOptions = options.WithHeaders(TrackingHelper.ProcessHeaders(headers)); return(updatedOptions); }
public override AsyncDuplexStreamingCall <TRequest, TResponse> AsyncDuplexStreamingCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options) { options = options.Headers == null?options.WithHeaders(new Metadata()) : options; var _context = new ClientInterceptorContext <TRequest, TResponse>(method, host, options); var rspCnt = Calls.AsyncDuplexStreamingCall(CreateCall(method, host, options)); var tracingRequestStream = new TracingClientStreamWriter <TRequest, TResponse>(rspCnt.RequestStream, _context, _tracer.Request); var tracingResponseStream = new TracingAsyncClientStreamReader <TResponse, TRequest>(rspCnt.ResponseStream, _context, _tracer.Response, _tracer.Finish, _tracer.Exception); return(new AsyncDuplexStreamingCall <TRequest, TResponse>(tracingRequestStream, tracingResponseStream, rspCnt.ResponseHeadersAsync, rspCnt.GetStatus, rspCnt.GetTrailers, rspCnt.Dispose)); }
internal static CallOptions WithCorrelationHeader(this CallOptions options, IInstrumentationContext context) { options = options.Headers == null ? options.WithHeaders(new Metadata()) : options; (string name, string value) = context.GetCorrelationHeader(); options.Headers.Add(new Metadata.Entry(name, value)); return(options); }
public CallOptions PreAction <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { if (options.Headers == null) { options = options.WithHeaders(new Metadata()); } if (!options.Deadline.HasValue) { options.Headers.Add(TIMEOUT_KEY, $"{this.TimoutMilliseconds}m"); } return(options); }
public static CallOptions Init(this CallOptions options, GrpcClientConfig config, bool noAuth) { var auth = noAuth ? null : !string.IsNullOrEmpty(config.UserName) && !string.IsNullOrEmpty(config.Password) ? "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(config.UserName + ":" + config.Password)) : !string.IsNullOrEmpty(config.BearerToken) ? "Bearer " + config.BearerToken : !string.IsNullOrEmpty(config.SessionId) ? nameof(config.SessionId) : null; if (config.Headers.Count > 0 || auth != null || config.UserAgent != null) { var headers = options.Headers; if (headers == null) { options = options.WithHeaders(headers = new Metadata()); } foreach (var entry in config.Headers) { headers.Add(entry); } if (auth != null) { if (auth == nameof(config.SessionId)) { headers.Set(GrpcClientConfig.Keywords.HeaderSessionId, config.SessionId); } else { headers.Set(HttpHeaders.Authorization, auth); } } if (config.UserAgent != null) { headers.Set(HttpHeaders.UserAgent, config.UserAgent); } } return(options); }
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 ApplyConfigToCallOptions(CallOptions callOptions) { if (callOptions.Headers == null) { // Add empty metadata to options: callOptions = callOptions.WithHeaders(new Metadata()); } if (_configuration.WaitForReady && callOptions.IsWaitForReady != _configuration.WaitForReady) { callOptions = callOptions.WithWaitForReady(); } if (_configuration.FallbackCancellationToken != default && callOptions.CancellationToken != _configuration.FallbackCancellationToken) { callOptions = callOptions.WithCancellationToken(_configuration.FallbackCancellationToken); } return(callOptions); }
public override TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { options = options.Headers == null?options.WithHeaders(new Metadata()) : options; var _context = new ClientInterceptorContext <TRequest, TResponse>(method, host, options); try { _tracer.Request(request, _context); var response = Calls.BlockingUnaryCall(CreateCall(method, host, options), request); _tracer.Response(response, _context); _tracer.Finish(_context); return(response); } catch (Exception ex) { _tracer.Exception(_context, ex, request); throw; } }
/// <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); }
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 token = new CancellationTokenSource().Token; Assert.AreEqual(token, options.WithCancellationToken(token).CancellationToken); // Change 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); }