Beispiel #1
0
        /// <inheritdoc />
        private protected override ChannelBase CreateChannelImpl(ServiceMetadata serviceMetadata, string endpoint, ChannelCredentials credentials, GrpcChannelOptions options)
        {
            var grpcNetClientOptions = ConvertOptions(credentials, options);

            _optionsConfigurationAction?.Invoke(grpcNetClientOptions);
            var address = ConvertEndpoint(endpoint);

            return(GrpcChannel.ForAddress(address, grpcNetClientOptions));
        }
 /// <summary>
 /// Creates a channel for the given endpoint, using the given credentials and options.
 /// </summary>
 /// <param name="serviceMetadata">The metadata for the service. Must not be null.</param>
 /// <param name="endpoint">The endpoint to connect to. Must not be null.</param>
 /// <param name="credentials">The channel credentials to use. Must not be null.</param>
 /// <param name="options">The channel options to use. Must not be null.</param>
 /// <returns>A channel for the specified settings.</returns>
 internal ChannelBase CreateChannel(ServiceMetadata serviceMetadata, string endpoint, ChannelCredentials credentials, GrpcChannelOptions options)
 {
     if (!SupportsApi(serviceMetadata))
     {
         throw new ArgumentException($"API {serviceMetadata.Name} does not have any transports in common with {GetType().Name}");
     }
     GaxPreconditions.CheckNotNull(endpoint, nameof(endpoint));
     GaxPreconditions.CheckNotNull(credentials, nameof(credentials));
     GaxPreconditions.CheckNotNull(options, nameof(options));
     return(CreateChannelImpl(serviceMetadata, endpoint, credentials, options));
 }
        /// <summary>
        /// Creates a cache which will apply the specified scopes to the default application credentials
        /// if they require any.
        /// </summary>
        /// <param name="serviceMetadata">The metadata of the service the credentials will be used with. Must not be null.</param>
        internal DefaultChannelCredentialsCache(ServiceMetadata serviceMetadata)
        {
            UseJwtAccessWithScopes = serviceMetadata.SupportsScopedJwts;

            // In theory, we don't actually need to store the scopes as field in this class. We could capture a local variable here.
            // However, it won't be any more efficient, and having the scopes easily available when debugging could be handy.
            _scopes = serviceMetadata.DefaultScopes;
            _lazyScopedDefaultChannelCredentials =
                new Lazy <Task <ChannelCredentials> >(() => Task.Run(async() =>
            {
                var appDefaultCredentials = await GoogleCredential.GetApplicationDefaultAsync().ConfigureAwait(false);
                if (appDefaultCredentials.IsCreateScopedRequired)
                {
                    appDefaultCredentials = appDefaultCredentials.CreateScoped(_scopes);
                }

                if (appDefaultCredentials.UnderlyingCredential is ServiceAccountCredential serviceCredential &&
                    serviceCredential.UseJwtAccessWithScopes != UseJwtAccessWithScopes)
                {
                    appDefaultCredentials = GoogleCredential.FromServiceAccountCredential(serviceCredential.WithUseJwtAccessWithScopes(UseJwtAccessWithScopes));
                }
                return(appDefaultCredentials.ToChannelCredentials());
            }));
        }
 /// <inheritdoc />
 private protected override ChannelBase CreateChannelImpl(ServiceMetadata serviceMetadata, string endpoint, ChannelCredentials credentials, GrpcChannelOptions options) =>
 channelFactory.Value.Invoke(endpoint, credentials, options);
 /// <summary>
 /// Creates a channel for the given endpoint, using the given credentials and options. All parameters
 /// are pre-validated to be non-null.
 /// </summary>
 /// <param name="apiMetadata"></param>
 /// <param name="endpoint">The endpoint to connect to. Will not be null.</param>
 /// <param name="credentials">The channel credentials to use. Will not be null.</param>
 /// <param name="options">The channel options to use. Will not be null.</param>
 /// <returns>A channel for the specified settings.</returns>
 private protected abstract ChannelBase CreateChannelImpl(ServiceMetadata apiMetadata, string endpoint, ChannelCredentials credentials, GrpcChannelOptions options);
 /// <summary>
 /// Returns a fallback provider suitable for the given API
 /// </summary>
 /// <param name="serviceMetadata">The descriptor of the API. Must not be null.</param>
 /// <returns>A suitable GrpcAdapter for the given API, preferring the use of the binary gRPC transport where available.</returns>
 internal static GrpcAdapter GetFallbackAdapter(ServiceMetadata serviceMetadata) =>
 // TODO: Do we need some way of indicating a preference, if the service supports both Rest and Grpc?
 // Probably not: the user code can always specify the adapter in the builder
 serviceMetadata.Transports.HasFlag(ApiTransports.Grpc) ? s_defaultGrpcTransportFactory.Value
     : serviceMetadata.Transports.HasFlag(ApiTransports.Rest) ? RestGrpcAdapter.Default
     : throw new ArgumentException("No known adapters support the given service.");
        // TODO: potentially expose either or both of the methods below if we want, or a
        // "get the first adapter that supports this service, from the following list, or a fallback"
        // method.

        /// <summary>
        /// Returns whether or not this adapter supports the specified service.
        /// </summary>
        /// <param name="serviceMetadata">The service metadata. Must not be null.</param>
        /// <returns><c>true</c> if this adapter supports the given service; <c>false</c> otherwise.</returns>
        internal bool SupportsApi(ServiceMetadata serviceMetadata)
        {
            GaxPreconditions.CheckNotNull(serviceMetadata, nameof(serviceMetadata));
            return((serviceMetadata.Transports & _supportedTransports) != 0);
        }
 /// <summary>
 /// Creates a channel pool which will use the given service metadata to determine scopes and the use of self-signed JWTs.
 /// </summary>
 /// <param name="serviceMetadata">The metadata for the service that this pool will be used with. Must not be null.</param>
 public ChannelPool(ServiceMetadata serviceMetadata)
 {
     _serviceMetadata = GaxPreconditions.CheckNotNull(serviceMetadata, nameof(serviceMetadata));
     _credentialCache = new DefaultChannelCredentialsCache(serviceMetadata);
 }