/// <summary> /// Gets the list of BulkAdGroupProductPartition that represent a product partition tree for the specified ad group. /// </summary> /// <param name="adGroupId">The identifier of the ad group whose product partition tree you want to get.</param> /// <returns>The BulkAdGroupProductPartition download results, filtered by the specified ad group ID.</returns> private async Task <IList <BulkAdGroupProductPartition> > GetBulkAdGroupProductPartitionTree(long adGroupId) { var downloadParameters = new DownloadParameters { Entities = BulkDownloadEntity.AdGroupProductPartitions, ResultFileDirectory = FileDirectory, ResultFileName = "download.csv", OverwriteResultFile = true, LastSyncTimeInUTC = null }; var bulkFilePath = await BulkService.DownloadFileAsync(downloadParameters); Reader = new BulkFileReader(bulkFilePath, ResultFileType.FullDownload, FileType); var bulkEntities = Reader.ReadEntities().ToList(); var bulkAdGroupProductPartitionResults = bulkEntities.OfType <BulkAdGroupProductPartition>().ToList(); Reader.Dispose(); IList <BulkAdGroupProductPartition> bulkAdGroupProductPartitions = new List <BulkAdGroupProductPartition>(); foreach (var bulkAdGroupProductPartitionResult in bulkAdGroupProductPartitionResults) { if (bulkAdGroupProductPartitionResult.AdGroupCriterion != null && bulkAdGroupProductPartitionResult.AdGroupCriterion.AdGroupId == adGroupId) { bulkAdGroupProductPartitions.Add(bulkAdGroupProductPartitionResult); } } return(bulkAdGroupProductPartitions); }
public void Import <T>(string source) where T : class { var documents = source.ExtractDocuments <T>(); var response = new BulkService <T>().SendBulk(documents, GetIndexName <T>()); response.Validate(); }
/// <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> > BulkApplyProductPartitionActions( 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)); }
/// <summary> /// Writes the specified entities to a local temporary file prior to upload. /// </summary> /// <param name="uploadEntities"></param> /// <returns></returns> protected async Task <List <BulkEntity> > UploadEntitiesAsync( IEnumerable <BulkEntity> uploadEntities, Progress <BulkOperationProgressInfo> progress) { // The system temp directory will be used if another working directory is not specified. If you are // using a cloud service such as Azure you'll want to ensure you do not exceed the file or directory limits. // You can specify a different working directory for each BulkServiceManager instance. BulkService.WorkingDirectory = FileDirectory; var entityUploadParameters = new EntityUploadParameters { Entities = uploadEntities, OverwriteResultFile = true, ResultFileDirectory = FileDirectory, ResultFileName = ResultFileName, ResponseMode = ResponseMode.ErrorsAndResults }; // The UploadEntitiesAsync method returns IEnumerable<BulkEntity>, so the result file will not // be accessible e.g. for CleanupTempFiles until you iterate over the result e.g. via ToList(). var resultEntities = (await BulkService.UploadEntitiesAsync(entityUploadParameters)).ToList(); // The CleanupTempFiles method removes all files (not sub-directories) within the working directory, // whether or not the files were created by this BulkServiceManager instance. BulkService.CleanupTempFiles(); return(resultEntities); }
/// <summary> /// You can submit a download or upload request and the BulkServiceManager will automatically /// return results. The BulkServiceManager abstracts the details of checking for result file /// completion, and you don't have to write any code for results polling. /// </summary> /// <param name="downloadParameters"></param> /// <param name="progress"></param> /// <returns></returns> private async Task BackgroundCompletionAsync( DownloadParameters downloadParameters, Progress <BulkOperationProgressInfo> progress) { // You may optionally cancel the DownloadFileAsync operation after a specified time interval. var tokenSource = new CancellationTokenSource(); tokenSource.CancelAfter(TimeoutInMilliseconds); var resultFilePath = await BulkService.DownloadFileAsync(downloadParameters, progress, tokenSource.Token); OutputStatusMessage(String.Format("Download result file: {0}\n", resultFilePath)); }
/// <summary> /// Submit the download request and then use the BulkDownloadOperation result to /// track status until the download is complete e.g. either using /// TrackAsync or GetStatusAsync. /// </summary> /// <param name="submitDownloadParameters"></param> /// <returns></returns> private async Task SubmitAndDownloadAsync(SubmitDownloadParameters submitDownloadParameters) { var bulkDownloadOperation = await BulkService.SubmitDownloadAsync(submitDownloadParameters); // You may optionally cancel the TrackAsync operation after a specified time interval. var tokenSource = new CancellationTokenSource(); tokenSource.CancelAfter(TimeoutInMilliseconds); BulkOperationStatus <DownloadStatus> downloadStatus = await bulkDownloadOperation.TrackAsync(null, tokenSource.Token); // You can use TrackAsync to poll until complete as shown above, // or use custom polling logic with GetStatusAsync as shown below. //BulkOperationStatus<DownloadStatus> downloadStatus; //var waitTime = new TimeSpan(0, 0, 5); //for (int i = 0; i < 24; i++) //{ // Thread.Sleep(waitTime); // downloadStatus = await bulkDownloadOperation.GetStatusAsync(); // if (downloadStatus.Status == DownloadStatus.Completed) // { // break; // } //} var resultFilePath = await bulkDownloadOperation.DownloadResultFileAsync( FileDirectory, ResultFileName, decompress : true, overwrite : true // Set this value true if you want to overwrite the same file. ); OutputStatusMessage(String.Format("Download result file: {0}\n", resultFilePath)); }