From 2ce0617218f2fd0d887ced471c70e9c022cb50b8 Mon Sep 17 00:00:00 2001 From: Prashant Vithani Date: Wed, 28 Nov 2018 22:36:12 +0530 Subject: [PATCH] Add support for bulk download campaign data for accounts --- lib/bing/ads/api/v12/constants.rb | 2 + lib/bing/ads/api/v12/constants/bulk.yml | 5 ++ lib/bing/ads/api/v12/data/bulk_request.rb | 62 +++++++++++++++++++++ lib/bing/ads/api/v12/data/report_request.rb | 26 ++++----- lib/bing/ads/api/v12/services/bulk.rb | 21 +++++++ 5 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 lib/bing/ads/api/v12/constants/bulk.yml create mode 100644 lib/bing/ads/api/v12/data/bulk_request.rb diff --git a/lib/bing/ads/api/v12/constants.rb b/lib/bing/ads/api/v12/constants.rb index 24dfa9f..88c2673 100644 --- a/lib/bing/ads/api/v12/constants.rb +++ b/lib/bing/ads/api/v12/constants.rb @@ -8,6 +8,7 @@ module Constants root_v12_path = File.expand_path('../', __FILE__) campaign_management_path = File.join(root_v12_path, 'constants', 'campaign_management.yml') + bulk_path = File.join(root_v12_path, 'constants', 'bulk.yml') languages_path = File.join(root_v12_path, 'constants', 'languages.yml') limits_path = File.join(root_v12_path, 'constants', 'limits.yml') time_zones_path = File.join(root_v12_path, 'constants', 'time_zones.yml') @@ -15,6 +16,7 @@ module Constants Persey.init(:default) do source :yaml, campaign_management_path, :campaign_management + source :yaml, bulk_path, :bulk source :yaml, languages_path, :languages source :yaml, limits_path, :limits source :yaml, time_zones_path, :time_zones diff --git a/lib/bing/ads/api/v12/constants/bulk.yml b/lib/bing/ads/api/v12/constants/bulk.yml new file mode 100644 index 0000000..c1cf0d5 --- /dev/null +++ b/lib/bing/ads/api/v12/constants/bulk.yml @@ -0,0 +1,5 @@ +download_entities: + campaigns: 1 + ad_groups: 2 + ads: 3 + keywords: 4 diff --git a/lib/bing/ads/api/v12/data/bulk_request.rb b/lib/bing/ads/api/v12/data/bulk_request.rb new file mode 100644 index 0000000..47da7a2 --- /dev/null +++ b/lib/bing/ads/api/v12/data/bulk_request.rb @@ -0,0 +1,62 @@ +module Bing + module Ads + module API + module V12 + module Data + class BulkRequest + + KEYS_ORDER = %i[ + account_ids + compression_type + data_scope + download_entities + download_file_type + format_version + last_sync_time_in_utc + ].freeze + + class << self + def prepare(bulk_request_row) + bulk_request_row[:account_ids] = prepare_account_ids( + account_ids: bulk_request_row[:account_ids] + ) + bulk_request_row[:download_entities] = + prepare_download_entities( + levels: bulk_request_row.delete(:levels), + ) + bulk_request_row.execpt!(:levels) + bulk_request = Bing::Ads::Utils.sort_keys(bulk_request_row, KEYS_ORDER) + namespace_identifier = Bing::Ads::API::V12::NAMESPACE_IDENTIFIER + { + get_campaigns_by_account_ids: Bing::Ads::Utils.camelcase_keys(bulk_request), + :attributes! => { + get_campaigns_by_account_ids: { + 'xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance', + 'i:type' => "#{namespace_identifier}:GetCampaignsByAccountIds" + } + } + } + end + + private + + def prepare_account_ids(account_ids:) + { + 'a1:long' => account_ids, + '@xmlns:a1' => 'http://schemas.microsoft.com/2003/10/Serialization/Arrays' + } + end + + def prepare_download_entities(levels:) + levels = %w[campaign] if levels.nil? || levels.empty? + return levels.map do |e| + Bing::Ads::API::V12.constants.bulk.download_entities.send(e) + end + end + end + end + end + end + end + end +end diff --git a/lib/bing/ads/api/v12/data/report_request.rb b/lib/bing/ads/api/v12/data/report_request.rb index 78b4ac7..7bb2297 100644 --- a/lib/bing/ads/api/v12/data/report_request.rb +++ b/lib/bing/ads/api/v12/data/report_request.rb @@ -8,19 +8,19 @@ class ReportRequest # @order # https://msdn.microsoft.com/en-us/library/bing-ads-reporting-reportrequest.aspx - KEYS_ORDER = [ - :exclude_column_headers, - :exclude_report_footer, - :exclude_report_header, - :format, - :language, - :report_name, - :return_only_complete_data, - :aggregation, - :columns, - :filter, - :scope, - :time + KEYS_ORDER = %i[ + exclude_column_headers + exclude_report_footer + exclude_report_header + format + language + report_name + return_only_complete_data + aggregation + columns + filter + scope + time ].freeze class << self diff --git a/lib/bing/ads/api/v12/services/bulk.rb b/lib/bing/ads/api/v12/services/bulk.rb index 40937aa..08eabbb 100644 --- a/lib/bing/ads/api/v12/services/bulk.rb +++ b/lib/bing/ads/api/v12/services/bulk.rb @@ -23,6 +23,27 @@ def get_bulk_upload_status(request_id) response_body.slice(:errors, :percent_complete, :request_status, :result_file_url) end + def download_campaigns_by_account_ids(options) + payload = Bing::Ads::API::V12::Data::BulkRequest.prepare(options) + response = call(__method__, payload) + response_body = response_body(response, __method__) + response_body.slice(:request_id, :download_url) + end + + # def download_campaigns_by_campaign_ids(options) + # options[:account_ids] ||= [@account_id] + # options[:download_entities] ||= [] + # response = call(__method__, response_mode: response_mode, account_id: account_id) + # response_body = response_body(response, __method__) + # response_body.slice(:request_id, :download_url) + # end + + def get_bulk_download_status(request_id) + response = call(__method__, request_id: request_id) + response_body = response_body(response, __method__) + response_body.slice(:errors, :percent_complete, :request_status, :result_file_url) + end + # TODO operations: https://msdn.microsoft.com/en-us/library/bing-ads-bulk-service-operations.aspx private