/// <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(AnalyzeOperation)}.{nameof(UpdateStatus)}"); scope.Start(); try { Response <AnalyzeJobState> update = async ? await _serviceClient.AnalyzeStatusAsync(Id, _showStats, null, null, cancellationToken).ConfigureAwait(false) : _serviceClient.AnalyzeStatus(Id, _showStats, null, null, cancellationToken); _response = update.GetRawResponse(); if (update.Value.Status == JobStatus.Succeeded) { // we need to first assign a vaue and then mark the operation as completed to avoid race conditions _value = Transforms.ConvertToAnalyzeOperationResult(update.Value, _idToIndexMap); NextLink = update.Value.NextLink; _hasCompleted = true; } else if (update.Value.Status == JobStatus.Failed) { _requestFailedException = await ClientCommon.CreateExceptionForFailedOperationAsync(async, _diagnostics, _response, update.Value.Errors).ConfigureAwait(false); _hasCompleted = true; throw _requestFailedException; } } catch (Exception e) { scope.Failed(e); throw; } } return(GetRawResponse()); }
async ValueTask <OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > > IOperation <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .UpdateStateAsync(bool async, CancellationToken cancellationToken) { Response <HealthcareJobStatusResult> response = async ? await _serviceClient.HealthStatusAsync(_jobId, _showStats, null, null, _idToIndexMap, cancellationToken).ConfigureAwait(false) : _serviceClient.HealthStatus(_jobId, _showStats, null, null, _idToIndexMap, cancellationToken); _createdOn = response.Value.CreatedOn; _expiresOn = response.Value.ExpiresOn; _lastModified = response.Value.LastModifiedOn; _status = response.Value.Status; Response rawResponse = response.GetRawResponse(); if (response.Value.Status == TextAnalyticsOperationStatus.Succeeded) { string nextLink = response.Value.NextLink; _firstPage = Page.FromValues(new List <AnalyzeHealthcareEntitiesResultCollection>() { response.Value.Result }, nextLink, rawResponse); return(OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .Success(rawResponse, CreateOperationValueAsync(CancellationToken.None))); } else if (response.Value.Status == TextAnalyticsOperationStatus.Failed) { RequestFailedException requestFailedException = await ClientCommon .CreateExceptionForFailedOperationAsync(async, _diagnostics, rawResponse, response.Value.Errors) .ConfigureAwait(false); return(OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .Failure(rawResponse, requestFailedException)); } else if (response.Value.Status == TextAnalyticsOperationStatus.Cancelled) { return(OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .Failure(rawResponse, new RequestFailedException("The operation was canceled so no value is available."))); } return(OperationState <AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> > .Pending(rawResponse)); }