bing_ads_api 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/README.md +57 -0
  2. data/lib/ads_common_for_bing_ads.rb +49 -0
  3. data/lib/ads_common_for_bing_ads/api_config.rb +41 -0
  4. data/lib/ads_common_for_bing_ads/auth/client_login_handler.rb +168 -0
  5. data/lib/ads_common_for_bing_ads/build/savon_registry.rb +15 -0
  6. data/lib/ads_common_for_bing_ads/parameters_validator.rb +36 -0
  7. data/lib/ads_common_for_bing_ads/savon_headers_base_header_handler.rb +9 -0
  8. data/lib/ads_common_for_bing_ads/savon_service.rb +141 -0
  9. data/lib/bing_ads_api.rb +175 -0
  10. data/lib/bing_ads_api/api_config.rb +162 -0
  11. data/lib/bing_ads_api/client_login_header_handler.rb +21 -0
  12. data/lib/bing_ads_api/credential_handler.rb +91 -0
  13. data/lib/bing_ads_api/errors.rb +564 -0
  14. data/lib/bing_ads_api/report_header_handler.rb +46 -0
  15. data/lib/bing_ads_api/report_utils.rb +202 -0
  16. data/lib/bing_ads_api/v7/administration_service.rb +37 -0
  17. data/lib/bing_ads_api/v7/administration_service_registry.rb +30 -0
  18. data/lib/bing_ads_api/v7/campaign_management_service.rb +398 -0
  19. data/lib/bing_ads_api/v7/campaign_management_service_registry.rb +30 -0
  20. data/lib/bing_ads_api/v7/customer_billing_service.rb +58 -0
  21. data/lib/bing_ads_api/v7/customer_billing_service_registry.rb +30 -0
  22. data/lib/bing_ads_api/v7/customer_management_service.rb +98 -0
  23. data/lib/bing_ads_api/v7/customer_management_service_registry.rb +30 -0
  24. data/lib/bing_ads_api/v7/notification_service.rb +38 -0
  25. data/lib/bing_ads_api/v7/notification_service_registry.rb +30 -0
  26. data/lib/bing_ads_api/v7/reporting_service.rb +38 -0
  27. data/lib/bing_ads_api/v7/reporting_service_registry.rb +30 -0
  28. data/lib/bing_ads_api/v8/ad_intelligence_service.rb +86 -0
  29. data/lib/bing_ads_api/v8/ad_intelligence_service_registry.rb +30 -0
  30. data/lib/bing_ads_api/v8/administration_service.rb +38 -0
  31. data/lib/bing_ads_api/v8/administration_service_registry.rb +30 -0
  32. data/lib/bing_ads_api/v8/bulk_service.rb +42 -0
  33. data/lib/bing_ads_api/v8/bulk_service_registry.rb +30 -0
  34. data/lib/bing_ads_api/v8/campaign_management_service.rb +390 -0
  35. data/lib/bing_ads_api/v8/campaign_management_service_registry.rb +30 -0
  36. data/lib/bing_ads_api/v8/customer_billing_service.rb +62 -0
  37. data/lib/bing_ads_api/v8/customer_billing_service_registry.rb +30 -0
  38. data/lib/bing_ads_api/v8/customer_management_service.rb +162 -0
  39. data/lib/bing_ads_api/v8/customer_management_service_registry.rb +30 -0
  40. data/lib/bing_ads_api/v8/notification_service.rb +38 -0
  41. data/lib/bing_ads_api/v8/notification_service_registry.rb +30 -0
  42. data/lib/bing_ads_api/v8/optimizer_service.rb +46 -0
  43. data/lib/bing_ads_api/v8/optimizer_service_registry.rb +30 -0
  44. data/lib/bing_ads_api/v8/reporting_service.rb +38 -0
  45. data/lib/bing_ads_api/v8/reporting_service_registry.rb +30 -0
  46. data/lib/bing_ads_api/version.rb +5 -0
  47. data/rakefile.rb +54 -0
  48. metadata +156 -0
@@ -0,0 +1,175 @@
1
+ require 'savon'
2
+ require 'httpi'
3
+ require 'active_support/inflector'
4
+
5
+ require 'ads_common_for_bing_ads'
6
+ require 'ads_common_for_bing_ads/api_config'
7
+ require 'ads_common_for_bing_ads/parameters_validator'
8
+ require 'ads_common_for_bing_ads/auth/client_login_handler'
9
+ require 'ads_common_for_bing_ads/savon_service'
10
+ require 'ads_common_for_bing_ads/savon_headers_base_header_handler'
11
+ require 'bing_ads_api/api_config'
12
+ require 'bing_ads_api/client_login_header_handler'
13
+ require 'bing_ads_api/credential_handler'
14
+ require 'bing_ads_api/errors'
15
+ require 'bing_ads_api/report_utils'
16
+
17
+ module BingAdsApi
18
+
19
+ # Wrapper class that serves as the main point of access for all the API usage.
20
+ #
21
+ # Holds all the services, as well as login credentials.
22
+ #
23
+ class Api < AdsCommonForBingAds::Api
24
+
25
+ # Constructor for API.
26
+ def initialize(provided_config = nil)
27
+ super(provided_config)
28
+ @credential_handler = BingAdsApi::CredentialHandler.new(@config)
29
+ end
30
+
31
+ # Getter for the API service configurations
32
+ def api_config()
33
+ BingAdsApi::ApiConfig
34
+ end
35
+
36
+ # Retrieve correct soap_header_handler.
37
+ #
38
+ # Args:
39
+ # - auth_handler: instance of an AdsCommonForBingAds::Auth::BaseHandler subclass to
40
+ # handle authentication
41
+ # - version: intended API version
42
+ # - header_ns: header namespace
43
+ # - default_ns: default namespace
44
+ #
45
+ # Returns:
46
+ # - SOAP header handler
47
+ #
48
+ def soap_header_handler(auth_handler, version, header_ns, default_ns)
49
+ auth_method = @config.read('authentication.method', :CLIENTLOGIN)
50
+ handler_class = case auth_method
51
+ when :CLIENTLOGIN then BingAdsApi::ClientLoginHeaderHandler
52
+ when :OAUTH, :OAUTH2 then AdsCommonForBingAds::SavonHeaders::OAuthHeaderHandler
53
+ end
54
+ return handler_class.new(@credential_handler, auth_handler, header_ns, default_ns, version)
55
+ end
56
+
57
+ # Helper method to provide a simple way of doing an MCC-level operation
58
+ # without the need to change credentials. Executes a block of code as an
59
+ # MCC-level operation and/or returns the current status of the property.
60
+ #
61
+ # Args:
62
+ # - accepts a block, which it will execute as an MCC-level operation
63
+ #
64
+ # Returns:
65
+ # - block execution result, if block given
66
+ # - boolean indicating whether MCC-level operations are currently
67
+ # enabled or disabled, if no block provided
68
+ #
69
+ def use_mcc(&block)
70
+ return (block_given?) ?
71
+ run_with_temporary_flag(:@use_mcc, true, block) :
72
+ @credential_handler.use_mcc
73
+ end
74
+
75
+ # Helper method to provide a simple way of doing an MCC-level operation
76
+ # without the need to change credentials. Sets the value of the property
77
+ # that controls whether MCC-level operations are enabled or disabled.
78
+ #
79
+ # Args:
80
+ # - value: the new value for the property (boolean)
81
+ #
82
+ def use_mcc=(value)
83
+ @credential_handler.use_mcc = value
84
+ end
85
+
86
+ # Helper method to provide a simple way of doing a validate-only operation
87
+ # without the need to change credentials. Executes a block of code as an
88
+ # validate-only operation and/or returns the current status of the property.
89
+ #
90
+ # Args:
91
+ # - accepts a block, which it will execute as a validate-only operation
92
+ #
93
+ # Returns:
94
+ # - block execution result, if block given
95
+ # - boolean indicating whether validate-only operations are currently
96
+ # enabled or disabled, if no block provided
97
+ #
98
+ def validate_only(&block)
99
+ return (block_given?) ?
100
+ run_with_temporary_flag(:@validate_only, true, block) :
101
+ @credential_handler.validate_only
102
+ end
103
+
104
+ # Helper method to provide a simple way of performing validate-only
105
+ # operations. Sets the value of the property
106
+ # that controls whether validate-only operations are enabled or disabled.
107
+ #
108
+ # Args:
109
+ # - value: the new value for the property (boolean)
110
+ #
111
+ def validate_only=(value)
112
+ @credential_handler.validate_only = value
113
+ end
114
+
115
+ # Helper method to provide a simple way of performing requests with support
116
+ # for partial failures. Executes a block of code with partial failures
117
+ # enabled and/or returns the current status of the property.
118
+ #
119
+ # Args:
120
+ # - accepts a block, which it will execute as a partial failure operation
121
+ #
122
+ # Returns:
123
+ # - block execution result, if block given
124
+ # - boolean indicating whether partial failure operations are currently
125
+ # enabled or disabled, if no block provided
126
+ #
127
+ def partial_failure(&block)
128
+ return (block_given?) ?
129
+ run_with_temporary_flag(:@partial_failure, true, block) :
130
+ @credential_handler.partial_failure
131
+ end
132
+
133
+ # Helper method to provide a simple way of performing requests with support
134
+ # for partial failures.
135
+ #
136
+ # Args:
137
+ # - value: the new value for the property (boolean)
138
+ #
139
+ def partial_failure=(value)
140
+ @credential_handler.partial_failure = value
141
+ end
142
+
143
+ # Returns an instance of ReportUtils object with all utilities relevant to
144
+ # the reporting.
145
+ #
146
+ # Args:
147
+ # - version: version of the API to use (optional).
148
+ #
149
+ def report_utils(version = nil)
150
+ version = api_config.default_version if version.nil?
151
+ # Check if version exists.
152
+ if !api_config.versions.include?(version)
153
+ raise AdsCommonForBingAds::Errors::Error, "Unknown version '%s'" % version
154
+ end
155
+ return BingAdsApi::ReportUtils.new(self, version)
156
+ end
157
+
158
+ private
159
+
160
+ # Executes block with a temporary flag set to a given value. Returns block
161
+ # result.
162
+ def run_with_temporary_flag(flag_name, flag_value, block)
163
+ previous = @credential_handler.instance_variable_get(flag_name)
164
+ @credential_handler.instance_variable_set(flag_name, flag_value)
165
+ begin
166
+ return block.call
167
+ ensure
168
+ @credential_handler.instance_variable_set(flag_name, previous)
169
+ end
170
+ end
171
+
172
+ end
173
+
174
+
175
+ end
@@ -0,0 +1,162 @@
1
+ # encoding: utf-8
2
+ # Helper methods for loading and managing the available services in the Bing Ads API.
3
+
4
+ require 'bing_ads_api/version'
5
+
6
+ module BingAdsApi
7
+
8
+ # Contains helper methods for loading and managing the available services.
9
+ module ApiConfig
10
+
11
+ # Inherit from AdsCommonForBingAds::ApiConfig
12
+ class << ApiConfig
13
+ include AdsCommonForBingAds::ApiConfig
14
+ end
15
+
16
+ # Set defaults
17
+ DEFAULT_VERSION = :v8
18
+ DEFAULT_ENVIRONMENT = :PRODUCTION
19
+ LATEST_VERSION = :v8
20
+
21
+ # Set other constants
22
+ API_NAME = 'BingAdsApi'
23
+ DEFAULT_CONFIG_FILENAME = 'bing_ads_api.yml'
24
+
25
+ # Configure the services available to each version
26
+ @@service_config = {
27
+ :v7 => [
28
+ :AdministrationService,
29
+ :CampaignManagementService,
30
+ :CustomerBillingService,
31
+ :CustomerManagementService,
32
+ :NotificationService,
33
+ :ReportingService
34
+ ],
35
+ :v8 => [
36
+ :AdIntelligenceService,
37
+ :AdministrationService,
38
+ :BulkService,
39
+ :CampaignManagementService,
40
+ :CustomerBillingService,
41
+ :CustomerManagementService,
42
+ :NotificationService,
43
+ :OptimizerService,
44
+ :ReportingService
45
+ ]
46
+ }
47
+
48
+ # Configure the different environments, with the base URL for each one
49
+ @@environment_config = {
50
+ :PRODUCTION => {
51
+ :oauth_scope => '',
52
+ :header_ns => 'https://adcenter.microsoft.com/api/adcenter/',
53
+ :v7 => '',
54
+ :v8 => ''
55
+ },
56
+ :SANDBOX => {
57
+ :oauth_scope => '',
58
+ :header_ns => 'https://adcenter.microsoft.com/api/adcenter/',
59
+ :v7 => '',
60
+ :v8 => ''
61
+ }
62
+ }
63
+
64
+ # Configure the subdirectories for each version / service pair.
65
+ # A missing pair means that only the base URL is used.
66
+ @@subdir_config = {}
67
+
68
+ @@address_config = {
69
+ :v8 => {
70
+ :AdIntelligenceService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v8/CampaignManagement/AdIntelligenceService.svc?wsdl", :SANDBOX => ""},
71
+ :AdministrationService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v8/Administration/AdministrationService.svc?wsdl", :SANDBOX => ""},
72
+ :BulkService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v8/CampaignManagement/BulkService.svc?wsdl", :SANDBOX => ""},
73
+ :CampaignManagementService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v8/CampaignManagement/CampaignManagementService.svc?wsdl", :SANDBOX => ""},
74
+ :CustomerBillingService => {:PRODUCTION => "https://sharedservices.adcenterapi.microsoft.com/Api/Billing/v8/CustomerBillingService.svc?wsdl", :SANDBOX => ""},
75
+ :CustomerManagementService => {:PRODUCTION => "https://sharedservices.adcenterapi.microsoft.com/Api/CustomerManagement/v8/CustomerManagementService.svc?wsdl", :SANDBOX => ""},
76
+ :NotificationService => {:PRODUCTION => "https://sharedservices.adcenterapi.microsoft.com/Api/Notification/v8/NotificationService.svc?wsdl", :SANDBOX => ""},
77
+ :OptimizerService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v8/Optimizer/OptimizerService.svc?wsdl", :SANDBOX => ""},
78
+ :ReportingService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc?wsdl", :SANDBOX => ""}
79
+ },
80
+ :v7 => {
81
+ :AdministrationService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v7/Administration/AdministrationService.svc?wsdl", :SANDBOX => "https://sandboxapi.adcenter.microsoft.com/Api/Advertiser/v7/Administration/AdministrationService.svc?wsdl"},
82
+ :CampaignManagementService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v7/CampaignManagement/CampaignManagementService.svc?wsdl", :SANDBOX => "https://sandboxapi.adcenter.microsoft.com/Api/Advertiser/v7/CampaignManagement/CampaignManagementService.svc?wsdl"},
83
+ :CustomerBillingService => {:PRODUCTION => "https://sharedservices.adcenterapi.microsoft.com/Api/Billing/v7/CustomerBillingService.svc?wsdl", :SANDBOX => "https://sharedservices-sbx.adcenterapi.microsoft.com/Api/Billing/v7/CustomerBillingService.svc?wsdl"},
84
+ :CustomerManagementService => {:PRODUCTION => "https://sharedservices.adcenterapi.microsoft.com/Api/CustomerManagement/v7/CustomerManagementService.svc?wsdl", :SANDBOX => "https://sharedservices-sbx.adcenterapi.microsoft.com/Api/CustomerManagement/v7/CustomerManagementService.svc?wsdl"},
85
+ :NotificationService => {:PRODUCTION => "https://sharedservices.adcenterapi.microsoft.com/Api/Notification/v8/NotificationService.svc?wsdl", :SANDBOX => ""},
86
+ :ReportingService => {:PRODUCTION => "https://adcenterapi.microsoft.com/Api/Advertiser/v7/Reporting/ReportingService.svc?wsdl", :SANDBOX => "https://sandboxapi.adcenter.microsoft.com/Api/Advertiser/v7/Reporting/ReportingService.svc?wsdl"}
87
+ }
88
+ }
89
+
90
+ # Auth constants for ClientLogin method.
91
+ #TODO: remove client_login_config
92
+ @@client_login_config = {
93
+ :AUTH_SERVER => 'https://www.microsoft.com',
94
+ :LOGIN_SERVICE_NAME => 'adcenter'
95
+ }
96
+
97
+ public
98
+
99
+ # Getters for constants and module variables.
100
+ def self.default_version
101
+ DEFAULT_VERSION
102
+ end
103
+
104
+ def self.default_environment
105
+ DEFAULT_ENVIRONMENT
106
+ end
107
+
108
+ def self.latest_version
109
+ LATEST_VERSION
110
+ end
111
+
112
+ def self.api_name
113
+ API_NAME
114
+ end
115
+
116
+ def self.service_config
117
+ @@service_config
118
+ end
119
+
120
+ def self.environment_config(environment, key)
121
+ return @@environment_config.include?(environment) ?
122
+ @@environment_config[environment][key] : nil
123
+ end
124
+
125
+ def self.address_config
126
+ @@address_config
127
+ end
128
+
129
+ def self.subdir_config
130
+ @@subdir_config
131
+ end
132
+
133
+ def self.client_login_config(key)
134
+ return @@client_login_config[key]
135
+ end
136
+
137
+ def self.default_config_filename
138
+ DEFAULT_CONFIG_FILENAME
139
+ end
140
+
141
+ def self.headers_config
142
+ @@headers_config
143
+ end
144
+
145
+ # Get the download URL for Ad Hoc reports.
146
+ #
147
+ # Args:
148
+ # - environment: the service environment to be used
149
+ # - version: the API version (as a symbol)
150
+ #
151
+ # Returns:
152
+ # - The endpoint URL (as a string)
153
+ #
154
+ def self.adhoc_report_download_url(environment, version)
155
+ base = get_wsdl_base(environment, version)
156
+ if base
157
+ base += 'reportdownload/%s' % version.to_s
158
+ end
159
+ return base
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,21 @@
1
+ # Handles SOAP headers and namespaces definition for ClientLogin type header.
2
+
3
+ module BingAdsApi
4
+ class ClientLoginHeaderHandler < AdsCommonForBingAds::SavonHeaders::BaseHeaderHandler
5
+
6
+ private
7
+
8
+ # Generates Bing Ads API specific request header with ClientLogin data.
9
+ def generate_request_header()
10
+ request_header = super()
11
+ #puts "--------------- generate_request_header >> request_header >>\n#{request_header}"
12
+ credentials = @credential_handler.credentials
13
+ #puts "--------------- generate_request_header >> credentials >>\n#{credentials}"
14
+ #request_header['authToken'] = @auth_handler.get_token(credentials)
15
+ credentials.each {|k,v| request_header[prepend_namespace(k.to_s.camelize)] = v}
16
+ request_header.select!{|k,_| ['ApplicationToken', 'CustomerAccountId', 'CustomerId', 'DeveloperToken', 'UserName', 'Password'].map{|h| prepend_namespace(h)}.include?(k.to_s)}
17
+ #puts "--------------- generate_request_header >> request_header final >>\n#{request_header}"
18
+ return request_header
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,91 @@
1
+ require 'bing_ads_api/api_config'
2
+
3
+ module BingAdsApi
4
+ class CredentialHandler < AdsCommonForBingAds::CredentialHandler
5
+ # Whether we're making MCC-level requests.
6
+ attr_accessor :use_mcc
7
+ # Whether we're making validate-only requests.
8
+ attr_accessor :validate_only
9
+ # Whether we're making partial failure requests.
10
+ attr_accessor :partial_failure
11
+
12
+ def initialize(config)
13
+ super(config)
14
+ @use_mcc = false
15
+ @validate_only = false
16
+ @partial_failure = false
17
+ end
18
+
19
+ # Create the list of credentials to be used by the auth handler for header
20
+ # generation.
21
+ def credentials(credentials_override = nil)
22
+ result = super(credentials_override)
23
+ #puts "credentials result=\n#{result}"
24
+ validate_headers_for_server(result)
25
+
26
+ extra_headers = {
27
+ 'userAgent' => generate_user_agent(),
28
+ 'developerToken' => result[:developer_token]
29
+ }
30
+ if !@use_mcc and result[:client_customer_id]
31
+ extra_headers['clientCustomerId'] = result[:client_customer_id]
32
+ end
33
+ extra_headers['validateOnly'] = 'true' if @validate_only
34
+ extra_headers['partialFailure'] = 'true' if @partial_failure
35
+ result[:extra_headers] = extra_headers
36
+ #puts "credentials result2=\n#{result}"
37
+ return result
38
+ end
39
+
40
+ # Generates string to use as user agent in headers.
41
+ def generate_user_agent(extra_ids = [])
42
+ agent_app = @config.read('authentication.user_agent')
43
+ extra_ids << ['AwApi-Ruby/%s' % BingAdsApi::ApiConfig::CLIENT_LIB_VERSION]
44
+ super(extra_ids, agent_app)
45
+ end
46
+
47
+ private
48
+
49
+ # Validates that the right credentials are being used for the chosen
50
+ # environment.
51
+ #
52
+ # Raises:
53
+ # - AdsCommonForBingAds::Error::EnvironmentMismatchError if sandbox credentials are
54
+ # being used for production or vice-versa.
55
+ # - BingAdsApi::Errors:BadCredentialsError if supplied credentials are not
56
+ # valid.
57
+ #
58
+ def validate_headers_for_server(credentials)
59
+ if credentials[:client_email]
60
+ raise BingAdsApi::Errors::BadCredentialsError, 'Deprecated header ' +
61
+ 'clientEmail is no longer supported, please use clientCustomerId'
62
+ end
63
+
64
+ client_customer_id = credentials[:client_customer_id]
65
+ if client_customer_id and
66
+ !(client_customer_id.is_a?(Integer) or
67
+ (client_customer_id =~ /^\d+(-\d+-\d+)?$/))
68
+ raise BingAdsApi::Errors::BadCredentialsError,
69
+ 'Invalid client customer ID: %s' % client_customer_id.to_s
70
+ end
71
+
72
+ token = credentials[:developer_token]
73
+ #sandbox_token = (token =~ /[a-zA-Z0-9]{12,}/)
74
+ #environment = @config.read('service.environment')
75
+ #case environment
76
+ # when :PRODUCTION
77
+ # if sandbox_token
78
+ # raise AdsCommonForBingAds::Errors::EnvironmentMismatchError,
79
+ # 'Attempting to connect to production with sandbox credentials.'
80
+ # end
81
+ # when :SANDBOX
82
+ # if (!sandbox_token)
83
+ # raise AdsCommonForBingAds::Errors::EnvironmentMismatchError,
84
+ # 'Attempting to connect to the sandbox with malformatted ' +
85
+ # 'credentials. Please check http://msdn.microsoft.com/en-US/library/aa983013 for details.'
86
+ # end
87
+ #end
88
+ return nil
89
+ end
90
+ end
91
+ end