public IHomeCenterClient CreateClient(HomeCenterConnectionStringBuilder connectionStringBuilder)
 {
     if (connectionStringBuilder == null)
     {
         throw new ArgumentNullException(nameof(connectionStringBuilder));
     }
     return(new HomeCenterClient(connectionStringBuilder, _loggerFactory));
 }
Ejemplo n.º 2
0
        public HomeCenterClient(HomeCenterConnectionStringBuilder builder, ILoggerFactory loggerFactory)
        {
            void ConfigureApiHttpClient(HttpClient client)
            {
                client.BaseAddress = new Uri(builder.Endpoint);
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                           Convert.ToBase64String(Encoding.ASCII.GetBytes(
                                                                                                                      $"{builder.Username}:{builder.Password}")));
                client.DefaultRequestHeaders.Add("X-Fibaro-Version", "2");
            }

            var services = new ServiceCollection();

            services.AddSingleton(loggerFactory ?? new LoggerFactory());

            services.AddHttpClient(nameof(HttpClient), ConfigureApiHttpClient);
            services.AddHttpClient <IDevicesClient, DevicesClient>(ConfigureApiHttpClient);
            services.AddHttpClient <IStatusClient, StatusClient>(ConfigureApiHttpClient);
            services.AddHttpClient <ISettingsClient, SettingsClient>(ConfigureApiHttpClient);

            _serviceProvider = services.BuildServiceProvider();
        }