Example #1
0
            /// <summary>Constructs a new initializer using the given token server URL.</summary>
            public Initializer(string tokenServerUrl)
            {
                TokenServerUrl = tokenServerUrl;

                AccessMethod = new BearerToken.AuthorizationHeaderAccessMethod();
                Clock        = SystemClock.Default;
                DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.UnsuccessfulResponse503;
            }
            /// <summary>Constructs a new initializer using the given token server URL.</summary>
            public Initializer(string tokenServerUrl)
            {
                TokenServerUrl = tokenServerUrl;

                AccessMethod = new BearerToken.AuthorizationHeaderAccessMethod();
                Clock = SystemClock.Default;
                DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.UnsuccessfulResponse503;
            }
Example #3
0
 internal Initializer(ServiceCredential other)
 {
     TokenServerUrl    = other.TokenServerUrl;
     Clock             = other.Clock;
     AccessMethod      = other.AccessMethod;
     HttpClientFactory = other.HttpClientFactory;
     DefaultExponentialBackOffPolicy = other.DefaultExponentialBackOffPolicy;
 }
Example #4
0
        private void AssertBackOffIsApproximately(ExponentialBackOffPolicy policy, TimeSpan reference)
        {
            DateTime startTime = DateTime.Now;

            policy.BackOff();
            TimeSpan difference = DateTime.Now - startTime;

            Assert.True(difference - reference < TimeSpan.FromMilliseconds(20));
        }
Example #5
0
        public void ShouldBackOffAnIncrementingIntervalEveryCallUpToTheMaximumSpecifiedInterval()
        {
            ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy();

            policy.InitialInterval = TimeSpan.FromMilliseconds(100);
            policy.MaxInterval     = TimeSpan.FromMilliseconds(300);
            policy.Multiplier      = 2;
            policy.StartContext();

            AssertBackOffIsApproximately(policy, TimeSpan.FromMilliseconds(100));
            AssertBackOffIsApproximately(policy, TimeSpan.FromMilliseconds(200));
            AssertBackOffIsApproximately(policy, TimeSpan.FromMilliseconds(300));
            AssertBackOffIsApproximately(policy, TimeSpan.FromMilliseconds(300));
        }
        /// <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);
        }
 /// <summary>
 /// Constructs a new back-off initializer with the given policy and back-off handler create function.
 /// </summary>
 public ExponentialBackOffInitializer(ExponentialBackOffPolicy policy, Func<BackOffHandler> createBackOff)
 {
     Policy = policy;
     CreateBackOff = createBackOff;
 }
Example #8
0
 /// <summary>
 /// Constructs a new back-off initializer with the given policy and back-off handler create function.
 /// </summary>
 public ExponentialBackOffInitializer(ExponentialBackOffPolicy policy, Func <BackOffHandler> createBackOff)
 {
     Policy        = policy;
     CreateBackOff = createBackOff;
 }
Example #9
0
 /// <summary>Creates a new instance of <see cref="RetryTemplate"/> given an instance of <see cref="LiteDatabase"/>.</summary>
 public RetryTemplate(LiteDatabase database)
 {
     this.database = database;
     BackOffPolicy = new ExponentialBackOffPolicy();
     RetryPolicy   = new SimpleRetryPolicy();
 }