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>
        /// 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>
        protected 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);
        }
        /// <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;
        }