Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Add support for bulk download campaign data for accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantvithani committed Nov 28, 2018
1 parent 6383e04 commit 2ce0617
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/bing/ads/api/v12/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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')
wsdl_path = File.join(root_v12_path, 'constants', 'wsdl.yml')

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
Expand Down
5 changes: 5 additions & 0 deletions lib/bing/ads/api/v12/constants/bulk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
download_entities:
campaigns: 1
ad_groups: 2
ads: 3
keywords: 4
62 changes: 62 additions & 0 deletions lib/bing/ads/api/v12/data/bulk_request.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 13 additions & 13 deletions lib/bing/ads/api/v12/data/report_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/bing/ads/api/v12/services/bulk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2ce0617

Please sign in to comment.