private FileUploadParameters CreateFileUploadParameters(EntityUploadParameters parameters)
        {
            CreateWorkingDirectoryIfNeeded();

            var fileName = Path.Combine(WorkingDirectory, Guid.NewGuid() + ".csv");

            using (var writer = new BulkFileWriter(fileName))
            {
                foreach (var entity in parameters.Entities)
                {
                    writer.WriteEntity(entity);
                }
            }

            var fileUploadParameters = new FileUploadParameters
            {
                UploadFilePath      = fileName,
                ResponseMode        = parameters.ResponseMode,
                ResultFileDirectory = parameters.ResultFileDirectory,
                ResultFileName      = parameters.ResultFileName,
                OverwriteResultFile = parameters.OverwriteResultFile,
                RenameUploadFileToMatchRequestId = true
            };

            return(fileUploadParameters);
        }
        /// <summary>
        /// Uploads the specified Bulk file.
        /// </summary>
        /// <param name="parameters">Determines various upload parameters, for example what file to upload. Please see <see cref="FileUploadParameters"/> for more information about available parameters.</param>
        /// <param name="progress">A class implementing <see cref="IProgress{T}"/> for tracking the percent complete progress information for the bulk operation.</param>
        /// <param name="cancellationToken">Cancellation token that can be used to cancel the tracking of the bulk operation on the client. Doesn't cancel the actual bulk operation on the server.</param>
        /// <returns>A task that represents the asynchronous operation. The task result will be an enumerable list of <see cref="BulkEntity"/> objects.</returns>
        /// <exception cref="FaultException{TDetail}">Thrown if a fault is returned from the Bing Ads service.</exception>
        /// <exception cref="OAuthTokenRequestException">Thrown if tokens can't be refreshed due to an error received from the Microsoft Account authorization server.</exception>
        /// <exception cref="BulkOperationCouldNotBeCompletedException{TStatus}">Thrown if the bulk operation has failed.</exception>
        public Task <string> UploadFileAsync(FileUploadParameters parameters, IProgress <BulkOperationProgressInfo> progress, CancellationToken cancellationToken)
        {
            ValidateSubmitUploadParameters(parameters.SubmitUploadParameters);

            ValidateUserData();

            return(UploadFileAsyncImpl(parameters, progress, cancellationToken));
        }
        private async Task <string> UploadFileAsyncImpl(FileUploadParameters parameters, IProgress <BulkOperationProgressInfo> progress, CancellationToken cancellationToken)
        {
            using (var operation = await SubmitUploadAsync(parameters.SubmitUploadParameters).ConfigureAwait(false))
            {
                await operation.TrackAsync(progress, cancellationToken).ConfigureAwait(false);

                return(await DownloadBulkFile(parameters.ResultFileDirectory, parameters.ResultFileName, parameters.OverwriteResultFile, operation).ConfigureAwait(false));
            }
        }
        private async Task <IEnumerable <BulkEntity> > UploadEntitiesAsyncImpl(IProgress <BulkOperationProgressInfo> progress, CancellationToken cancellationToken, FileUploadParameters fileUploadParameters)
        {
            var resultFile = await UploadFileAsyncImpl(fileUploadParameters, progress, cancellationToken).ConfigureAwait(false);

            return(new BulkFileReaderEnumerable(BulkFileReaderFactory.CreateBulkFileReader(resultFile, ResultFileType.Upload, DownloadFileType.Csv)));
        }
 /// <summary>
 /// Uploads the specified Bulk file.
 /// </summary>
 /// <param name="parameters">Determines various upload parameters, for example what file to upload. Please see <see cref="FileUploadParameters"/> for more information about available parameters.</param>
 /// <returns>A task that represents the asynchronous operation. The task result will be an enumerable list of <see cref="BulkEntity"/> objects.</returns>
 /// <exception cref="FaultException{TDetail}">Thrown if a fault is returned from the Bing Ads service.</exception>
 /// <exception cref="OAuthTokenRequestException">Thrown if tokens can't be refreshed due to an error received from the Microsoft Account authorization server.</exception>
 /// <exception cref="BulkOperationCouldNotBeCompletedException{TStatus}">Thrown if the bulk operation has failed.</exception>
 public Task <string> UploadFileAsync(FileUploadParameters parameters)
 {
     return(UploadFileAsync(parameters, null, CancellationToken.None));
 }
        /// <summary>
        /// Uploads a list of BulkAdGroupProductPartition objects that must represent
        /// a product partition tree for one ad group. You can include BulkAdGroupProductPartition records for more than one
        /// ad group per upload, however, this code example assumes that only one ad group is in scope. 
        /// </summary>
        /// <param name="partitionActions">The list of BulkAdGroupProductPartition objects that must represent
        /// a product partition tree.</param>
        /// <returns>The BulkAdGroupProductPartition upload results.</returns>
        private async Task<IList<BulkAdGroupProductPartition>> ApplyBulkProductPartitionActions(
            IList<BulkAdGroupProductPartition> partitionActions)
        {
            var fileUploadParameters = new FileUploadParameters
            {
                ResultFileDirectory = FileDirectory,
                ResultFileName = ResultFileName,
                OverwriteResultFile = true,
                UploadFilePath = FileDirectory + UploadFileName,
                ResponseMode = ResponseMode.ErrorsAndResults
            };

            Writer = new BulkFileWriter(FileDirectory + UploadFileName);
            foreach (var partitionAction in partitionActions)
            {
                Writer.WriteEntity(partitionAction);
            }
            Writer.Dispose();

            var bulkFilePath =
                await BulkService.UploadFileAsync(fileUploadParameters);
            Reader = new BulkFileReader(bulkFilePath, ResultFileType.Upload, FileType);

            var bulkEntities = Reader.ReadEntities().ToList();
            var bulkAdGroupProductPartitionResults = bulkEntities.OfType<BulkAdGroupProductPartition>().ToList();
            OutputBulkAdGroupProductPartitions(bulkAdGroupProductPartitionResults);

            Reader.Dispose();

            return bulkAdGroupProductPartitionResults;
        }
        /// <summary>
        /// Writes the specified entities to a local file and uploads the file. We could have uploaded directly
        /// without writing to file. This example writes to file as an exercise so that you can view the structure 
        /// of the bulk records being uploaded as needed. 
        /// </summary>
        /// <param name="uploadEntities"></param>
        /// <returns></returns>
        private async Task<BulkFileReader> UploadEntities(IEnumerable<BulkEntity> uploadEntities)
        {
            Writer = new BulkFileWriter(FileDirectory + UploadFileName);

            foreach (var entity in uploadEntities)
            {
                Writer.WriteEntity(entity);
            }

            Writer.Dispose();

            var fileUploadParameters = new FileUploadParameters
            {
                ResultFileDirectory = FileDirectory,
                CompressUploadFile = true,
                ResultFileName = ResultFileName,
                OverwriteResultFile = true,
                UploadFilePath = FileDirectory + UploadFileName,
                ResponseMode = ResponseMode.ErrorsAndResults
            };

            var bulkFilePath = await BulkService.UploadFileAsync(fileUploadParameters);

            return new BulkFileReader(bulkFilePath, ResultFileType.Upload, FileType);
        }