Ejemplo n.º 1
0
        private ITokenRepository CreateInstance(string server, int rateLimit, TimeSpan restDuration, TimeSpan watchDuration, int maxTotal, bool sliding)
        {
            _mockApiLimits = new Mock <IConfigureApiLimits>();
            _mockApiLimits.Setup(x => x.Setup(server, rateLimit, restDuration, watchDuration, maxTotal, sliding)).Verifiable();
            _mockApiLimits.SetupGet(c => c.Server).Returns(server);
            _mockApiLimits.SetupGet(c => c.RateLimit).Returns(rateLimit);
            _mockApiLimits.SetupGet(c => c.RestDuration).Returns(restDuration);
            _mockApiLimits.SetupGet(c => c.WatchDuration).Returns(watchDuration);
            _mockApiLimits.SetupGet(c => c.TotalLimit).Returns(maxTotal);
            //var mockLogger = new Mock<ILogger<FixWindowTokenRepo>>();
            var mockLogger = new TestLoggerT <FixWindowTokenRepo>();
            var sut        = new FixWindowTokenRepo(_mockApiLimits.Object, mockLogger);

            return(sut);
        }
Ejemplo n.º 2
0
        public static void AddTokenRepository(this IServiceCollection services, string server, int maxRateLimit, TimeSpan restDuration, TimeSpan watchDuration, int maxForDuration = int.MinValue, bool blocking = false)
        {
            //An entity should not be a service that is injected.. it should simply be instantiated and used....
            //services.AddSingleton<IConfigureApiLimits>((services) =>
            //{
            //  var retVal = new ApiLimitsConfig() as IConfigureApiLimits;
            //  retVal.Setup(server, maxRateLimit, restDuration, watchDuration, maxForDuration, blocking);
            //  return retVal;
            //});
            var apiLimitsConfig = new ApiLimitsConfig();

            apiLimitsConfig.Setup(server, maxRateLimit, restDuration, watchDuration, maxForDuration, blocking);

            if (maxRateLimit > 0)
            {
                services.AddSingleton <ITokenRepository>((services) =>
                {
                    var logFactory = services.GetRequiredService <ILoggerFactory>();
                    var retVal     = blocking
              ? new BlockingTokenRepo(apiLimitsConfig, logFactory.CreateLogger <BlockingTokenRepo>()) as ITokenRepository
              : new NonBlockingTokenRepo(apiLimitsConfig, logFactory.CreateLogger <NonBlockingTokenRepo>()) as ITokenRepository;
                    return(retVal);
                });
            }
            if (maxForDuration > 0)
            {
                services.AddSingleton <ITokenRepository>((services) =>
                {
                    var logFactory = services.GetRequiredService <ILoggerFactory>();
                    var retVal     = new FixWindowTokenRepo(apiLimitsConfig, logFactory.CreateLogger <FixWindowTokenRepo>()) as ITokenRepository;
                    return(retVal);
                });
            }
            if (maxForDuration > 0 && maxRateLimit > 0) //injects only 2 ITokenRepository
            {
                services.AddTransient <IGrantToken, MultiTokenApiGateway>();
            }
            else if (maxForDuration > 0 || maxRateLimit > 0) //injects only 1 ITokenRepository
            {
                services.AddTransient <IGrantToken, SingleTokenApiGateway>();
            }
            else
            {
                throw new InvalidOperationException("ITokenRepository cannot be determined, make sure configuration is valid to inject at least 1 ITokenRepository.");
            }
        }