예제 #1
0
        public static RestClientFactoryConfig UseFormUrlEncodedHandler(this RestClientFactoryConfig config)
        {
            DelegatingHandler HandlerFactory() => new FormUrlEncodedHandler();

            config.HttpClientBuilder.AddHttpMessageHandler(HandlerFactory);

            return(config);
        }
예제 #2
0
        public static RestClientFactoryConfig UseHandler(this RestClientFactoryConfig config, Func <HttpRequestMessage, CancellationToken, Func <HttpRequestMessage, CancellationToken, Task <HttpResponseMessage> >, Task <HttpResponseMessage> > handler)
        {
            DelegatingHandler HandlerFactory() => new DelegatingHandlerWrapper(handler);

            config.HttpClientBuilder.AddHttpMessageHandler(HandlerFactory);

            return(config);
        }
예제 #3
0
        public static RestClientFactoryConfig UseUnitTestHandler(this RestClientFactoryConfig config, Action <HttpRequestMessage> handler)
        {
            DelegatingHandler HandlerFactory() => new UnitTestHandler(handler);

            config.HttpClientBuilder.AddHttpMessageHandler(HandlerFactory);

            return(config);
        }
예제 #4
0
        public static RestClientFactoryConfig UseTwitterHandler(this RestClientFactoryConfig config, string consumerKey, string consumerKeySecret, string accessToken, string accessTokenSecret)
        {
            DelegatingHandler HandlerFactory() => new TwitterHandler(consumerKey, consumerKeySecret, accessToken, accessTokenSecret);

            config.HttpClientBuilder.AddHttpMessageHandler(HandlerFactory);

            return(config);
        }
예제 #5
0
        public static RestClientFactoryConfig UseRetryHandler(this RestClientFactoryConfig config, int maxRetries, double waitToRetryInSeconds, double maxWaitToRetryInSeconds, RetryHandler.BackOffStrategy backOffStrategy)
        {
            DelegatingHandler HandlerFactory() => new RetryHandler(maxRetries, waitToRetryInSeconds, maxWaitToRetryInSeconds, backOffStrategy);

            config.HttpClientBuilder.AddHttpMessageHandler(HandlerFactory);

            return(config);
        }
예제 #6
0
        public static RestClientFactoryConfig UseMultipartFormDataHandler(this RestClientFactoryConfig config)
        {
            DelegatingHandler HandlerFactory() => new MultipartFormDataHandler();

            config.HttpClientBuilder.AddHttpMessageHandler(HandlerFactory);

            return(config);
        }
예제 #7
0
        public static RestClientFactoryConfig UseCookieHandler(this RestClientFactoryConfig config, CookieContainer cookieContainer)
        {
            config.UseHttpClientHandler(httpClientHandler => { httpClientHandler.CookieContainer = cookieContainer; });

            DelegatingHandler HandlerFactory() => new CookieHandler();

            config.UseHandler(HandlerFactory);

            return(config);
        }
예제 #8
0
        public static RestClientFactoryConfig UseHttpClientHandler(this RestClientFactoryConfig config, Action <HttpClientHandler> httpClientHandlerOptions)
        {
            HttpClientHandler ConfigureHttpClientHandler(IServiceProvider provider) // Delegate called at Factory creation time
            {
                var clientHandler = provider.GetRequiredService <HttpClientHandlerWrapper>();

                httpClientHandlerOptions(clientHandler);

                return(clientHandler);
            }

            config.HttpClientBuilder.Services.TryAddTransient <HttpClientHandlerWrapper>();
            config.HttpClientBuilder.ConfigurePrimaryHttpMessageHandler(ConfigureHttpClientHandler);

            return(config);
        }
        public static RestClientFactoryConfig AddRestClient(this IServiceCollection services, string name, string baseUri, Headers defaultRequestHeaders)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("name cannot be empty", nameof(name));
            }

            services.TryAddSingleton <IRestClientFactory, RestClientFactory>();

            var httpClientBuilder = services.AddHttpClient(name);
            var config            = new RestClientFactoryConfig(httpClientBuilder);

            services.AddSingleton(serviceProvider => new RestClientContainer(serviceProvider, name, baseUri, defaultRequestHeaders));

            return(config);
        }
예제 #10
0
        public static RestClientFactoryConfig UseHttpClientHandler(this RestClientFactoryConfig config, Func <HttpClientHandler> handler)
        {
            config.HttpClientBuilder.ConfigurePrimaryHttpMessageHandler(handler);

            return(config);
        }
예제 #11
0
        public static RestClientFactoryConfig UseHandler(this RestClientFactoryConfig config, Func <DelegatingHandler> handler)
        {
            config.HttpClientBuilder.AddHttpMessageHandler(handler);

            return(config);
        }
예제 #12
0
 public static RestClientFactoryConfig UseNoDefaultHandlers(this RestClientFactoryConfig config)
 {
     config.UseDefaultHandlers = false;
     return(config);
 }
예제 #13
0
 public static RestClientFactoryConfig SetJsonSerializerSettings(this RestClientFactoryConfig config, JsonSerializerSettings jsonSerializerSettings)
 {
     config.JsonSerializerSettings = jsonSerializerSettings;
     return(config);
 }
예제 #14
0
 public static RestClientFactoryConfig UseCookieHandler(this RestClientFactoryConfig config)
 {
     return(UseCookieHandler(config, new CookieContainer()));
 }