コード例 #1
0
        /// <summary>
        /// Wraps a <see cref="IResponseMessageCacheEntry"/> object as a <see cref="ResponseMessageCacheEntry"/>.
        /// </summary>
        public static async Task <ResponseMessageCacheEntry> WrapAsync(IResponseMessageCacheEntry entry)
        {
            var result = entry as ResponseMessageCacheEntry;

            if (result == null)
            {
                result = await CreateAsync(entry.ETag, entry.LastModified, entry.ToResponseMessage());
            }
            return(result);
        }
コード例 #2
0
ファイル: BasecampClient.cs プロジェクト: 22222/NBasecampApi3
        private async Task <IPageCollection <T> > GetPageCollectionAsync <T>(Uri uri, string accessToken, CancellationToken cancellationToken)
        {
            IResponseMessageCacheEntry responseMessageCacheEntry = null;

            if (responseMessageCache != null)
            {
                responseMessageCacheEntry = await responseMessageCache.GetAsync(uri);
            }

            await rateLimiter.WaitIfNecessaryAsync();

            string responseJson;
            Uri    nextUri;

            using (var requestMessage = CreateRequest(HttpMethod.Get, uri, accessToken: accessToken, responseMessageCacheEntry: responseMessageCacheEntry))
                using (var httpClient = httpClientPool.Create())
                    using (var responseMessage = await httpClient.SendAsync(requestMessage, cancellationToken))
                    {
                        var localResponseMessage = responseMessage;
                        if (responseMessage.StatusCode == System.Net.HttpStatusCode.NotModified && responseMessageCacheEntry != null)
                        {
                            localResponseMessage = responseMessageCacheEntry.ToResponseMessage();
                        }
                        else
                        {
                            await HttpResponseUtils.EnsureSuccessAsync(responseMessage);
                        }
                        responseJson = await localResponseMessage.Content.ReadAsStringAsync();

                        nextUri = HttpResponseUtils.ParseNextUriOrNull(localResponseMessage);

                        if (responseMessageCacheEntry == null)
                        {
                            await CacheResponseMessageAsync(uri, localResponseMessage, responseJson);
                        }
                    }

            var elements = JsonConvert.DeserializeObject <IReadOnlyCollection <T> >(responseJson);

            return(new PageCollection <T>(elements, nextUri, this));
        }