Ejemplo n.º 1
0
        private HttpClient CreateClient(ElasticSearchSettings settings, HttpPolicy httpPolicy)
        {
            var httpClient = new HttpClient
            {
                BaseAddress = settings.Address,
                Timeout     = httpPolicy.Timeout
            };

            switch (settings.AuthorizationSchema)
            {
            case ElasticSearchAuthorizationSchemes.Anonymous:
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", settings.BearerToken);
                break;

            case ElasticSearchAuthorizationSchemes.Basic:
                var byteArray = Encoding.ASCII.GetBytes($"{settings.UserName}:{settings.Password}");
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                break;

            case ElasticSearchAuthorizationSchemes.BearerToken:
                break;

            default:
                throw new NotImplementedException($"The specified schema {settings.AuthorizationSchema} is not implemented");
            }

            return(httpClient);
        }
Ejemplo n.º 2
0
 public ElasticSearchBulkClient(
     ILoggerFactory loggerFactory,
     ElasticSearchSettings elasticSearchSettings)
     : this(
         loggerFactory,
         elasticSearchSettings,
         new HttpPolicy())
 {
 }
Ejemplo n.º 3
0
        public ElasticSearchBulkClient(
            ILoggerFactory loggerFactory,
            ElasticSearchSettings elasticSearchSettings,
            HttpPolicy httpPolicy,
            HttpMessageHandler httpMessageHandler = null)
        {
            if (elasticSearchSettings == null)
            {
                throw new ArgumentNullException(nameof(elasticSearchSettings));
            }

            if (httpPolicy == null)
            {
                throw new ArgumentNullException(nameof(httpPolicy));
            }

            _httpClient            = CreateClient(elasticSearchSettings, httpPolicy);
            _backOffPeriod         = httpPolicy.BackoffPeriod;
            _failuresBeforeBackoff = httpPolicy.FailuresBeforeBackoff;
            _failureAttempts       = 0;
            _logger = loggerFactory.CreateLogger <ElasticSearchBulkClient>();
        }
Ejemplo n.º 4
0
 public BulkPayloadBuilder(ElasticSearchSettings settings, Func <string, string> metricTagValueFormatter)
 {
     _settings = settings ?? throw new ArgumentNullException(nameof(settings));
     _metricTagValueFormatter = metricTagValueFormatter;
     _serializer = JsonSerializer.Create();
 }