public async Task <TraktResponse <ITraktDevice> > GetDeviceAsync(DeviceRequest request, CancellationToken cancellationToken = default) { try { request.Validate(); ExtendedHttpRequestMessage requestMessage = await _requestMessageBuilder.Reset(request) .WithRequestBody(request.RequestBody) .DisableAPIVersionHeader() .DisableAPIClientIdHeader() .Build().ConfigureAwait(false); HttpResponseMessage responseMessage = await _client.HttpClientProvider.GetHttpClient().SendAsync(requestMessage, cancellationToken).ConfigureAwait(false); if (!responseMessage.IsSuccessStatusCode) { await ResponseErrorHandler.HandleErrorsAsync(requestMessage, responseMessage, isDeviceRequest : true, cancellationToken : cancellationToken).ConfigureAwait(false); } DebugAsserter.AssertResponseMessageIsNotNull(responseMessage); DebugAsserter.AssertHttpResponseCodeIsExpected(responseMessage.StatusCode, HttpStatusCode.OK, DebugAsserter.SINGLE_ITEM_RESPONSE_PRECONDITION_INVALID_STATUS_CODE); Stream responseContentStream = await ResponseMessageHelper.GetResponseContentStreamAsync(responseMessage).ConfigureAwait(false); IObjectJsonReader <ITraktDevice> objectJsonReader = JsonFactoryContainer.CreateObjectReader <ITraktDevice>(); ITraktDevice device = await objectJsonReader.ReadObjectAsync(responseContentStream, cancellationToken).ConfigureAwait(false); var response = new TraktResponse <ITraktDevice>() { Value = device, HasValue = device != null, IsSuccess = device != null }; if (responseMessage.Headers != null) { ResponseHeaderParser.ParseResponseHeaderValues(response, responseMessage.Headers); } _client.Authentication.Device = device; return(response); } catch (Exception ex) { if (_client.Configuration.ThrowResponseExceptions) { throw; } return(new TraktResponse <ITraktDevice> { IsSuccess = false, Exception = ex }); } }
private async Task <HttpResponseMessage> ExecuteRequestAsync(ExtendedHttpRequestMessage requestMessage, bool isCheckinRequest = false, CancellationToken cancellationToken = default) { HttpResponseMessage responseMessage = await _client.HttpClientProvider.GetHttpClient().SendAsync(requestMessage, cancellationToken).ConfigureAwait(false); if (!responseMessage.IsSuccessStatusCode) { await ResponseErrorHandler.HandleErrorsAsync(requestMessage, responseMessage, isCheckinRequest, cancellationToken : cancellationToken).ConfigureAwait(false); } return(responseMessage); }
private async Task <TraktResponse <ITraktAuthorization> > ExecuteAuthorizationRequestAsync <TRequestBodyType>(IPostRequest <ITraktAuthorization, TRequestBodyType> request, bool isRefreshRequest, CancellationToken cancellationToken = default) where TRequestBodyType : IRequestBody { try { request.Validate(); ExtendedHttpRequestMessage requestMessage = await _requestMessageBuilder.Reset(request) .WithRequestBody(request.RequestBody) .DisableAPIVersionHeader() .DisableAPIClientIdHeader() .Build().ConfigureAwait(false); HttpResponseMessage responseMessage = await _client.HttpClientProvider.GetHttpClient().SendAsync(requestMessage, cancellationToken).ConfigureAwait(false); HttpStatusCode responseCode = responseMessage.StatusCode; Stream responseContentStream; if (responseCode == HttpStatusCode.OK) { responseContentStream = await ResponseMessageHelper.GetResponseContentStreamAsync(responseMessage).ConfigureAwait(false); IObjectJsonReader <ITraktAuthorization> objectJsonReader = JsonFactoryContainer.CreateObjectReader <ITraktAuthorization>(); ITraktAuthorization traktAuthorization = await objectJsonReader.ReadObjectAsync(responseContentStream, cancellationToken).ConfigureAwait(false); var response = new TraktResponse <ITraktAuthorization>() { Value = traktAuthorization, HasValue = traktAuthorization != null, IsSuccess = traktAuthorization != null }; if (responseMessage.Headers != null) { ResponseHeaderParser.ParseResponseHeaderValues(response, responseMessage.Headers); } _client.Authentication.Authorization = traktAuthorization; return(response); } else if (responseCode == HttpStatusCode.Unauthorized) // Invalid code { responseContentStream = await ResponseMessageHelper.GetResponseContentStreamAsync(responseMessage).ConfigureAwait(false); IObjectJsonReader <ITraktError> objectJsonReader = JsonFactoryContainer.CreateObjectReader <ITraktError>(); ITraktError traktError = await objectJsonReader.ReadObjectAsync(responseContentStream, cancellationToken).ConfigureAwait(false); var errorMessage = traktError != null ? ($"error on {(isRefreshRequest ? "refreshing" : "retrieving")} oauth access token\nerror: {traktError.Error}\ndescription: {traktError.Description}") : "unknown error"; throw new TraktAuthenticationOAuthException(errorMessage) { StatusCode = responseCode, RequestUrl = requestMessage.Url, RequestBody = requestMessage.RequestBodyJson, ServerReasonPhrase = responseMessage.ReasonPhrase }; } await ResponseErrorHandler.HandleErrorsAsync(requestMessage, responseMessage, isAuthorizationRequest : true, cancellationToken : cancellationToken).ConfigureAwait(false); } catch (Exception ex) { if (_client.Configuration.ThrowResponseExceptions) { throw; } return(new TraktResponse <ITraktAuthorization> { IsSuccess = false, Exception = ex }); } return(new TraktResponse <ITraktAuthorization>()); }
public async Task <TraktNoContentResponse> RevokeAuthorizationAsync(AuthorizationRevokeRequest request, CancellationToken cancellationToken = default) { try { request.Validate(); ExtendedHttpRequestMessage requestMessage = await _requestMessageBuilder.Reset(request) .WithRequestBody(request.RequestBody) .DisableAPIVersionHeader() .DisableAPIClientIdHeader() .Build().ConfigureAwait(false); HttpResponseMessage responseMessage = await _client.HttpClientProvider.GetHttpClient().SendAsync(requestMessage, cancellationToken).ConfigureAwait(false); if (!responseMessage.IsSuccessStatusCode) { if (responseMessage.StatusCode == HttpStatusCode.Unauthorized) { string responseContent = responseMessage.Content != null ? await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false) : string.Empty; throw new TraktAuthenticationException("error on revoking access token") { RequestUrl = requestMessage.Url, RequestBody = requestMessage.RequestBodyJson, Response = responseContent, ServerReasonPhrase = responseMessage.ReasonPhrase }; } await ResponseErrorHandler.HandleErrorsAsync(requestMessage, responseMessage, isAuthorizationRevoke : true, cancellationToken : cancellationToken).ConfigureAwait(false); } else { _client.Authorization = TraktAuthorization.CreateWith(string.Empty, string.Empty); return(new TraktNoContentResponse { IsSuccess = true }); } throw new TraktAuthenticationException("unknown exception") { RequestUrl = requestMessage.Url, RequestBody = requestMessage.RequestBodyJson, StatusCode = responseMessage.StatusCode, ServerReasonPhrase = responseMessage.ReasonPhrase }; } catch (Exception ex) { if (_client.Configuration.ThrowResponseExceptions) { throw; } return(new TraktNoContentResponse { IsSuccess = false, Exception = ex }); } }
public async Task <TraktResponse <ITraktAuthorization> > PollForAuthorizationAsync(AuthorizationPollRequest request, CancellationToken cancellationToken = default) { try { request.Validate(); ExtendedHttpRequestMessage requestMessage = await _requestMessageBuilder.Reset(request) .WithRequestBody(request.RequestBody) .DisableAPIVersionHeader() .DisableAPIClientIdHeader() .Build().ConfigureAwait(false); HttpResponseMessage responseMessage; Stream responseContentStream; HttpStatusCode responseCode; string reasonPhrase = string.Empty; uint totalExpiredSeconds = 0; ITraktDevice device = request.RequestBody.Device; IObjectJsonReader <ITraktAuthorization> objectJsonReader = JsonFactoryContainer.CreateObjectReader <ITraktAuthorization>(); while (totalExpiredSeconds < device.ExpiresInSeconds) { responseMessage = await _client.HttpClientProvider.GetHttpClient().SendAsync(requestMessage, cancellationToken).ConfigureAwait(false); responseCode = responseMessage.StatusCode; reasonPhrase = responseMessage.ReasonPhrase; if (responseCode == HttpStatusCode.OK) // Success { responseContentStream = await ResponseMessageHelper.GetResponseContentStreamAsync(responseMessage).ConfigureAwait(false); ITraktAuthorization traktAuthorization = await objectJsonReader.ReadObjectAsync(responseContentStream, cancellationToken).ConfigureAwait(false); var response = new TraktResponse <ITraktAuthorization>() { Value = traktAuthorization, HasValue = traktAuthorization != null, IsSuccess = traktAuthorization != null }; if (responseMessage.Headers != null) { ResponseHeaderParser.ParseResponseHeaderValues(response, responseMessage.Headers); } _client.Authentication.Authorization = traktAuthorization; return(response); } else if (responseCode == HttpStatusCode.BadRequest) // Pending { await Task.Delay((int)device.IntervalInMilliseconds).ConfigureAwait(false); totalExpiredSeconds += device.IntervalInSeconds; requestMessage = await _requestMessageBuilder.Reset(request).WithRequestBody(request.RequestBody).Build().ConfigureAwait(false); continue; } await ResponseErrorHandler.HandleErrorsAsync(requestMessage, responseMessage, isInAuthorizationPolling : true, cancellationToken : cancellationToken).ConfigureAwait(false); break; } throw new TraktAuthenticationDeviceException("unknown exception") { RequestUrl = requestMessage.Url, RequestBody = requestMessage.RequestBodyJson, ServerReasonPhrase = reasonPhrase }; } catch (Exception ex) { if (_client.Configuration.ThrowResponseExceptions) { throw; } return(new TraktResponse <ITraktAuthorization> { IsSuccess = false, Exception = ex }); } }