/// <summary>The download data.</summary>
        /// <param name="entityId">The entity id.</param>
        /// <returns>The <see cref="IEnumerable" />.</returns>
        private IEnumerable <ItemOrderStats> DownloadData(int entityId)
        {
            IEnumerable <ItemOrderStats> results     = null;
            Task <HttpResponseMessage>   requestTask =
                _requestProvider.GetAsync(
                    new Uri(EveHqMarketDataDumpsLocation.FormatInvariant(entityId.ToInvariantString())));

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

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

                // process result
                using (var buffer = new MemoryStream())
                    using (ZipFile compressedData = ZipFile.Read(readTask.Result))
                    {
                        ZipEntry marketData = compressedData["{0}.txt".FormatInvariant(entityId)];
                        marketData.Extract(buffer);

                        string jsonData = Encoding.UTF8.GetString(buffer.ToArray());
                        results = JsonConvert.DeserializeObject <IEnumerable <ItemOrderStats> >(jsonData);
                    }
            }

            return(results);
        }
Beispiel #2
0
        /// <summary>
        /// Send request to CREST, fetch data in an async process and store the result to the cache
        /// </summary>
        /// <param name="crestEndpoint"></param>
        public void fetchData(Uri crestEndpoint)
        {
            // call data, parse, and so on
            Task <HttpResponseMessage> requestTask = _requestProvider.GetAsync(crestEndpoint);

            requestTask.Wait();

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

                using (Stream stream = contentStreamTask.Result)
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        if (!_cache.ContainsKey(crestEndpoint))
                        {
                            _cache.Add(crestEndpoint, new CacheItem(1, reader.ReadToEnd()));
                        }
                    }
                }
            }
        }