Skip to content
Snippets Groups Projects
ServiceCollectionExtensions.cs 1.14 KiB
Newer Older
using GatewayAPI.Clients.FlightService;
using GatewayAPI.Clients.UserService;
using System.Runtime.CompilerServices;

namespace GatewayAPI
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddHttpClients(this IServiceCollection services, ConfigurationManager configurationManager)
        {
            services.AddHttpClient<IUserServiceClient, UserServiceClient>(client =>
            {
                string? baseUrl = configurationManager["UserMicroservice:BaseUrl"];
                client.BaseAddress = new Uri(baseUrl ?? throw new InvalidOperationException("UserMicroservice BaseUrl is not configured."));
            }).AddHttpMessageHandler<RequestCookieHandler>();

            services.AddHttpClient<IFlightServiceClient, FlightServiceClient>(client =>
            {
                string? baseUrl = configurationManager["FlightMicroservice:BaseUrl"];
                client.BaseAddress = new Uri(baseUrl ?? throw new InvalidOperationException("FlightMicroservice BaseUrl is not configured."));
            }).AddHttpMessageHandler<RequestCookieHandler>();

            return services;
        }
    }
}