Example #1
0
        /// <summary>
        /// Private constructor, because this is a singleton class
        /// </summary>
        internal Configuration()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Schemas.settings));

            Trace.WriteLine($"Loading settings from {_settingsPath}");
            using (StreamReader reader = new StreamReader(_settingsPath))
            {
                Settings = (Schemas.settings)serializer.Deserialize(reader);

                _producerConfig = new Dictionary <string, string>
                {
                    { "bootstrap.servers", Settings.brokerurl },
                    { "retries", Settings.retrycount.ToString() },
                    { "request.timeout.ms", Settings.retrytime.ToString() },
                    { "security.protocol", Settings.securityprotocol },
                    { "ssl.ca.location", Settings.securitycapath },
                    { "ssl.keystore.location", Settings.securitykeystorepath },
                    { "ssl.keystore.password", Settings.securitykeystorepassword },
                };

                _consumerConfig = new Dictionary <string, string>
                {
                    { "bootstrap.servers", Settings.brokerurl },
                    { "group.id", Settings.clientid },
                    { "security.protocol", Settings.securityprotocol },
                    { "ssl.ca.location", Settings.securitycapath },
                    { "ssl.keystore.location", Settings.securitykeystorepath },
                    { "ssl.keystore.password", Settings.securitykeystorepassword },
                };

                SchemaRegistryClient = new Confluent.SchemaRegistry.CachedSchemaRegistryClient(
                    new Confluent.SchemaRegistry.SchemaRegistryConfig()
                {
                    Url = Settings.schemaurl,
                    RequestTimeoutMs = 5000,
                    MaxCachedSchemas = 10,
                }
                    );
            }
        }
        public static async Task <string> ResolveSchema(this ISchemaResolutionOptions options)
        {
            var configuration = new SchemaRegistryConfiguration
            {
                SchemaRegistryUrl = options.RegistryUrl
            };

            using (var client = new SchemaRegistryClient(configuration))
            {
                try
                {
                    if (options.SchemaId is int id)
                    {
                        if (!string.IsNullOrEmpty(options.SchemaSubject) || options.SchemaVersion.HasValue)
                        {
                            throw new ProgramException(message: "When using --id, don’t use --schema or --version.");
                        }

                        try
                        {
                            return(await client.GetSchemaAsync(id));
                        }
                        catch (AggregateException aggregate) when(aggregate.InnerException is SchemaRegistryException inner)
                        {
                            throw new ProgramException(message: $"Failed to retrieve schema with ID {id} ({inner.Message}).", inner: inner);
                        }
                    }
                    else
                    {
                        if (options.SchemaSubject is var subject && string.IsNullOrEmpty(subject))
                        {
                            throw new ProgramException(message: "Either --id or --schema (and optionally --version) must be provided.");
                        }

                        if (options.SchemaVersion is int version)
                        {
                            try
                            {
                                return(await client.GetSchemaAsync(subject, version));
                            }
                            catch (AggregateException aggregate) when(aggregate.InnerException is SchemaRegistryException inner)
                            {
                                throw new ProgramException(message: $"Failed to retrieve schema with subject {subject} and version {version} ({inner.Message}).", inner: inner);
                            }
                        }
                        else
                        {
                            try
                            {
                                return((await client.GetLatestSchemaAsync(options.SchemaSubject)).SchemaString);
                            }
                            catch (AggregateException aggregate) when(aggregate.InnerException is SchemaRegistryException inner)
                            {
                                throw new ProgramException(message: $"Failed to retrieve latest schema with subject {subject} ({inner.Message}).", inner: inner);
                            }
                        }
                    }
                }
                catch (AggregateException aggregate) when(aggregate.InnerException is Exception inner)
                {
                    throw new ProgramException(message: $"Failed to connect to the Schema Registry ({inner.Message}).", inner: inner);
                }
            }
        }
        public static async Task <string> ResolveSchema(this ISchemaResolutionOptions options)
        {
            if (Console.IsInputRedirected)
            {
                using var stream       = Console.OpenStandardInput();
                using var streamReader = new System.IO.StreamReader(stream);

                return(await streamReader.ReadToEndAsync());
            }
            else
            {
                if (string.IsNullOrWhiteSpace(options.RegistryUrl))
                {
                    throw new ProgramException(message: "When not reading from stdin, you must provide --registry-url.");
                }

                var configuration = new SchemaRegistryConfiguration
                {
                    SchemaRegistryUrl = options.RegistryUrl
                };

                using var client = new SchemaRegistryClient(configuration);

                try
                {
                    if (options.SchemaId is int id)
                    {
                        if (!string.IsNullOrEmpty(options.SchemaSubject) || options.SchemaVersion.HasValue)
                        {
                            throw new ProgramException(message: "When using --id, don’t use --schema or --version.");
                        }

                        try
                        {
                            return(await client.GetSchemaAsync(id));
                        }
                        catch (AggregateException aggregate) when(aggregate.InnerException is SchemaRegistryException inner)
                        {
                            throw new ProgramException(message: $"Failed to retrieve schema with ID {id} ({inner.Message}).", inner: inner);
                        }
                    }
                    else
                    {
                        if (options.SchemaSubject is var subject && string.IsNullOrEmpty(subject))
                        {
                            throw new ProgramException(message: "Either --id or --schema (and optionally --version) must be provided.");
                        }

                        if (options.SchemaVersion is int version)
                        {
                            try
                            {
                                return(await client.GetSchemaAsync(subject, version));
                            }
                            catch (AggregateException aggregate) when(aggregate.InnerException is SchemaRegistryException inner)
                            {
                                throw new ProgramException(message: $"Failed to retrieve schema with subject {subject} and version {version} ({inner.Message}).", inner: inner);
                            }
                        }
                        else
                        {
                            try
                            {
                                return((await client.GetLatestSchemaAsync(options.SchemaSubject)).SchemaString);
                            }
                            catch (AggregateException aggregate) when(aggregate.InnerException is SchemaRegistryException inner)
                            {
                                throw new ProgramException(message: $"Failed to retrieve latest schema with subject {subject} ({inner.Message}).", inner: inner);
                            }
                        }
                    }
                }
                catch (AggregateException aggregate) when(aggregate.InnerException is Exception inner)
                {
                    throw new ProgramException(message: $"Failed to connect to the Schema Registry ({inner.Message}).", inner: inner);
                }
            }
        }