/// <summary> /// Performs the BackgroundUploadOperation. /// </summary> public async Task <LiveOperationResult> ExecuteAsync() { Debug.Assert(this.status != OperationStatus.Completed, "Cannot execute on a completed operation."); if (this.status == OperationStatus.Cancelled) { return(await this.tcs.Task); } this.status = OperationStatus.Started; string filename = Path.GetFileName(this.uploadLocationOnDevice.OriginalString); Debug.Assert(!string.IsNullOrEmpty(filename)); Uri requestUri = this.client.GetResourceUri(this.path, ApiMethod.Upload); // TODO: Figure out how to mock out GetUploadLinkOperation elegantly so this can be tested. this.getUploadLinkOperation = new GetUploadLinkOperation( this.client, requestUri, filename, this.overwriteOption, null); LiveOperationResult uploadLinkResult = await this.getUploadLinkOperation.ExecuteAsync(); var uploadRequestUri = new Uri(uploadLinkResult.RawResult, UriKind.Absolute); var downloadLocationOnDevice = new Uri(this.uploadLocationOnDevice.OriginalString + ".json", UriKind.RelativeOrAbsolute); var builder = new BackgroundUploadRequestBuilder { RequestUri = uploadRequestUri, AccessToken = this.client.Session.AccessToken, UploadLocationOnDevice = this.uploadLocationOnDevice, DownloadLocationOnDevice = downloadLocationOnDevice, TransferPreferences = this.transferPreferences }; this.request = builder.Build(); var eventAdapter = new BackgroundUploadEventAdapter(this.backgroundTransferService, this.tcs); Task <LiveOperationResult> task = progress == null? eventAdapter.ConvertTransferStatusChangedToTask(this.request) : eventAdapter.ConvertTransferStatusChangedToTask(this.request, progress); Debug.Assert(this.tcs.Task == task, "EventAdapter returned a different task. This could affect cancel."); if (this.status != OperationStatus.Cancelled) { this.backgroundTransferService.Add(this.request); this.requestAddedToService = true; } LiveOperationResult result = await task; this.status = OperationStatus.Completed; return(result); }
public void TestCreateBackgroundTransferRequestForUpload() { const string accessToken = "accessToken"; var requestUri = new Uri("https://apis.live.net/v5.0/me/skydrive/files"); var uploadLocation = new Uri(@"\shared\transfers\myFile.txt", UriKind.RelativeOrAbsolute); var downloadLocation = new Uri(uploadLocation.OriginalString + ".json", UriKind.RelativeOrAbsolute); const TransferPreferences transferPreferences = TransferPreferences.AllowCellularAndBattery; var builder = new BackgroundUploadRequestBuilder() { AccessToken = accessToken, DownloadLocationOnDevice = downloadLocation, RequestUri = requestUri, UploadLocationOnDevice = uploadLocation, TransferPreferences = transferPreferences }; BackgroundTransferRequest request = builder.Build(); var expectedRequestUri = new Uri(requestUri.OriginalString + "?method=PUT"); Assert.AreEqual(expectedRequestUri, request.RequestUri, "request.RequestUri was not set properly."); Assert.AreEqual( downloadLocation, request.DownloadLocation, "request.DownloadLocation was not set properly."); Assert.AreEqual( uploadLocation, request.UploadLocation, "request.UploadLocation was not set properly."); Assert.AreEqual(request.Tag, BackgroundTransferHelper.Tag); Assert.AreEqual( "bearer " + accessToken, request.Headers["Authorization"], "Authorization header was not set properly."); Assert.AreEqual( Platform.GetLibraryHeaderValue(), request.Headers["X-HTTP-Live-Library"], "Library Header not set properly."); Assert.AreEqual(transferPreferences, request.TransferPreferences); }
/// <summary> /// Performs the BackgroundUploadOperation. /// </summary> public async Task<LiveOperationResult> ExecuteAsync() { Debug.Assert(this.status != OperationStatus.Completed, "Cannot execute on a completed operation."); if (this.status == OperationStatus.Cancelled) { return await this.tcs.Task; } this.status = OperationStatus.Started; string filename = Path.GetFileName(this.uploadLocationOnDevice.OriginalString); Debug.Assert(!string.IsNullOrEmpty(filename)); Uri requestUri = this.client.GetResourceUri(this.path, ApiMethod.Upload); // TODO: Figure out how to mock out GetUploadLinkOperation elegantly so this can be tested. this.getUploadLinkOperation = new GetUploadLinkOperation( this.client, requestUri, filename, this.overwriteOption, null); LiveOperationResult uploadLinkResult = await this.getUploadLinkOperation.ExecuteAsync(); var uploadRequestUri = new Uri(uploadLinkResult.RawResult, UriKind.Absolute); var downloadLocationOnDevice = new Uri(this.uploadLocationOnDevice.OriginalString + ".json", UriKind.RelativeOrAbsolute); var builder = new BackgroundUploadRequestBuilder { RequestUri = uploadRequestUri, AccessToken = (this.client.Session != null) ? this.client.Session.AccessToken : "", UploadLocationOnDevice = this.uploadLocationOnDevice, DownloadLocationOnDevice = downloadLocationOnDevice, TransferPreferences = this.transferPreferences }; this.request = builder.Build(); var eventAdapter = new BackgroundUploadEventAdapter(this.backgroundTransferService, this.tcs); Task<LiveOperationResult> task = progress == null ? eventAdapter.ConvertTransferStatusChangedToTask(this.request) : eventAdapter.ConvertTransferStatusChangedToTask(this.request, progress); Debug.Assert(this.tcs.Task == task, "EventAdapter returned a different task. This could affect cancel."); if (this.status != OperationStatus.Cancelled) { this.backgroundTransferService.Add(this.request); this.requestAddedToService = true; } LiveOperationResult result = await task; this.status = OperationStatus.Completed; return result; }