예제 #1
0
        private async Task <TraktPagedResponse <TResponseContentType> > QueryPagedListAsync <TResponseContentType>(ExtendedHttpRequestMessage requestMessage, CancellationToken cancellationToken = default)
        {
            HttpResponseMessage responseMessage = null;

            try
            {
                responseMessage = await ExecuteRequestAsync(requestMessage, false, cancellationToken).ConfigureAwait(false);

                DebugAsserter.AssertResponseMessageIsNotNull(responseMessage);
                DebugAsserter.AssertHttpResponseCodeIsNotExpected(responseMessage.StatusCode, HttpStatusCode.NoContent, DebugAsserter.PAGED_LIST_RESPONSE_PRECONDITION_INVALID_STATUS_CODE);
                Stream responseContentStream = await ResponseMessageHelper.GetResponseContentStreamAsync(responseMessage).ConfigureAwait(false);

                DebugAsserter.AssertResponseContentStreamIsNotNull(responseContentStream);
                IArrayJsonReader <TResponseContentType> arrayJsonReader = new ArrayJsonReader <TResponseContentType>();
                DebugAsserter.AssertArrayJsonReaderIsNotNull(arrayJsonReader);
                IEnumerable <TResponseContentType> contentObject = await arrayJsonReader.ReadArrayAsync(responseContentStream, cancellationToken).ConfigureAwait(false);

                var response = new TraktPagedResponse <TResponseContentType>
                {
                    IsSuccess = true,
                    HasValue  = contentObject != null,
                    Value     = contentObject
                };

                if (responseMessage.Headers != null)
                {
                    ResponseHeaderParser.ParsePagedResponseHeaderValues(response, responseMessage.Headers);
                }

                return(response);
            }
            catch (Exception ex)
            {
                if (_client.Configuration.ThrowResponseExceptions)
                {
                    throw;
                }

                return(new TraktPagedResponse <TResponseContentType> {
                    IsSuccess = false, Exception = ex
                });
            }
            finally
            {
                responseMessage?.Dispose();
            }
        }