Exemple #1
0
        public static IServiceCollection AddCommandClient(this IServiceCollection services, Action <AppCommandClientConfig> configure)
        {
            var client = new AppCommandClientConfig();

            configure.Invoke(client);

            var sharedCredentials = new SharedClientCredentials();

            client.GetSharedClientCredentials?.Invoke(sharedCredentials);
            sharedCredentials.MergeWith(client.ClientCredentials);

            services.TryAddSingleton <IHttpClientFactory, DefaultHttpClientFactory>();

            services.TryAddScoped <AppCommandEntry>(s =>
            {
                var clientCredsFactory = new ClientCredentialsAccessTokenFactory <AppCommandEntry>(client.ClientCredentials, new BearerHttpClientFactory <AppCommandEntry>(s.GetRequiredService <IHttpClientFactory>()));
                return(new AppCommandEntry(client.ServiceUrl, clientCredsFactory));
            });

            services.TryAddScoped <IAppCommandHalClientFactory>(s =>
            {
                var clientCredsFactory = new ClientCredentialsAccessTokenFactory <AppCommandEntry>(client.ClientCredentials, new BearerHttpClientFactory <AppCommandEntry>(s.GetRequiredService <IHttpClientFactory>()));
                return(new AppCommandHalClientFactory(clientCredsFactory));
            });

            services.TryAddScoped <IAppCommandClient, AppCommandClient>();
            services.TryAddScoped <AppCommandValueProvider>();

            return(services);
        }
Exemple #2
0
        private async Task <AppCommandSetCollectionResult> LoadCommands()
        {
            var factory  = new ClientCredentialsAccessTokenFactory <EntryPointInjector>(Config.ButlerClient.ClientCredentials, new BearerHttpClientFactory <EntryPointInjector>(new DefaultHttpClientFactory()));
            var injector = new EntryPointInjector(Config.ButlerClient.ServiceUrl, factory);
            var entry    = await injector.Load();

            var commandResult = await entry.ListAppCommandSets(new AppCommandSetQuery()
            {
                Limit = int.MaxValue
            });

            return(commandResult);
        }
Exemple #3
0
        /// <summary>
        /// Add the home client set as a repository.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <param name="configure">The configure callback.</param>
        /// <returns></returns>
        public static IServiceCollection AddHomeClientRepository(this IServiceCollection services, Action <HomeClientOptions> configure)
        {
            var options = new HomeClientOptions();

            configure?.Invoke(options);

            var sharedCredentials = new SharedClientCredentials();

            options.GetSharedClientCredentials?.Invoke(sharedCredentials);

            services.TryAddSingleton <IHttpClientFactory, DefaultHttpClientFactory>();

            services.TryAddSingleton <IHomeClientManager>(s =>
            {
                var manager = new HomeClientManager();
                if (options.Clients != null)
                {
                    foreach (var client in options.Clients)
                    {
                        var creds = client.Value.ClientCredentials;
                        sharedCredentials.MergeWith(creds);
                        var clientCredsFactory = new ClientCredentialsAccessTokenFactory <EntryPointInjector>(creds, new BearerHttpClientFactory <EntryPointInjector>(s.GetRequiredService <IHttpClientFactory>()));
                        var entryPointInjector = new EntryPointInjector(client.Value.ServiceUrl, clientCredsFactory);
                        manager.SetClient(client.Key, new HomeClient(entryPointInjector));
                    }
                }
                return(manager);
            });

            services.TryAddScoped(typeof(IClientSwitchRepository <,>), typeof(ClientSwitchRepository <,>));
            services.TryAddScoped(typeof(IClientThermostatRepository <,>), typeof(ClientThermostatRepository <,>));

            services.AdditionalSwitchConfiguration(o =>
            {
                o.AddSwitch(typeof(IClientSwitchRepository <,>));
                o.AddThermostat(typeof(IClientThermostatRepository <,>));
            });

            return(services);
        }