Exemple #1
0
        /// <summary>
        /// Displays the login/consent UI and returns a Session object when user completes the auth flow.
        /// </summary>
        /// <param name="scopes">The list of offers that the application is requesting user consent for.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public async Task <LiveLoginResult> LoginAsync(IEnumerable <string> scopes)
        {
            if (scopes == null && this.scopes == null)
            {
                throw new ArgumentNullException("scopes");
            }

            if (scopes != null)
            {
                this.scopes = new List <string>(scopes);
            }

            bool onUiThread = Deployment.Current.CheckAccess();

            if (!onUiThread)
            {
                throw new InvalidOperationException(
                          string.Format(ResourceHelper.GetString("NotOnUiThread"), "LoginAsync"));
            }

            this.PrepareForAsync();

            return(await this.AuthenticateAsync(false /* silent flow */));
        }
Exemple #2
0
        /// <summary>
        /// Download a file to disk.
        /// </summary>
        /// <param name="path">relative or absolute uri to the file to be downloaded.</param>
        /// <param name="outputFile">the file that the downloaded content is written to.</param>
        /// <param name="ct">a token that is used to cancel the download operation.</param>
        /// <param name="progress">an object that is called to report the download's progress.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public Task <LiveDownloadOperationResult> BackgroundDownloadAsync(
            string path,
            IStorageFile outputFile,
            CancellationToken ct,
            IProgress <LiveOperationProgress> progress)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("path",
                                            String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("UrlInvalid"), "path"));
            }

            if (outputFile == null)
            {
                throw new ArgumentNullException("outputFile",
                                                String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("InvalidNullParameter"), "outputFile"));
            }

            return(this.InternalDownloadAsync(path, outputFile, ct, progress));
        }
Exemple #3
0
        /// <summary>
        /// Upload a file to the server.
        /// </summary>
        /// <param name="path">relative or absolute uri to the location where the file should be uploaded to.</param>
        /// <param name="fileName">name for the uploaded file.</param>
        /// <param name="inputFile">the file object of the local file to be uploaded.</param>
        /// <param name="option">an enum to specify the overwrite behavior if a file with the same name already exists.</param>
        /// <param name="ct">a token that is used to cancel the upload operation.</param>
        /// <param name="progress">an object that is called to report the upload's progress.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public Task <LiveOperationResult> BackgroundUploadAsync(
            string path,
            string fileName,
            IStorageFile inputFile,
            OverwriteOption option,
            CancellationToken ct,
            IProgress <LiveOperationProgress> progress)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("path",
                                            String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("UrlInvalid"), "path"));
            }

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException("fileName",
                                            String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("InvalidNullOrEmptyParameter"), "fileName"));
            }

            if (inputFile == null)
            {
                throw new ArgumentNullException("inputFile",
                                                String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("InvalidNullParameter"), "inputFile"));
            }

            ApiOperation op = new TailoredUploadOperation(
                this,
                this.GetResourceUri(path, ApiMethod.Upload),
                fileName,
                inputFile,
                option,
                progress,
                null);

            return(this.ExecuteApiOperation(op, ct));
        }
        /// <summary>
        /// Creates a background upload operation.
        /// </summary>
        /// <param name="path">relative or absolute uri to the location where the file should be uploaded to.</param>
        /// <param name="fileName">name for the uploaded file.</param>
        /// <param name="inputFile">the file object of the local file to be uploaded.</param>
        /// <param name="option">an enum to specify the overwrite behavior if a file with the same name already exists.</param>
        /// <returns>A Task object representing the asynchronous operation.</returns>
        public Task <LiveUploadOperation> CreateBackgroundUploadAsync(
            string path,
            string fileName,
            IStorageFile inputFile,
            OverwriteOption option)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("path",
                                            String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("UrlInvalid"), "path"));
            }

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException("fileName",
                                            String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("InvalidNullOrEmptyParameter"), "fileName"));
            }

            if (inputFile == null)
            {
                throw new ArgumentNullException("inputFile",
                                                String.Format(CultureInfo.CurrentUICulture, ResourceHelper.GetString("InvalidNullParameter"), "inputFile"));
            }

            var op = new CreateBackgroundUploadOperation(
                this,
                this.GetResourceUri(path, ApiMethod.Upload),
                fileName,
                inputFile,
                option);

            return(op.ExecuteAsync());
        }
Exemple #5
0
        private async Task <LiveOperationResult> ExecuteAsync(bool start, CancellationToken cancellationToken, IProgress <LiveOperationProgress> progressHandler)
        {
            LiveOperationResult opResult;
            Exception           error = null;

            try
            {
                var progressHandlerWrapper = new Progress <UploadOperation>(t =>
                {
                    if (progressHandler != null)
                    {
                        progressHandler.Report(
                            new LiveOperationProgress(
                                (long)t.Progress.BytesReceived,
                                (long)t.Progress.TotalBytesToReceive));
                    }
                });

                IAsyncOperationWithProgress <UploadOperation, UploadOperation> asyncOperation = start ? this.uploadOperation.StartAsync() : this.uploadOperation.AttachAsync();
                await asyncOperation.AsTask(cancellationToken, progressHandlerWrapper);
            }
            catch (TaskCanceledException)
            {
                throw;
            }
            catch (Exception exp)
            {
                // This might be an server error. We will read the response to determine the error message.
                error = exp;
            }

            IInputStream responseStream = this.uploadOperation.GetResultStreamAt(0);

            if (responseStream == null)
            {
                throw new LiveConnectException(
                          ApiOperation.ApiClientErrorCode,
                          ResourceHelper.GetString("ConnectionError"));
            }
            else
            {
                var  reader = new DataReader(responseStream);
                uint length = await reader.LoadAsync(MaxUploadResponseLength);

                opResult = ApiOperation.CreateOperationResultFrom(reader.ReadString(length), ApiMethod.Upload);

                if (opResult.Error != null)
                {
                    if (opResult.Error is LiveConnectException)
                    {
                        throw opResult.Error;
                    }
                    else if (error != null)
                    {
                        // If the error did not come from the api service,
                        // we'll just return the error thrown by the uploader.
                        throw error;
                    }
                }

                return(opResult);
            }
        }
        /// <summary>
        /// Upload a file to the server.
        /// </summary>
        /// <param name="path">relative or absolute uri to the location where the file should be uploaded to.</param>
        /// <param name="fileName">name for the uploaded file.</param>
        /// <param name="inputStream">Stream that contains the file content.</param>
        /// <param name="option">
        ///     a enum to specify the overwrite behavior if a file with the same name already exists.
        ///     Default is DoNotOverwrite.
        /// </param>
        /// <param name="ct">a cancellation token</param>
        /// <param name="progress">a progress event callback handler</param>
        public Task <LiveOperationResult> UploadAsync(
            string path,
            string fileName,
            Stream inputStream,
            OverwriteOption option,
            CancellationToken ct,
            IProgress <LiveOperationProgress> progress)
        {
            LiveUtility.ValidateNotNullOrWhiteSpaceString(path, "path");
            LiveUtility.ValidateNotNullParameter(inputStream, "inputStream");

            if (string.IsNullOrEmpty(fileName))
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("InvalidNullOrEmptyParameter"),
                                               "fileName");
                throw new ArgumentException(message, "fileName");
            }

            if (!inputStream.CanRead)
            {
                string message = String.Format(CultureInfo.CurrentUICulture,
                                               ResourceHelper.GetString("StreamNotReadable"),
                                               "inputStream");
                throw new ArgumentException(message, "inputStream");
            }

            if (this.Session == null)
            {
                throw new LiveConnectException(ApiOperation.ApiClientErrorCode, ResourceHelper.GetString("UserNotLoggedIn"));
            }

            var tcs = new TaskCompletionSource <LiveOperationResult>();
            var op  = new UploadOperation(
                this,
                this.GetResourceUri(path, ApiMethod.Upload),
                fileName,
                inputStream,
                option,
                progress,
                null);

            op.OperationCompletedCallback = (LiveOperationResult result) =>
            {
                if (result.IsCancelled)
                {
                    tcs.TrySetCanceled();
                }
                else if (result.Error != null)
                {
                    tcs.TrySetException(result.Error);
                }
                else
                {
                    tcs.TrySetResult(result);
                }
            };

            ct.Register(op.Cancel);
            op.Execute();

            return(tcs.Task);
        }