Exemplo n.º 1
0
 /// <summary>
 ///     Sets data into cache.
 /// </summary>
 /// <typeparam name="T">type of data being stored.</typeparam>
 /// <param name="key">key to store the data under.</param>
 /// <param name="data">date to store.</param>
 private void SetCacheEntry <T>(string key, EveServiceResponse <T> data) where T : class
 {
     _cache.Add(key, data, data.CacheUntil);
 }
Exemplo n.º 2
0
 private void SetCacheEntry(string key, EveServiceResponse <EveStructure> data)
 {
     _cacheProvider.Add(key, data, data.CacheUntil);
 }
Exemplo n.º 3
0
        private EveServiceResponse <T> ProcessServiceResponse <T>(Task <HttpResponseMessage> webTask, string cacheKey,
                                                                  int defaultCacheSeconds, Func <XElement, T> parseXml)
            where T : class
        {
            Exception      faultError   = null;
            T              result       = default(T);
            DateTimeOffset cacheTime    = DateTimeOffset.Now.AddSeconds(defaultCacheSeconds);
            int            eveErrorCode = 0;
            string         eveErrorText = string.Empty;

            if (webTask.Exception != null)
            {
                faultError = webTask.Exception;
                Trace.TraceWarning(faultError.FormatException());
            }

            if (faultError == null)
            {
                try
                {
                    // Get the xml from the response.
                    XDocument xml = GetXmlFromResponse(webTask.Result);
                    XElement  resultsElement;
                    if (xml != null && xml.Root != null && (resultsElement = xml.Root.Element(Result)) != null)
                    {
                        // using LINQ convert the XML into the collection of characters.
                        result    = parseXml(resultsElement);
                        cacheTime = GetCacheExpiryFromResponse(xml.Root, defaultCacheSeconds);
                    }
                    else if (xml != null && xml.Root != null && (resultsElement = xml.Root.Element(Error)) != null)
                    {
                        // CCP server side error
                        eveErrorCode = GetErrorCodeFromResponse(resultsElement, ref eveErrorText);

                        // because cacheTime in the response is bugged (per http://wiki.eve-id.net/APIv2_Eve_ErrorList_XML ),
                        // defaulting cacheTime for 1 hour
                        cacheTime = DateTimeOffset.Now.AddHours(1);
                    }
                    else
                    {
                        // shenanigans.
                        eveErrorCode = 999999;
                        eveErrorText = "Unknown Error.";
                    }
                }
                catch (Exception e)
                {
                    // catch any of the xml processing errors
                    faultError = e;
                }
            }

            // store it.
            var eveResult = new EveServiceResponse <T>
            {
                ResultData             = result,
                ServiceException       = faultError,
                IsSuccessfulHttpStatus = faultError == null && webTask.Result.IsSuccessStatusCode,
                HttpStatusCode         = faultError == null ? webTask.Result.StatusCode : HttpStatusCode.NotFound,
                CacheUntil             = cacheTime,
                EveErrorCode           = eveErrorCode,
                EveErrorText           = eveErrorText
            };

            if (faultError == null)
            {
                // cache it
                SetCacheEntry(cacheKey, eveResult);
            }

            return(eveResult);
        }