Ejemplo n.º 1
0
        public SessionFactoryConfiguration BuildFactoryConfiguration()
        {
            // load all the models
            var modelScanner = new ModelScanner();

            foreach (var scan in ModelScannners)
            {
                scan(modelScanner);
            }

            var jsonSettings = new JsonSerializerSettings();

            foreach (var settings in SerializerSettings)
            {
                settings(jsonSettings);
            }

            var bundledHttpRequestModifiers  = new BundledHttpRequestModifier(HttpRequestModifiers);
            var bundledHttpResponseListeners = new BundledHttpResponseListener(HttpResponseListeners);

            // translate model attributes to session config
            modelScanner.Configure(SessionFactoryConfiguration);
            SessionFactoryConfiguration.Configure(jsonSettings);
            SessionFactoryConfiguration.Configure(bundledHttpRequestModifiers);
            SessionFactoryConfiguration.Configure(bundledHttpResponseListeners);
            ClientConfigurators.Add(client => client.BaseAddress = ApiHost);

            var builder = new HttpClientBuilder();

            (HttpClientBuilder ?? (_ => { }))(builder);

            // build HttpClient factory
            SessionFactoryConfiguration.HttpClientFactory = () =>
            {
                var client = new HttpClient(builder.Build());

                client
                .DefaultRequestHeaders
                .Accept
                .Add(MediaTypeWithQualityHeaderValue.Parse("application/vnd.api+json"));

                foreach (var configure in ClientConfigurators)
                {
                    configure(client);
                }

                Task.WaitAll(AsyncClientConfigurators.Select(x => x.Invoke(client)).ToArray());

                return(client);
            };

            return(SessionFactoryConfiguration);
        }