コード例 #1
0
        /// <summary>
        ///     get as an asynchronous operation.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="uri">The URI.</param>
        /// <param name="accessToken">The access token.</param>
        /// <returns>Task&lt;T&gt;.</returns>
        public async Task <T> GetAsync <T>(Uri uri, string accessToken)
            where T : class, ICrestResource <T>
        {
            string responseContent = null;

            if (CacheLevel == CacheLevel.Default || CacheLevel == CacheLevel.CacheOnly)
            {
                responseContent = await Cache.LoadAsync(uri).ConfigureAwait(false);
            }
            var cached = responseContent != null;
            T   result;

            if (cached)
            {
                result             = Serializer.Deserialize <T>(responseContent);
                result.IsFromCache = true;
                return(result);
            }
            if (CacheLevel == CacheLevel.CacheOnly)
            {
                return(default(T));
            }

            // set up request
            var request = HttpRequestHelper.CreateRequest(uri);

            request.Accept      = ContentTypes.Get <T>(ThrowOnMissingContentType) + ";";
            request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
            request.Method      = WebRequestMethods.Http.Get;
            _trace.TraceEvent(TraceEventType.Error, 0, "Initiating Request: " + uri);
            var response = await requestAsync(request, accessToken).ConfigureAwait(false);

            // handle response
            var header = response.Headers;

            responseContent = await HttpRequestHelper.GetResponseContentAsync(response).ConfigureAwait(false);

            if (CacheLevel == CacheLevel.Default || CacheLevel == CacheLevel.Refresh)
            {
                await Cache.StoreAsync(uri, getCacheExpirationTime(header), responseContent).ConfigureAwait(false);
            }
            result = Serializer.Deserialize <T>(responseContent);
            result.ResponseHeaders = header;
            result.Uri             = uri;
            return(result);
        }
コード例 #2
0
        /// <summary>
        ///     Performs a request, and returns the response content.
        /// </summary>
        /// <typeparam name="T">Response type</typeparam>
        /// <param name="uri">URI to request</param>
        /// <param name="accessToken">CREST acces token</param>
        /// <returns>T.</returns>
        /// <exception cref="DeprecatedResourceException">The CREST resource is deprecated.</exception>
        /// <exception cref="EveCrestException">
        ///     Undefined error
        ///     or
        ///     or
        /// </exception>
        public async Task <T> RequestAsync <T>(Uri uri, string accessToken) where T : class, ICrestResource <T>
        {
            string data = null;

            if (CacheLevel == CacheLevel.Default || CacheLevel == CacheLevel.CacheOnly)
            {
                data = await Cache.LoadAsync(uri).ConfigureAwait(false);
            }
            bool cached = data != null;

            if (cached)
            {
                return(Serializer.Deserialize <T>(data));
            }
            if (CacheLevel == CacheLevel.CacheOnly)
            {
                return(default(T));
            }
            // set up request
            CrestMode      mode    = (accessToken == null) ? CrestMode.Public : CrestMode.Authenticated;
            HttpWebRequest request = HttpRequestHelper.CreateRequest(uri);

            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            request.Accept      = ContentTypes.Get <T>(ThrowOnMissingContentType) + ";";
            request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
            if (!String.IsNullOrEmpty(Charset))
            {
                request.Accept = request.Accept + " " + Charset;
            }
            if (!String.IsNullOrEmpty(XRequestedWith))
            {
                request.Headers.Add("X-Requested-With", XRequestedWith);
            }
            if (!String.IsNullOrEmpty(UserAgent))
            {
                request.UserAgent = UserAgent;
            }
            if (mode == CrestMode.Authenticated)
            {
                request.Headers.Add(HttpRequestHeader.Authorization, TokenType + " " + accessToken);
                _authedPool.WaitOne();
            }
            else
            {
                _publicPool.WaitOne();
            }

            _trace.TraceEvent(TraceEventType.Error, 0, "Initiating Request: " + uri);
            WebHeaderCollection header;

            try {
                HttpWebResponse response = await HttpRequestHelper.GetResponseAsync(request).ConfigureAwait(false);

                header = response.Headers;
                string deprecated = response.GetResponseHeader("X-Deprecated");

                if (!String.IsNullOrEmpty(deprecated))
                {
                    _trace.TraceEvent(TraceEventType.Warning, 0,
                                      "This CREST resource is deprecated. Please update to the newest EveLib version or notify the developers.");
                    if (ThrowOnDeprecated)
                    {
                        throw new DeprecatedResourceException("The CREST resource is deprecated.", response);
                    }
                }
                data = await HttpRequestHelper.GetResponseContentAsync(response).ConfigureAwait(false);

                // release semaphores
                if (mode == CrestMode.Authenticated)
                {
                    _authedPool.Release();
                }
                else
                {
                    _publicPool.Release();
                }
            }
            catch (WebException e) {
                // release semaphores
                if (mode == CrestMode.Authenticated)
                {
                    _authedPool.Release();
                }
                else
                {
                    _publicPool.Release();
                }

                _trace.TraceEvent(TraceEventType.Error, 0, "CREST Request Failed.");
                var response = (HttpWebResponse)e.Response;

                Stream responseStream = response.GetResponseStream();
                if (responseStream == null)
                {
                    throw new EveCrestException("Undefined error", e);
                }
                using (var reader = new StreamReader(responseStream)) {
                    data = reader.ReadToEnd();
                    if (response.StatusCode == HttpStatusCode.InternalServerError ||
                        response.StatusCode == HttpStatusCode.BadGateway)
                    {
                        throw new EveCrestException(data, e);
                    }
                    var error = Serializer.Deserialize <CrestError>(data);
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "Message: {0}, Key: {1}",
                                      "Exception Type: {2}, Ref ID: {3}", error.Message, error.Key, error.ExceptionType,
                                      error.RefId);
                    throw new EveCrestException(error.Message, e, error.Key, error.ExceptionType, error.RefId);
                }
            }
            if (CacheLevel == CacheLevel.Default || CacheLevel == CacheLevel.Refresh)
            {
                await Cache.StoreAsync(uri, getCacheExpirationTime(header), data).ConfigureAwait(false);
            }
            var result = Serializer.Deserialize <T>(data);

            result.ResponseHeaders = header;
            return(result);
        }