Esempio n. 1
0
        private static void InitApiCallbackClient(AcTestContext context)
        {
            TestContext.Out.WriteLine("Initialising API Callback Client");
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("bearer", context.Tokens.NotificationCallbackBearerToken);
            var baseUrl = context.Config.ServicesConfig.NotificationApiUrl;

            context.ApiCallbackClient = NotificationApiClient.GetClient(baseUrl, httpClient);
        }
        public static IServiceCollection AddCustomTypes(this IServiceCollection serviceCollection)
        {
            serviceCollection.AddHttpContextAccessor();
            serviceCollection.AddMemoryCache();
            serviceCollection.AddTransient <HearingApiTokenHandler>();
            serviceCollection.AddTransient <UserApiTokenHandler>();
            serviceCollection.AddTransient <VideoApiTokenHandler>();
            serviceCollection.AddTransient <NotificationApiTokenHandler>();
            serviceCollection.AddTransient <IHearingsService, HearingsService>();
            serviceCollection.AddScoped <ITokenProvider, TokenProvider>();
            serviceCollection.AddScoped <IUserAccountService, UserAccountService>();
            serviceCollection.AddScoped <AzureAdConfiguration>();
            serviceCollection.AddSingleton <IClaimsCacheProvider, MemoryClaimsCacheProvider>();
            serviceCollection.AddScoped <ICachedUserClaimBuilder, CachedUserClaimBuilder>();
            serviceCollection.AddSingleton <IPollyRetryService, PollyRetryService>();

            // Build the hearings api client using a reusable HttpClient factory and predefined base url
            var container = serviceCollection.BuildServiceProvider();
            var settings  = container.GetService <IOptions <ServiceConfiguration> >().Value;

            serviceCollection.AddHttpClient <IBookingsApiClient, BookingsApiClient>()
            .AddHttpMessageHandler(() => container.GetService <HearingApiTokenHandler>())
            .AddTypedClient(httpClient =>
            {
                var client     = BookingsApiClient.GetClient(httpClient);
                client.BaseUrl = settings.BookingsApiUrl;
                client.ReadResponseAsString = true;
                return((IBookingsApiClient)client);
            });

            serviceCollection.AddHttpClient <IUserApiClient, UserApiClient>()
            .AddHttpMessageHandler(() => container.GetService <UserApiTokenHandler>())
            .AddTypedClient(httpClient =>
            {
                var client     = UserApiClient.GetClient(httpClient);
                client.BaseUrl = settings.UserApiUrl;
                client.ReadResponseAsString = true;
                return((IUserApiClient)client);
            });

            serviceCollection.AddHttpClient <IVideoApiClient, VideoApiClient>()
            .AddHttpMessageHandler(() => container.GetService <VideoApiTokenHandler>())
            .AddTypedClient(httpClient =>
            {
                var client     = VideoApiClient.GetClient(httpClient);
                client.BaseUrl = settings.VideoApiUrl;
                client.ReadResponseAsString = true;
                return((IVideoApiClient)client);
            });

            serviceCollection.AddHttpClient <INotificationApiClient, NotificationApiClient>()
            .AddHttpMessageHandler(() => container.GetService <NotificationApiTokenHandler>())
            .AddTypedClient(httpClient =>
            {
                var client     = NotificationApiClient.GetClient(httpClient);
                client.BaseUrl = settings.NotificationApiUrl;
                client.ReadResponseAsString = true;
                return((INotificationApiClient)client);
            });

            serviceCollection.AddHttpClient <IPublicHolidayRetriever, UkPublicHolidayRetriever>();
            serviceCollection.AddTransient <IUserIdentity, UserIdentity>((ctx) =>
            {
                var userPrincipal = ctx.GetService <IHttpContextAccessor>().HttpContext.User;

                return(new UserIdentity(userPrincipal));
            });

            serviceCollection.AddSingleton <IValidator <EditHearingRequest>, EditHearingRequestValidator>();

            return(serviceCollection);
        }