Beispiel #1
0
            public ConsulClientConfigurationContainer()
            {
                Config = new ConsulClientConfiguration();
#if NETSTANDARD || NETCOREAPP
                _httpHandler = new HttpClientHandler();
#else
                _httpHandler = new WebRequestHandler();
#endif
                _httpClient         = new HttpClient(_httpHandler);
                _httpClient.Timeout = TimeSpan.FromMinutes(15);
                _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                _httpClient.DefaultRequestHeaders.Add("Keep-Alive", "true");
            }
Beispiel #2
0
            public ConsulClientConfigurationContainer(ConsulClientConfiguration config)
            {
                Config = config;
#if CORECLR
                HttpHandler = new HttpClientHandler();
#else
                HttpHandler = new WebRequestHandler();
#endif
                HttpClient         = new HttpClient(HttpHandler);
                HttpClient.Timeout = TimeSpan.FromMinutes(15);
                HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpClient.DefaultRequestHeaders.Add("Keep-Alive", "true");
            }
Beispiel #3
0
        internal Request(ConsulClientConfiguration config, HttpMethod method, string path)
            : this()
        {
            Config = config;
            Method = method;

            var builder = new UriBuilder {
                Scheme = config.Scheme
            };

            if (config.Address.Contains(":"))
            {
                var split = config.Address.Split(':');
                try
                {
                    builder.Host = split[0];
                    builder.Port = int.Parse(split[1]);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Could not parse port from client config address", ex);
                }
            }
            else
            {
                builder.Host = config.Address;
            }

            builder.Path = path;

            if (!string.IsNullOrEmpty(config.Datacenter))
            {
                Params["dc"] = config.Datacenter;
            }

            if (config.WaitTime.HasValue)
            {
                Params["wait"] = config.WaitTime.Value.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
            }

            if (!string.IsNullOrEmpty(config.Token))
            {
                Params["token"] = config.Token;
            }

            Url = builder.Uri;
        }
        protected override void ApplyOptions(ConsulClientConfiguration clientConfig)
        {
            if (Filter != null)
            {
                Params["filter"] = Filter.Encode();
            }

            if (Options == QueryOptions.Default)
            {
                return;
            }

            if (!string.IsNullOrEmpty(Options.Datacenter))
            {
                Params["dc"] = Options.Datacenter;
            }
            switch (Options.Consistency)
            {
            case ConsistencyMode.Consistent:
                Params["consistent"] = string.Empty;
                break;

            case ConsistencyMode.Stale:
                Params["stale"] = string.Empty;
                break;

            case ConsistencyMode.Default:
                break;
            }
            if (Options.WaitIndex != 0)
            {
                Params["index"] = Options.WaitIndex.ToString();
            }
            if (Options.WaitTime.HasValue)
            {
                Params["wait"] = Options.WaitTime.Value.ToGoDuration();
            }
            if (!string.IsNullOrEmpty(Options.Near))
            {
                Params["near"] = Options.Near;
            }
        }
Beispiel #5
0
        void ApplyConfig(ConsulClientConfiguration config, HttpClientHandler handler, HttpClient client)
#endif
        {
#pragma warning disable CS0618 // Type or member is obsolete
            if (config.HttpAuth != null)
#pragma warning restore CS0618 // Type or member is obsolete
            {
#pragma warning disable CS0618 // Type or member is obsolete
                handler.Credentials = config.HttpAuth;
#pragma warning restore CS0618 // Type or member is obsolete
            }
#if !__MonoCS__
            if (config.ClientCertificateSupported)
            {
#pragma warning disable CS0618 // Type or member is obsolete
                if (config.ClientCertificate != null)
#pragma warning restore CS0618 // Type or member is obsolete
                {
                    handler.ClientCertificateOptions = ClientCertificateOption.Manual;
#pragma warning disable CS0618 // Type or member is obsolete
                    handler.ClientCertificates.Add(config.ClientCertificate);
#pragma warning restore CS0618 // Type or member is obsolete
                }
                else
                {
                    handler.ClientCertificateOptions = ClientCertificateOption.Manual;
                    handler.ClientCertificates.Clear();
                }
            }
#endif
#if !CORECLR
#pragma warning disable CS0618 // Type or member is obsolete
            if (config.DisableServerCertificateValidation)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                handler.ServerCertificateValidationCallback += (certSender, cert, chain, sslPolicyErrors) => { return(true); };
            }
            else
            {
                handler.ServerCertificateValidationCallback = null;
            }
#else
#pragma warning disable CS0618 // Type or member is obsolete
            if (config.DisableServerCertificateValidation)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                handler.ServerCertificateCustomValidationCallback += (certSender, cert, chain, sslPolicyErrors) => { return(true); };
            }
            else
            {
                handler.ServerCertificateCustomValidationCallback = null;
            }
#endif

            if (!string.IsNullOrEmpty(config.Token))
            {
                if (client.DefaultRequestHeaders.Contains("X-Consul-Token"))
                {
                    client.DefaultRequestHeaders.Remove("X-Consul-Token");
                }
                client.DefaultRequestHeaders.Add("X-Consul-Token", config.Token);
            }
        }
Beispiel #6
0
 void ApplyConfig(ConsulClientConfiguration config, WebRequestHandler handler, HttpClient client)
Beispiel #7
0
 public ConsulClientConfigurationContainer(ConsulClientConfiguration config, HttpClient client)
 {
     skipClientDispose = true;
     Config            = config;
     HttpClient        = client;
 }
 protected abstract void ApplyHeaders(HttpRequestMessage message, ConsulClientConfiguration clientConfig);
 protected abstract void ApplyOptions(ConsulClientConfiguration clientConfig);
Beispiel #10
0
 public Client(ConsulClientConfiguration c)
 {
     _config = c;
 }
Beispiel #11
0
 public Client()
 {
     _config = new ConsulClientConfiguration();
 }
Beispiel #12
0
 public Modify(ConsulClientConfiguration config, HttpMethod method, string path, WriteOptions q)
     : base(config, method, path, q)
 {
 }
Beispiel #13
0
        /// <summary>
        /// Initializes a new Consul client using a <see cref="IConsulClientFactory"/> to initialize the HttpClient and the Configuration
        /// </summary>
        /// <param name="clientFactory">Factoty to initialzie the configuration and the Http client</param>
        public ConsulClient(IConsulClientFactory clientFactory)
        {
            Config = clientFactory.CreateConsulConfiguration();

            HttpClient = clientFactory.CreateHttpClient();
        }