public HttpClient CreateHttpClient(HttpClientOptions options)
        {
            HttpMessageHandler handler = new HttpClientHandler {AllowAutoRedirect = options.AllowAutoRedirect};
            if (((HttpClientHandler) handler).SupportsAutomaticDecompression)
                ((HttpClientHandler) handler).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            if (options.AddTokenToRequests)
                handler = new AccessTokenAuthenticator(options.TokenRetriever, handler);

            TimeSpanSemaphore readTimeSpanSemaphore = null;
            TimeSpanSemaphore writeTimeSpanSemaphore = null;
            if (options.ReadRequestsPerSecond.HasValue)
            {
                double delay = (1/options.ReadRequestsPerSecond.Value);
                readTimeSpanSemaphore = (_readTimeSpanSemaphore ?? (_readTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay))));
            }
            if (options.WriteRequestsPerSecond.HasValue)
            {
                double delay = (1/options.WriteRequestsPerSecond.Value);
                writeTimeSpanSemaphore = (_writeTimeSpanSemaphore ?? (_writeTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay))));
            }

            if (readTimeSpanSemaphore != null || writeTimeSpanSemaphore != null)
            {
                handler = new ThrottlingMessageHandler(readTimeSpanSemaphore, writeTimeSpanSemaphore, handler);
            }
            return new HttpClient(handler);
        }
        public HttpClient CreateHttpClient(HttpClientOptions options)
        {
            HttpMessageHandler httpMessageHandler = new HttpClientHandler
            {
                AllowAutoRedirect = options.AllowAutoRedirect
            };
            if (((HttpClientHandler)httpMessageHandler).SupportsAutomaticDecompression)
                ((HttpClientHandler)httpMessageHandler).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

            if (options.AddTokenToRequests)
                httpMessageHandler = new AccessTokenAuthenticator(options.TokenRetriever, httpMessageHandler);

            TimeSpanSemaphore readTimeSpanSemaphore = null;
            TimeSpanSemaphore writeTimeSpanSemaphore = null;

            if (options.ReadRequestsPerSecond.HasValue)
                readTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(1.0 / options.ReadRequestsPerSecond.Value));

            if (options.WriteRequestsPerSecond.HasValue)
                writeTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(1.0 / options.WriteRequestsPerSecond.Value));

            if (readTimeSpanSemaphore != null || writeTimeSpanSemaphore != null)
                httpMessageHandler = new ThrottlingMessageHandler(readTimeSpanSemaphore, writeTimeSpanSemaphore, httpMessageHandler);

            httpMessageHandler = new RemoveEtagDelegatingHandler(httpMessageHandler);

            return new HttpClient(httpMessageHandler);
        }
Example #3
0
        public HttpClient CreateHttpClient(HttpClientOptions options)
        {
            HttpMessageHandler handler = new HttpClientHandler {
                AllowAutoRedirect = options.AllowAutoRedirect
            };

            if (((HttpClientHandler)handler).SupportsAutomaticDecompression)
            {
                ((HttpClientHandler)handler).AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            }

            if (options.AddTokenToRequests)
            {
                handler = new AccessTokenAuthenticator(options.TokenRetriever, handler);
            }

            TimeSpanSemaphore readTimeSpanSemaphore  = null;
            TimeSpanSemaphore writeTimeSpanSemaphore = null;

            if (options.ReadRequestsPerSecond.HasValue)
            {
                double delay = (1 / options.ReadRequestsPerSecond.Value);
                readTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay));
            }
            if (options.WriteRequestsPerSecond.HasValue)
            {
                double delay = (1 / options.WriteRequestsPerSecond.Value);
                writeTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay));
            }

            if (readTimeSpanSemaphore != null || writeTimeSpanSemaphore != null)
            {
                handler = new ThrottlingMessageHandler(readTimeSpanSemaphore, writeTimeSpanSemaphore, handler);
            }
            return(new HttpClient(handler));
        }