Ejemplo n.º 1
0
        public void RegisterContract <TContract>()
            where TContract : class
        {
            var validationResult = new ApiContractValidator
            {
                ContractKeyMustBeSpecified = true
            }.Validate(typeof(TContract));

            if (!validationResult.Success)
            {
                throw new ApiContractValidationException(validationResult);
            }

            string serviceKey = typeof(TContract).GetCustomAttribute <ApiAttribute>()?.Key;

            _services.AddSingleton(serviceProvider =>
            {
                var httpFactory = (IHttpClientFactory)serviceProvider.GetService(typeof(IHttpClientFactory));
                return(ApiProxy <TContract> .Create(new FactoryHttpClientProvider(httpFactory, serviceKey)));
            });
        }
        /// <summary>
        /// Creates api client for specified contract
        /// </summary>
        public static ApiClient <TContract> CreateApiClient <TContract>(this IHttpClientFactory httpClientFactory)
        {
            if (httpClientFactory == null)
            {
                throw new ArgumentNullException(nameof(httpClientFactory));
            }

            var valRes = new ApiContractValidator
            {
                ContractKeyMustBeSpecified = true
            }.Validate(typeof(TContract));

            if (!valRes.Success)
            {
                throw new ApiContractValidationException(valRes);
            }

            var contractKey = typeof(TContract).GetCustomAttribute <ApiAttribute>().Key;

            return(new ApiClient <TContract>(new FactoryHttpClientProvider(httpClientFactory, contractKey)));
        }