protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var handler = base.CreateHandler(args);

            var clientHandler = handler as HttpClientHandler;

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

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

      return webRequestHandler;
    }
        public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args)
        {
            // Create the handler.
            var handler = CreateHandler(args);
            var configurableHandler = new ConfigurableMessageHandler(handler)
                {
                    ApplicationName = args.ApplicationName
                };

            // Create the client.
            var client = new ConfigurableHttpClient(configurableHandler);
            foreach (var initializer in args.Initializers)
            {
                initializer.Initialize(client);
            }

            return client;
        }
        public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args)
        {
            // Create the handler.
            var handler             = CreateHandler(args);
            var configurableHandler = new ConfigurableMessageHandler(handler)
            {
                ApplicationName = args.ApplicationName
            };

            // Create the client.
            var client = new ConfigurableHttpClient(configurableHandler);

            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>
        protected virtual HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var handler = new HttpClientHandler();

            // If the framework supports redirect configuration, set it to false, because ConfigurableMessageHandler
            // handles redirect.
            if (handler.SupportsRedirectConfiguration)
            {
                handler.AllowAutoRedirect = false;
            }

            // If the framework supports automatic decompression and GZip is enabled, set automatic decompression.
            if (handler.SupportsAutomaticDecompression && args.GZipEnabled)
            {
                handler.AutomaticDecompression = System.Net.DecompressionMethods.GZip |
                                                 System.Net.DecompressionMethods.Deflate;
            }

            Logger.Debug("Handler was created. SupportsRedirectConfiguration={0}, SupportsAutomaticDecompression={1}",
                         handler.SupportsRedirectConfiguration, handler.SupportsAutomaticDecompression);

            return(handler);
        }
        /// <summary>Creates a HTTP message handler. Override this method to mock a message handler.</summary>
        protected virtual HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var handler = new HttpClientHandler();

            // If the framework supports redirect configuration, set it to false, because ConfigurableMessageHnalder 
            // handles redirect.
            if (handler.SupportsRedirectConfiguration)
            {
                handler.AllowAutoRedirect = false;
            }

            // If the framework supports automatic decompression and GZip is enabled, set automatic decompression.
            if (handler.SupportsAutomaticDecompression && args.GZipEnabled)
            {
                handler.AutomaticDecompression = System.Net.DecompressionMethods.GZip |
                    System.Net.DecompressionMethods.Deflate;
            }

            Logger.Debug("Handler was created. SupportsRedirectConfiguration={0}, SupportsAutomaticDecompression={1}",
                handler.SupportsRedirectConfiguration, handler.SupportsAutomaticDecompression);

            return handler;
        }
        /// <summary>Creates a HTTP message handler. Override this method to mock a message handler.</summary>
        protected virtual HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            // We need to handle three situations in order to intercept uncompressed data where necessary
            // while using the built-in decompression where possible.
            // - No compression requested
            // - Compression requested but not supported by HttpClientHandler (easy; just GzipDeflateHandler on top of an interceptor on top of HttpClientHandler)
            // - Compression requested and HttpClientHandler (complex: create two different handlers and decide which to use on a per-request basis)

            var clientHandler = CreateSimpleClientHandler();

            if (!args.GZipEnabled)
            {
                // Simple: nothing will be decompressing content, so we can just intercept the original handler.
                return(new StreamInterceptionHandler(clientHandler));
            }
            else if (!clientHandler.SupportsAutomaticDecompression)
            {
                // Simple: we have to create our own decompression handler anyway, so there's still just a single chain.
                var interceptionHandler = new StreamInterceptionHandler(clientHandler);
                return(new GzipDeflateHandler(interceptionHandler));
            }
            else
            {
                // Complex: we want to use a simple handler with no interception but with built-in decompression
                // for requests that wouldn't perform interception anyway, and a longer chain for interception cases.
                clientHandler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

                return(new TwoWayDelegatingHandler(
                           // Normal handler (with built-in decompression) when there's no interception.
                           clientHandler,
                           // Alternative handler for requests that might be intercepted, and need that interception to happen
                           // before decompression. We need to delegate to a new client handler that *doesn't*
                           new GzipDeflateHandler(new StreamInterceptionHandler(CreateSimpleClientHandler())),
                           request => StreamInterceptionHandler.GetInterceptorProvider(request) != null));
            }
        }
        /// <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);
        }
        /// <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.Load(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.
            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);
        }
 protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
 {
     return Handler;
 }