Esempio n. 1
0
        /// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        /// <returns>The HTTP response received from the server.</returns>
        async ValueTask <OperationState <AsyncPageable <DocumentStatusResult> > > IOperation <AsyncPageable <DocumentStatusResult> > .UpdateStateAsync(bool async, CancellationToken cancellationToken)
        {
            var update = async
                        ? await _serviceClient.GetTranslationStatusAsync(new Guid(Id), cancellationToken).ConfigureAwait(false)
                        : _serviceClient.GetTranslationStatus(new Guid(Id), cancellationToken);

            _createdOn           = update.Value.CreatedOn;
            _lastModified        = update.Value.LastModified;
            _status              = update.Value.Status;
            _documentsTotal      = update.Value.DocumentsTotal;
            _documentsFailed     = update.Value.DocumentsFailed;
            _documentsInProgress = update.Value.DocumentsInProgress;
            _documentsSucceeded  = update.Value.DocumentsSucceeded;
            _documentsNotStarted = update.Value.DocumentsNotStarted;
            _documentsCanceled   = update.Value.DocumentsCanceled;

            Response rawResponse = update.GetRawResponse();

            if (update.Value.Status == DocumentTranslationStatus.Succeeded || update.Value.Status == DocumentTranslationStatus.Failed)
            {
                return(OperationState <AsyncPageable <DocumentStatusResult> > .Success(rawResponse, CreateOperationValueAsync(CancellationToken.None)));
            }
            else if (update.Value.Status == DocumentTranslationStatus.ValidationFailed)
            {
                RequestFailedException requestFailedException = _diagnostics.CreateRequestFailedException(rawResponse, update.Value.Error, CreateAdditionalInformation(update.Value.Error));
                return(OperationState <AsyncPageable <DocumentStatusResult> > .Failure(rawResponse, requestFailedException));
            }

            return(OperationState <AsyncPageable <DocumentStatusResult> > .Pending(rawResponse));
        }
Esempio n. 2
0
        /// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        /// <returns>The HTTP response received from the server.</returns>
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (!_hasCompleted)
            {
                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(DocumentTranslationOperation)}.{nameof(UpdateStatus)}");
                scope.Start();

                try
                {
                    var update = async
                        ? await _serviceClient.GetTranslationStatusAsync(new Guid(Id), cancellationToken).ConfigureAwait(false)
                        : _serviceClient.GetTranslationStatus(new Guid(Id), cancellationToken);

                    _response = update.GetRawResponse();
                    _retryAfterHeaderValue = update.Headers.RetryAfter;

                    _createdOn           = update.Value.CreatedOn;
                    _lastModified        = update.Value.LastModified;
                    _status              = update.Value.Status;
                    _documentsTotal      = update.Value.DocumentsTotal;
                    _documentsFailed     = update.Value.DocumentsFailed;
                    _documentsInProgress = update.Value.DocumentsInProgress;
                    _documentsSucceeded  = update.Value.DocumentsSucceeded;
                    _documentsNotStarted = update.Value.DocumentsNotStarted;
                    _documentsCanceled   = update.Value.DocumentsCanceled;

                    if (update.Value.Status == DocumentTranslationStatus.Succeeded ||
                        update.Value.Status == DocumentTranslationStatus.Canceled ||
                        update.Value.Status == DocumentTranslationStatus.Failed)
                    {
                        _hasCompleted = true;
                        _hasValue     = true;
                    }
                    else if (update.Value.Status == DocumentTranslationStatus.ValidationFailed)
                    {
                        DocumentTranslationError error = (DocumentTranslationError)update.Value.Error;
                        _requestFailedException = _diagnostics.CreateRequestFailedException(_response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error));
                        _hasCompleted           = true;
                        throw _requestFailedException;
                    }
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            return(GetRawResponse());
        }