private IEnumerable <ItemOrderStats> RetrieveItems(IEnumerable <int> typesToRequest, int regionId, string cacheKey)
        {
            var resultItems = new List <ItemOrderStats>();

            if (!_isServiceAvailable)
            {
                return(resultItems);
            }

            var requestUri  = new Uri($"https://market.fuzzwork.co.uk/aggregates/?region={regionId}&types={string.Join(",", typesToRequest)}");
            var requestTask = _requestProvider.GetAsync(requestUri);

            requestTask.Wait();             // wait for the completion (we're in a background task anyways)

            if (!requestTask.Result.IsSuccessStatusCode ||
                WebServiceExceptionHelper.IsServiceUnavailableError(requestTask.Exception))
            {
                _isServiceAvailable = false;                 // TODO: postpone next check.
                return(resultItems);
            }

            if (!requestTask.IsTaskSuccessfullyCompleted())
            {
                return(resultItems);
            }

            var contentStreamTask = requestTask.Result.Content.ReadAsStreamAsync();

            contentStreamTask.Wait();

            using (var reader = new StreamReader(contentStreamTask.Result))
            {
                try
                {
                    // process result
                    var retrievedItems = ParseResult(reader.ReadToEnd());

                    // cache it.
                    foreach (var item in retrievedItems)
                    {
                        _regionDataCache.Add(
                            ItemKeyFormat.FormatInvariant(item.ItemTypeId, cacheKey),
                            item,
                            DateTimeOffset.Now.Add(_cacheTtl));
                        resultItems.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.FormatException());
                    throw;
                }
            }

            return(resultItems);
        }
Exemplo n.º 2
0
        /// <summary>The retrieve items.</summary>
        /// <param name="typesToRequest">The types to request.</param>
        /// <param name="includeRegions">The include regions.</param>
        /// <param name="systemId">The system id.</param>
        /// <param name="minQuantity">The min quantity.</param>
        /// <param name="cacheKey">The cache key.</param>
        /// <returns>The <see cref="IEnumerable" />.</returns>
        private IEnumerable <Market.ItemOrderStats> RetrieveItems(
            List <int> typesToRequest,
            IEnumerable <int> includeRegions,
            int systemId,
            int minQuantity,
            string cacheKey)
        {
            var resultItems = new List <Market.ItemOrderStats>();

            if (!_isServiceAvailable)
            {
                return(resultItems);
            }

            // make the request for the types we don't have valid caches for
            NameValueCollection requestParameters = CreateMarketRequestParameters(
                typesToRequest,
                includeRegions,
                systemId,
                minQuantity);

            Task <HttpResponseMessage> requestTask =
                _requestProvider.PostAsync(new Uri(EveCentralBaseUrl + MarketStatApi), requestParameters);

            requestTask.Wait();     // wait for the completion (we're in a background task anyways)
            if (!requestTask.Result.IsSuccessStatusCode || WebServiceExceptionHelper.IsServiceUnavailableError(requestTask.Exception))
            {
                _isServiceAvailable = false;
                return(resultItems);
            }

            if (requestTask.IsCompleted && requestTask.Result != null && !requestTask.IsCanceled &&
                !requestTask.IsFaulted && requestTask.Exception == null)
            {
                Task <Stream> contentStreamTask = requestTask.Result.Content.ReadAsStreamAsync();
                contentStreamTask.Wait();

                using (Stream stream = contentStreamTask.Result)
                {
                    try
                    {
                        // process result
                        IEnumerable <Market.ItemOrderStats> retrievedItems = GetOrderStatsFromXml(stream);

                        // cache it.
                        foreach (Market.ItemOrderStats item in retrievedItems)
                        {
                            _regionDataCache.Add(
                                ItemKeyFormat.FormatInvariant(item.ItemTypeId, cacheKey),
                                item,
                                DateTimeOffset.Now.Add(_cacheTtl));
                            resultItems.Add(item);
                            _regionDataCache.Add(
                                ItemKeyFormat.FormatInvariant(item.ItemTypeId, cacheKey),
                                item,
                                DateTimeOffset.Now.Add(_cacheTtl));
                            resultItems.Add(item);
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.FormatException());
                        throw;
                    }
                }
            }

            return(resultItems);
        }