private HalHttpClient CreateHalHttpClient(HttpClient httpClient = null)
        {
            var client = new HalHttpClient(_parser, httpClient);

            try
            {
                Configure(client);
                return(client);
            }
            catch (Exception)
            {
                client.Dispose(); // client is unusable ...
                throw;
            }
        }
Esempio n. 2
0
        private IHalHttpClient CreateHalHttpClient(HttpClient httpClient, T context)
        {
            var wrapped = new HalHttpClient(HalJsonParser, httpClient);

            try
            {
                Configure(wrapped.Configuration, context);
                var decorated = Decorate(wrapped, context) ?? wrapped;
                return(decorated);
            }
            catch (Exception)
            {
                wrapped.Dispose();                 // client is unusable ...
                throw;
            }
        }
Esempio n. 3
0
        private async Task <IHalHttpClient> CreateHalHttpClientAsync(HttpClient httpClient, T context, CachingBehavior apiRootCachingBehavior)
        {
            if (httpClient == null)
            {
                throw new ArgumentNullException(nameof(httpClient));
            }

            var wrapped = new HalHttpClient(HalJsonParser, httpClient);

            try
            {
                Configure(wrapped.Configuration, context);

                var decorated = Decorate(wrapped, context) ?? wrapped;

                switch (apiRootCachingBehavior)
                {
                case CachingBehavior.Never:
                    break;

                case CachingBehavior.PerClient:
                    var apiRootResource = await GetFreshRootResourceAsync(decorated, wrapped.Configuration).ConfigureAwait(false);

                    wrapped.CachedApiRootResource = apiRootResource;
                    break;

                case CachingBehavior.Once:
                    _cachedApiRootResource = _cachedApiRootResource ?? await GetFreshRootResourceAsync(decorated, wrapped.Configuration).ConfigureAwait(false);

                    wrapped.CachedApiRootResource = _cachedApiRootResource;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(apiRootCachingBehavior), apiRootCachingBehavior, null);
                }

                return(decorated);
            }
            catch (Exception)
            {
                wrapped.Dispose();                 // client is unusable ...
                throw;
            }
        }
        private void SetRoot(HalHttpClient client, Uri baseAddress, bool refresh)
        {
            try
            {
                if (refresh || (_root == null))
                {
                    client.GetAsync(baseAddress)
                    .ContinueWith(x => _root = x.Result, TaskContinuationOptions.NotOnFaulted)
                    .Wait();
                }

                client.Root = _root;
            }
            catch (AggregateException ex)
            {
                client.Dispose(); // client is unusable ...
                throw new Exception("Could not GET the root response from '" + baseAddress + "'.", ex.InnerExceptions.FirstOrDefault());
            }
        }
        private IHalHttpClient CreateHalHttpClient(HttpClient httpClient)
        {
            if (httpClient == null)
            {
                throw new ArgumentNullException(nameof(httpClient));
            }

            var wrapped = new HalHttpClient(Parser, httpClient);

            try
            {
                Configure(wrapped.Configuration);

                var decorated = Decorate(wrapped) ?? wrapped;

                return(decorated);
            }
            catch (Exception)
            {
                wrapped.Dispose();                 // client is unusable ...
                throw;
            }
        }