private async Task <TraktResponse <TResponseContentType> > QuerySingleItemAsync <TResponseContentType>(ExtendedHttpRequestMessage requestMessage, bool isCheckinRequest = false, CancellationToken cancellationToken = default) { HttpResponseMessage responseMessage = null; try { responseMessage = await ExecuteRequestAsync(requestMessage, isCheckinRequest, cancellationToken).ConfigureAwait(false); DebugAsserter.AssertResponseMessageIsNotNull(responseMessage); DebugAsserter.AssertHttpResponseCodeIsNotExpected(responseMessage.StatusCode, HttpStatusCode.NoContent, DebugAsserter.SINGLE_ITEM_RESPONSE_PRECONDITION_INVALID_STATUS_CODE); Stream responseContentStream = await ResponseMessageHelper.GetResponseContentStreamAsync(responseMessage).ConfigureAwait(false); DebugAsserter.AssertResponseContentStreamIsNotNull(responseContentStream); IObjectJsonReader <TResponseContentType> objectJsonReader = JsonFactoryContainer.CreateObjectReader <TResponseContentType>(); DebugAsserter.AssertObjectJsonReaderIsNotNull(objectJsonReader); TResponseContentType contentObject = await objectJsonReader.ReadObjectAsync(responseContentStream, cancellationToken).ConfigureAwait(false); bool hasValue = !EqualityComparer <TResponseContentType> .Default.Equals(contentObject, default); var response = new TraktResponse <TResponseContentType> { IsSuccess = true, HasValue = hasValue, Value = contentObject }; if (responseMessage.Headers != null) { ResponseHeaderParser.ParseResponseHeaderValues(response, responseMessage.Headers); } return(response); } catch (Exception ex) { if (_client.Configuration.ThrowResponseExceptions) { throw; } return(new TraktResponse <TResponseContentType> { IsSuccess = false, Exception = ex }); } finally { responseMessage?.Dispose(); } }
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(); } }