Class: MicrosoftAds::CampaignManagementClient

Inherits:
Object
  • Object
show all
Defined in:
app/services/microsoft_ads/campaign_management_client.rb

Overview

Minimal SOAP client for the Microsoft Advertising (Bing Ads) Campaign
Management API v13 — just the one read operation we need,
GetCampaignsByAccountId, which MicrosoftAdsCampaignSyncWorker uses to
mirror campaigns into the sources table. The direct parallel to
OpenaiAds::AdvertiserApiClient#list_campaigns, but SOAP rather than REST.

Why hand-rolled SOAP: the Microsoft Advertising API is SOAP-only (v13)
and ships no Ruby SDK (C#/Java/PHP/Python only). Rather than pull in a
generic SOAP gem for a single fixed operation, we POST a static envelope
and parse the response with Nokogiri.

Every call carries the four credentials Microsoft requires as SOAP header
elements: the developer token, an OAuth access token from
OauthService (the AuthenticationToken), the CustomerId
(cid), and the CustomerAccountId (aid).

Constant Summary collapse

SERVICE_URL =

Production Campaign Management v13 service endpoint. (Sandbox swaps
campaign.api.bingadscampaign.api.sandbox.bingads.)

'https://campaign.api.bingads.microsoft.com/Api/Advertiser/CampaignManagement/v13/CampaignManagementService.svc'
NS =

XML namespace for the v13 Campaign Management service.

'https://bingads.microsoft.com/CampaignManagement/v13'
CAMPAIGN_TYPES =

Space-delimited CampaignType flags. GetCampaignsByAccountId requires an
explicit type filter; this covers every campaign type a search
advertiser like WarmlyYours might run. Types the account isn't entitled
to simply return nothing rather than erroring.

'Search Shopping DynamicSearchAds Audience PerformanceMax'

Instance Method Summary collapse

Constructor Details

#initialize(developer_token:, access_token:, customer_id:, account_id:) ⇒ CampaignManagementClient

Returns a new instance of CampaignManagementClient.

Parameters:

  • developer_token (String)

    Microsoft Advertising developer token

  • access_token (String)

    OAuth access token (the AuthenticationToken header)

  • customer_id (String, Integer)

    CustomerId header (cid)

  • account_id (String, Integer)

    CustomerAccountId header + AccountId body (aid)



41
42
43
44
45
46
# File 'app/services/microsoft_ads/campaign_management_client.rb', line 41

def initialize(developer_token:, access_token:, customer_id:, account_id:)
  @developer_token = developer_token
  @access_token    = access_token
  @customer_id     = customer_id
  @account_id      = 
end

Instance Method Details

#list_campaignsArray<Hash>

Fetch every campaign on the account.

Returns:

  • (Array<Hash>)

    one { 'id' =>, 'name' =>, 'status' => } per campaign

Raises:

  • (StandardError)

    on a transport error or a SOAP fault / operation error



52
53
54
55
56
57
58
59
60
61
# File 'app/services/microsoft_ads/campaign_management_client.rb', line 52

def list_campaigns
  response = post(get_campaigns_envelope)

  unless response.success?
    raise "MicrosoftAds::CampaignManagementClient: GetCampaignsByAccountId failed " \
          "(HTTP #{response.status}): #{fault_message(response.body)}"
  end

  parse_campaigns(response.body)
end