private async Task <IEnumerable <BulkEntity> > DownloadEntitiesAsyncImpl(DownloadParameters parameters, IProgress <BulkOperationProgressInfo> progress, CancellationToken cancellationToken)
        {
            var resultFile = await DownloadFileAsyncImpl(parameters, progress, cancellationToken).ConfigureAwait(false);

            var resultFileType = parameters.LastSyncTimeInUTC == null ? ResultFileType.FullDownload : ResultFileType.PartialDownload;

            return(new BulkFileReaderEnumerable(BulkFileReaderFactory.CreateBulkFileReader(resultFile, resultFileType, parameters.FileType)));
        }
        /// <summary>
        /// Downloads the specified Bulk entities to a local file.
        /// </summary>
        /// <param name="parameters">Determines various download parameters, for example what entities to download and where the file should be downloaded.
        /// Please see <see cref="DownloadParameters"/> 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 the local bulk file path.</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> DownloadFileAsync(DownloadParameters parameters, IProgress <BulkOperationProgressInfo> progress, CancellationToken cancellationToken)
        {
            ValidateUserData();

            ValidateSubmitDownloadParameters(parameters.SubmitDownloadParameters);

            return(DownloadFileAsyncImpl(parameters, progress, cancellationToken));
        }
        /// <summary>
        /// Downloads the specified Bulk entities.
        /// </summary>
        /// <param name="parameters">Determines various download parameters, for example what entities to download. Please see <see cref="DownloadParameters"/> 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 <IEnumerable <BulkEntity> > DownloadEntitiesAsync(DownloadParameters parameters, IProgress <BulkOperationProgressInfo> progress, CancellationToken cancellationToken)
        {
            ValidateSubmitDownloadParameters(parameters.SubmitDownloadParameters);

            ValidateUserData();

            return(DownloadEntitiesAsyncImpl(parameters, progress, cancellationToken));
        }
        private async Task <string> DownloadFileAsyncImpl(DownloadParameters parameters, IProgress <BulkOperationProgressInfo> progress, CancellationToken cancellationToken)
        {
            using (var operation = await SubmitDownloadAsyncImpl(parameters.SubmitDownloadParameters).ConfigureAwait(false))
            {
                await operation.TrackAsync(progress, cancellationToken).ConfigureAwait(false);

                return(await DownloadBulkFile(parameters.ResultFileDirectory, parameters.ResultFileName, parameters.OverwriteResultFile, operation).ConfigureAwait(false));
            }
        }
        /// <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 = ResultFileName,
                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;
        }
 /// <summary>
 /// Downloads the specified Bulk entities to a local file.
 /// </summary>
 /// <param name="parameters">Determines various download parameters, for example what entities to download and where the file should be downloaded.
 /// Please see <see cref="DownloadParameters"/> for more information about available parameters.</param>
 /// <returns>A task that represents the asynchronous operation. The task result will be the local bulk file path.</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> DownloadFileAsync(DownloadParameters parameters)
 {
     return(DownloadFileAsync(parameters, null, CancellationToken.None));
 }
 /// <summary>
 /// Downloads the specified Bulk entities.
 /// </summary>
 /// <param name="parameters">Determines various download parameters, for example what entities to download. Please see <see cref="DownloadParameters"/> 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 <IEnumerable <BulkEntity> > DownloadEntitiesAsync(DownloadParameters parameters)
 {
     return(DownloadEntitiesAsync(parameters, null, CancellationToken.None));
 }