/// <summary> /// You can submit a download request and the ReportingServiceManager will automatically /// return results. The ReportingServiceManager abstracts the details of checking for result file /// completion, and you don't have to write any code for results polling. /// </summary> /// <param name="reportingDownloadParameters"></param> private async Task BackgroundCompletionAsync(ReportingDownloadParameters reportingDownloadParameters) { // You may optionally cancel the DownloadFileAsync operation after a specified time interval. var tokenSource = new CancellationTokenSource(); tokenSource.CancelAfter(TimeoutInMilliseconds); var resultFilePath = await ReportingService.DownloadFileAsync(reportingDownloadParameters, tokenSource.Token); OutputStatusMessage(String.Format("Download result file: {0}\n", resultFilePath)); }
public async override Task RunAsync(AuthorizationData authorizationData) { try { ReportingService = new ReportingServiceManager(authorizationData); ReportingService.StatusPollIntervalInMilliseconds = 5000; // You can submit one of the example reports, or build your own. var reportRequest = GetAccountPerformanceReportRequest(authorizationData.AccountId); //var reportRequest = GetAdGroupPerformanceReportRequest(authorizationData.AccountId); //var reportRequest = GetAudiencePerformanceReportRequest(authorizationData.AccountId); //var reportRequest = GetCampaignPerformanceReportRequest(authorizationData.AccountId); //var reportRequest = GetKeywordPerformanceReportRequest(authorizationData.AccountId); //var reportRequest = GetProductDimensionPerformanceReportRequest(authorizationData.AccountId); //var reportRequest = GetProductPartitionPerformanceReportRequest(authorizationData.AccountId); //var reportRequest = GetSearchCampaignChangeHistoryReportRequest(authorizationData.AccountId); var reportingDownloadParameters = new ReportingDownloadParameters { ReportRequest = reportRequest, ResultFileDirectory = FileDirectory, ResultFileName = ResultFileName, OverwriteResultFile = true, }; // Option A - Background Completion with ReportingServiceManager // You can submit a download request and the ReportingServiceManager will automatically // return results. The ReportingServiceManager abstracts the details of checking for result file // completion, and you don't have to write any code for results polling. OutputStatusMessage("Awaiting Background Completion . . ."); await BackgroundCompletionAsync(reportingDownloadParameters); // Option B - Submit and Download with ReportingServiceManager // Submit the download request and then use the ReportingDownloadOperation result to // track status until the report is complete e.g. either using // TrackAsync or GetStatusAsync. //OutputStatusMessage("Awaiting Submit and Download . . ."); //await SubmitAndDownloadAsync(reportRequest); // Option C - Download Results with ReportingServiceManager // If for any reason you have to resume from a previous application state, // you can use an existing download request identifier and use it // to download the result file. // For example you might have previously retrieved a request ID using SubmitDownloadAsync. //var reportingDownloadOperation = await ReportingService.SubmitDownloadAsync(reportRequest); //var requestId = reportingDownloadOperation.RequestId; // Given the request ID above, you can resume the workflow and download the report. // The report request identifier is valid for two days. // If you do not download the report within two days, you must request the report again. //OutputStatusMessage("Awaiting Download Results . . ."); //await DownloadResultsAsync(requestId, authorizationData); } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(String.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Reporting service exceptions catch (FaultException <Microsoft.BingAds.Reporting.AdApiFaultDetail> ex) { OutputStatusMessage(String.Join("; ", ex.Detail.Errors.Select(error => String.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.Reporting.ApiFaultDetail> ex) { OutputStatusMessage(String.Join("; ", ex.Detail.OperationErrors.Select(error => String.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(String.Join("; ", ex.Detail.BatchErrors.Select(error => String.Format("{0}: {1}", error.Code, error.Message)))); } catch (ReportingOperationInProgressException ex) { OutputStatusMessage("The result file for the reporting operation is not yet available for download."); OutputStatusMessage(ex.Message); } catch (ReportingOperationCouldNotBeCompletedException ex) { OutputStatusMessage(String.Format("ReportingOperationCouldNotBeCompletedException Message: {0}", ex.Message)); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }