protected async Task <T> PollForOperationCompletionAsync(IProgress <AsyncOperationStatus> progress, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { if (!this.client.IsAuthenticated || this.client.AuthenticationProvider.CurrentAccountSession.IsExpiring()) { await this.client.AuthenticateAsync(); } using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, this.monitorUrl)) { await this.client.AuthenticationProvider.AppendAuthHeaderAsync(httpRequestMessage); using (var responseMessage = await this.client.HttpProvider.SendAsync(httpRequestMessage)) { // The monitor service will return an Accepted status for any monitor operation that hasn't completed. // If we have a success code that isn't Accepted, the operation is complete. Return the resulting object. if (responseMessage.StatusCode != HttpStatusCode.Accepted && responseMessage.IsSuccessStatusCode) { using (var responseStream = await responseMessage.Content.ReadAsStreamAsync()) { return(this.client.HttpProvider.Serializer.DeserializeObject <T>(responseStream)); } } using (var responseStream = await responseMessage.Content.ReadAsStreamAsync()) { this.asyncOperationStatus = this.client.HttpProvider.Serializer.DeserializeObject <AsyncOperationStatus>(responseStream); if (this.asyncOperationStatus == null) { throw new OneDriveException( new Error { Code = OneDriveErrorCode.GeneralException.ToString(), Message = "Error retrieving monitor status." }); } if (string.Equals(this.asyncOperationStatus.Status, "cancelled", StringComparison.OrdinalIgnoreCase)) { return(default(T)); } if (string.Equals(this.asyncOperationStatus.Status, "failed", StringComparison.OrdinalIgnoreCase) || string.Equals(this.asyncOperationStatus.Status, "deleteFailed", StringComparison.OrdinalIgnoreCase)) { object message = null; if (this.asyncOperationStatus.AdditionalData != null) { this.asyncOperationStatus.AdditionalData.TryGetValue("message", out message); } throw new OneDriveException( new Error { Code = OneDriveErrorCode.GeneralException.ToString(), Message = message as string }); } if (progress != null) { progress.Report(this.asyncOperationStatus); } } } } await Task.Delay(Constants.PollingIntervalInMs, cancellationToken); } return(default(T)); }
public void ProgressCallback(AsyncOperationStatus asyncOperationStatus, out bool called) { this.httpResponseMessage.StatusCode = HttpStatusCode.OK; this.asyncMonitor.monitorUrl = AsyncMonitorTests.itemUrl; called = true; }