Ejemplo n.º 1
0
        private ConfigurableHttpClient CreateHttpClient(Initializer initializer)
        {
            // if factory wasn't set use the default Http client factory
            var factory = initializer.HttpClientFactory ?? new HttpClientFactory();
            var args    = new CreateHttpClientArgs
            {
                GZipEnabled     = GZipEnabled,
                ApplicationName = ApplicationName,
            };

            // add the user's input initializer
            if (HttpClientInitializer != null)
            {
                args.Initializers.Add(HttpClientInitializer);
            }

            // add exponential back-off initializer if necessary
            if (initializer.DefaultExponentialBackOffPolicy != ExponentialBackOffPolicy.None)
            {
                args.Initializers.Add(new ExponentialBackOffInitializer(initializer.DefaultExponentialBackOffPolicy,
                                                                        CreateBackOffHandler));
            }

            // add authenticator initializer to intercept a request and add the "Authorization" header and also handle
            // abnormal 401 responses in case the authenticator is an instance of unsuccessful response handler.
            args.Initializers.Add(new AuthenticatorMessageHandlerInitializer(Authenticator));

            return(factory.CreateHttpClient(args));
        }
Ejemplo n.º 2
0
        private ConfigurableHttpClient CreateHttpClient(Initializer initializer, string versionHeader)
        {
            // If factory wasn't set use the default HTTP client factory.
            var factory = initializer.HttpClientFactory ?? new HttpClientFactory();
            var args    = new CreateHttpClientArgs
            {
                GZipEnabled           = GZipEnabled,
                ApplicationName       = ApplicationName,
                GoogleApiClientHeader = versionHeader
            };

            // Add the user's input initializer.
            if (HttpClientInitializer != null)
            {
                args.Initializers.Add(HttpClientInitializer);
            }

            // Add exponential back-off initializer if necessary.
            if (initializer.DefaultExponentialBackOffPolicy != ExponentialBackOffPolicy.None)
            {
                args.Initializers.Add(new ExponentialBackOffInitializer(initializer.DefaultExponentialBackOffPolicy,
                                                                        CreateBackOffHandler));
            }

            var httpClient = factory.CreateHttpClient(args);

            if (initializer.MaxUrlLength > 0)
            {
                httpClient.MessageHandler.AddExecuteInterceptor(new MaxUrlLengthInterceptor(initializer.MaxUrlLength));
            }
            return(httpClient);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates an authenticated <see cref="ConfigurableHttpClient"/> from the
        /// factory.
        /// </summary>
        /// <returns>An HTTP client that can be used to OAuth2 authorized requests.</returns>
        /// <param name="clientFactory">The <see cref="HttpClientFactory"/> used to create
        /// the HTTP client.</param>
        /// <param name="credential">The Google credential that will be used to authenticate
        /// outgoing HTTP requests.</param>
        public static ConfigurableHttpClient CreateAuthorizedHttpClient(
            this HttpClientFactory clientFactory, GoogleCredential credential)
        {
            var args = new CreateHttpClientArgs();

            args.Initializers.Add(credential.ThrowIfNull(nameof(credential)));
            return(clientFactory.CreateHttpClient(args));
        }
Ejemplo n.º 4
0
 protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
 {
     return(new WebRequestHandler
     {
         UseProxy = true,
         UseCookies = false,
         Proxy = new WebProxy(ProxyAddress, false, null, ProxyCredentials)
     });
 }
Ejemplo n.º 5
0
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var handler = TimeLimiter
                          .GetFromMaxCountByInterval(1000, TimeSpan.FromSeconds(100))
                          .AsDelegatingHandler();

            handler.InnerHandler = base.CreateHandler(args);

            return(handler);
        }
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var webRequestHandler = new HttpClientHandler()
            {
                UseProxy   = true,
                Proxy      = Proxy.Get,
                UseCookies = false
            };

            return(webRequestHandler);
        }
Ejemplo n.º 7
0
        private static ConfigurableHttpClient CreateHttpClient(
            MockMessageHandler handler, RetryOptions options, IWaiter waiter, IClock clock = null)
        {
            var args = new CreateHttpClientArgs();

            args.Initializers.Add(new RetryHttpClientInitializer(options, clock, waiter));

            var factory = new MockHttpClientFactory(handler);

            return(factory.CreateHttpClient(args));
        }
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var webRequestHandler = new WebRequestHandler()
            {
                Proxy      = _proxyOrNull,
                UseProxy   = (_proxyOrNull != null),
                UseCookies = false
            };

            return(webRequestHandler);
        }
Ejemplo n.º 9
0
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var proxy             = new WebProxy("http://127.0.0.1:3128", true, null, null);
            var webRequestHandler = new HttpClientHandler()
            {
                UseProxy   = true,
                Proxy      = proxy,
                UseCookies = false
            };

            return(webRequestHandler);
        }
Ejemplo n.º 10
0
        public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args)
        {
            HttpClientHandler httpClientHandler = new HttpClientHandler()
            {
                UseProxy = true,
                Proxy    = webProxy
            };

            ConfigurableMessageHandler httpMessageHandler = new ConfigurableMessageHandler(httpClientHandler);

            return(new ConfigurableHttpClient(httpMessageHandler));
        }
Ejemplo n.º 11
0
        }/*End of ProxySupportedHttpClientFactory constructor*/

        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var proxy = new WebProxy(_proxyAddress, _bypassLocal, _bypassList, _credentials);

            var webRequestHandler = new HttpClientHandler()
            {
                UseProxy   = true,
                Proxy      = proxy,
                UseCookies = false
            };

            return(webRequestHandler);
        } /*End of CreateHandler method override*/
Ejemplo n.º 12
0
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var handler = base.CreateHandler(args);

            var clientHandler = handler as HttpClientHandler;

            if (clientHandler != null)
            {
                clientHandler.ApplyProxy();
            }

            return(handler);
        }
Ejemplo n.º 13
0
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            //ICredentials credentials = new NetworkCredential("user1", "user1");
            //var proxy = new WebProxy("http://127.0.0.1:3128", true, null, credentials);
            var proxy             = new WebProxy("http://127.0.0.1:3128", true, null, null);
            var webRequestHandler = new HttpClientHandler()
            {
                UseProxy   = true,
                Proxy      = proxy,
                UseCookies = false
            };

            return(webRequestHandler);
        }
Ejemplo n.º 14
0
            protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
            {
                var webRequestHandler = new WebRequestHandler
                {
                    UseProxy   = this.UseProxy,
                    UseCookies = false
                };

                if (webRequestHandler.UseProxy)
                {
                    webRequestHandler.Proxy = Proxy;
                }

                return(webRequestHandler);
            }
        /// <summary>Constructs a new flow using the initializer's properties.</summary>
        public AuthorizationCodeFlow(Initializer initializer)
        {
            clientSecrets = initializer.ClientSecrets;
            if (clientSecrets == null)
            {
                if (initializer.ClientSecretsStream == null)
                {
                    throw new ArgumentException("You MUST set ClientSecret or ClientSecretStream on the initializer");
                }

                using (initializer.ClientSecretsStream)
                {
                    clientSecrets = GoogleClientSecrets.FromStream(initializer.ClientSecretsStream).Secrets;
                }
            }
            else if (initializer.ClientSecretsStream != null)
            {
                throw new ArgumentException(
                          "You CAN'T set both ClientSecrets AND ClientSecretStream on the initializer");
            }

            accessMethod           = initializer.AccessMethod.ThrowIfNull("Initializer.AccessMethod");
            clock                  = initializer.Clock.ThrowIfNull("Initializer.Clock");
            tokenServerUrl         = initializer.TokenServerUrl.ThrowIfNullOrEmpty("Initializer.TokenServerUrl");
            authorizationServerUrl = initializer.AuthorizationServerUrl.ThrowIfNullOrEmpty
                                         ("Initializer.AuthorizationServerUrl");

            dataStore = initializer.DataStore;
            if (dataStore == null)
            {
                Logger.Warning("Datastore is null, as a result the user's credential will not be stored");
            }
            scopes = initializer.Scopes;

            // Set the HTTP client.
            DefaultExponentialBackOffPolicy = initializer.DefaultExponentialBackOffPolicy;
            HttpClientFactory = initializer.HttpClientFactory ?? new HttpClientFactory();

            var httpArgs = new CreateHttpClientArgs();

            // Add exponential back-off initializer if necessary.
            if (DefaultExponentialBackOffPolicy != ExponentialBackOffPolicy.None)
            {
                httpArgs.Initializers.Add(new ExponentialBackOffInitializer(
                                              DefaultExponentialBackOffPolicy, () => new BackOffHandler(new ExponentialBackOff())));
            }
            httpClient = HttpClientFactory.CreateHttpClient(httpArgs);
        }
Ejemplo n.º 16
0
        /// <summary>Constructs a new service account credential using the given initializer.</summary>
        public ServiceCredential(Initializer initializer)
        {
            tokenServerUrl = initializer.TokenServerUrl;
            accessMethod   = initializer.AccessMethod.ThrowIfNull("initializer.AccessMethod");
            clock          = initializer.Clock.ThrowIfNull("initializer.Clock");

            // Set the HTTP client.
            var httpArgs = new CreateHttpClientArgs();

            // Add exponential back-off initializer if necessary.
            if (initializer.DefaultExponentialBackOffPolicy != ExponentialBackOffPolicy.None)
            {
                httpArgs.Initializers.Add(
                    new ExponentialBackOffInitializer(initializer.DefaultExponentialBackOffPolicy,
                                                      () => new BackOffHandler(new ExponentialBackOff())));
            }
            httpClient = (initializer.HttpClientFactory ?? new HttpClientFactory()).CreateHttpClient(httpArgs);
        }
Ejemplo n.º 17
0
            public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args)
            {
                var handler             = CreateHandler(args);
                var configurableHandler = new ConfigurableMessageHandler(handler)
                {
                    ApplicationName = args.ApplicationName
                };

                var client = new ConfigurableHttpClient(configurableHandler);

                client.DefaultRequestHeaders.Referrer = new Uri("https://youtubeuploader.daram.me");
                foreach (var initializer in args.Initializers)
                {
                    initializer.Initialize(client);
                }

                return(client);
            }
        /// <summary>
        /// Creates a HTTP message handler. Override this method to mock a message handler.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            if (config.Proxy != null)
            {
                HttpClientHandler webRequestHandler = new HttpClientHandler()
                {
                    UseProxy   = true,
                    Proxy      = config.Proxy,
                    UseCookies = false
                };

                return(webRequestHandler);
            }
            else
            {
                return(base.CreateHandler(args));
            }
        }
Ejemplo n.º 19
0
            protected virtual HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
            {
                var handler = new HttpClientHandler();

                if (handler.SupportsRedirectConfiguration)
                {
                    handler.AllowAutoRedirect = false;
                }

                if (handler.SupportsAutomaticDecompression && args.GZipEnabled)
                {
                    handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }

                handler.UseProxy = false;
                handler.Proxy    = null;

                return(handler);
            }
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            IWebProxy iProxy  = WebRequest.DefaultWebProxy;
            WebProxy  myProxy = new WebProxy(iProxy.GetProxy(new Uri("https://www.google.com")));

            // potentially, setup credentials on the proxy here
            myProxy.Credentials           = CredentialCache.DefaultCredentials;
            myProxy.UseDefaultCredentials = true;

            //var proxy = new WebProxy("http://proxyserver:8080", true, null, null);

            var webRequestHandler = new WebRequestHandler()
            {
                UseProxy   = true,
                Proxy      = myProxy,
                UseCookies = false
            };

            return(webRequestHandler);
        }
        private static HttpClient CreateHttpClient(ErrorHandlingHttpClientArgs <T> args)
        {
            var clientArgs = new CreateHttpClientArgs();
            var credential = args.Credential;

            if (credential != null)
            {
                clientArgs.Initializers.Add(credential);
            }

            var retry = args.RetryOptions;

            if (retry != null)
            {
                clientArgs.Initializers.Add(new RetryHttpClientInitializer(retry));
            }

            var clientFactory = args.HttpClientFactory.ThrowIfNull(nameof(args.HttpClientFactory));

            return(clientFactory.CreateHttpClient(clientArgs));
        }
Ejemplo n.º 22
0
        private static CreateHttpClientArgs CreateDefaultHttpClientArgs(IFcmClientSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings", "Settings are needed to create the Default HttpClientArgs");
            }

            var args = new CreateHttpClientArgs();

            // Create the Default BackOff Algorithm:
            var backoff = new ExponentialBackOff(settings.ExponentialBackOffSettings.DeltaBackOff, settings.ExponentialBackOffSettings.MaxNumberOfRetries);

            // Create the Initializer. Make sure to set the Maximum Timespan between two Requests. It is 16 Seconds per Default:
            var backoffInitializer = new BackOffHandler.Initializer(backoff)
            {
                MaxTimeSpan = settings.ExponentialBackOffSettings.MaxTimeSpan
            };

            args.Initializers.Add(new ExponentialBackOffInitializer(ExponentialBackOffPolicy.UnsuccessfulResponse503, () => new BackOffHandler(backoffInitializer)));

            return(args);
        }
Ejemplo n.º 23
0
 protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
 {
     return(_proxy.GetHttpMessageHandler());
 }
Ejemplo n.º 24
0
 public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args) =>
 new ConfigurableHttpClient(_handler);
Ejemplo n.º 25
0
 public FcmHttpClient(IFcmClientSettings settings, IHttpClientFactory httpClientFactory, CreateHttpClientArgs httpClientArgs)
     : this(settings, JsonSerializer.Default, httpClientFactory, httpClientArgs)
 {
 }
Ejemplo n.º 26
0
        public FcmHttpClient(IFcmClientSettings settings, IJsonSerializer serializer, IHttpClientFactory httpClientFactory, CreateHttpClientArgs httpClientArgs)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            this.settings   = settings;
            this.client     = httpClientFactory.CreateHttpClient(httpClientArgs);
            this.serializer = serializer;
            this.credential = CreateServiceAccountCredential(httpClientFactory, settings);

            InitializeExponentialBackOff(client, settings);
        }
Ejemplo n.º 27
0
 public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args) => null;
Ejemplo n.º 28
0
 protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
 {
     return(this.handler);
 }
Ejemplo n.º 29
0
 public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args)
 {
     return(this.httpClient);
 }
 public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args)
 {
     return(new ConfigurableHttpClient(new FakeHttpMessageHandler(_data)));
 }