Example #1
0
        /// <inheritdoc />
        public IFluentHttpClient Add(FluentHttpClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (string.IsNullOrEmpty(options.Identifier))
            {
                throw ClientBuilderValidationException.FieldNotSpecified(nameof(options.Identifier));
            }

            if (Has(options.Identifier))
            {
                throw new ClientBuilderValidationException($"FluentHttpClient '{options.Identifier}' is already registered.");
            }

            if (string.IsNullOrEmpty(options.BaseUrl))
            {
                throw ClientBuilderValidationException.FieldNotSpecified(nameof(options.BaseUrl));
            }

            // todo: find a way how to use DI with additional param (or so) to factory for abstraction.
            var client = (IFluentHttpClient)ActivatorUtilities.CreateInstance <FluentHttpClient>(_serviceProvider, options);

            _clientsMap.Add(options.Identifier, client);
            return(client);
        }
Example #2
0
        /// <summary>
        /// Build a new HTTP client.
        /// </summary>
        /// <typeparam name="THttpClient">HttpClient type</typeparam>
        /// <param name="options"></param>
        public IFluentHttpClient Build <THttpClient>(FluentHttpClientOptions?options = null)
            where THttpClient : IFluentHttpClient
        {
            options ??= BuildOptions();

            if (string.IsNullOrEmpty(options.Identifier))
            {
                throw ClientBuilderValidationException.FieldNotSpecified(nameof(options.Identifier));
            }

            return(ActivatorUtilities.CreateInstance <THttpClient>(_serviceProvider, options, _fluentHttpClientFactory));
        }