Esempio n. 1
0
 public BargainsForCouplesApiClient(IOptions <BargainsForCouplesSettings> settings, HttpClient httpClient)
 {
     _HttpClient = httpClient;
     _Settings   = settings.Value;
     //_Logger = logger; TODO Add log functionality
     _BaseUrl = _Settings.BaseUrl;
 }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version        = "v1",
                    Title          = "CheapAwesome API",
                    Description    = "Cheap Awesome travel",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact        = new OpenApiContact
                    {
                        Name  = "Cheap Awesome Travel",
                        Email = "*****@*****.**",
                        Url   = new Uri("http://cheapawesome.travel/"),
                    }
                });
            });

            // Dependency Injection
            services.AddTransient <IHotelAvailabilityService, HotelAvailabilityService>();

            services.AddSingleton <ICacheService, MemoryCacheService>();

            Random jitterer    = new Random();
            var    retryPolicy = HttpPolicyExtensions
                                 .HandleTransientHttpError()
                                 .WaitAndRetryAsync(5,
                                                    retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) // exponential back-off: 2, 4, 8 etc
                                                    + TimeSpan.FromMilliseconds(jitterer.Next(0, 1000))             // plus some jitter: up to 1 second
                                                    );

            var breakerPolicy = HttpPolicyExtensions
                                .HandleTransientHttpError()
                                .AdvancedCircuitBreakerAsync(
                failureThreshold: 0.5,
                samplingDuration: TimeSpan.FromSeconds(5),
                minimumThroughput: 20,
                durationOfBreak: TimeSpan.FromSeconds(30));

            //TODO : Register all implementations of IHotelAvailabilitySupplier dynamically
            services.AddHttpClient <IHotelAvailabilitySupplier, BargainsForCouplesHotelAvailabilityService>()
            .AddPolicyHandler(retryPolicy)
            .AddPolicyHandler(breakerPolicy);

            var bargainsForCouplesSetting = new BargainsForCouplesSettings();

            Configuration.GetSection(nameof(BargainsForCouplesSettings)).Bind(bargainsForCouplesSetting);
            services.AddSingleton(bargainsForCouplesSetting);

            var memoryCacheSettings = new MemoryCacheSettings();

            Configuration.GetSection(nameof(MemoryCacheSettings)).Bind(memoryCacheSettings);
            services.AddSingleton(memoryCacheSettings);
        }