class Google::Ads::GoogleAds::GoogleAdsClient
Constants
- DEFAULT_CONFIG_FILENAME
- MAX_MESSAGE_LENGTH
- MAX_METADATA_SIZE
- SCOPE
Attributes
Allow setting the lookup_util
manually for users who use it before creating the client.
Public Class Methods
# File lib/google/ads/google_ads/google_ads_client.rb, line 55 def initialize(config_path = nil, &block) if block_given? @config = Google::Ads::GoogleAds::Config.new yield @config else if config_path.nil? config_path = ENV.fetch("GOOGLE_ADS_CONFIGURATION_FILE_PATH", File.join(ENV['HOME'], DEFAULT_CONFIG_FILENAME)) end unless File.exist?(config_path) raise ArgumentError, sprintf('No configuration file found at location "%s"', config_path) end file = File.read(config_path) eval_result = eval(file, binding, config_path) unless eval_result.instance_of?(Google::Ads::GoogleAds::Config) raise ArgumentError, sprintf( 'Configuration file did not produce expected type ' + 'Google::Ads::GoogleAds::Config, got "%s" instead', eval_result.class ) end @config = eval_result end begin @logger = create_default_logger rescue STDERR.puts( "Could not create default logger. Check your config file.") end end
Public Instance Methods
# File lib/google/ads/google_ads/google_ads_client.rb, line 92 def configure(&block) yield @config end
Decode a partial failure error from a response. See Google::Ads::GoogleAds::PartialFailureErrorDecoder
for full documentation.
# File lib/google/ads/google_ads/google_ads_client.rb, line 196 def decode_partial_failure_error(pfe) PartialFailureErrorDecoder.decode(pfe) end
# File lib/google/ads/google_ads/google_ads_client.rb, line 128 def endpoint target.split(":443").first end
# File lib/google/ads/google_ads/google_ads_client.rb, line 168 def enum Factories.version_alternate_for(:enums) end
Returns a reference to the FieldMaskUtil
class for ease of access.
# File lib/google/ads/google_ads/google_ads_client.rb, line 173 def field_mask() Google::Ads::GoogleAds::FieldMaskUtil end
# File lib/google/ads/google_ads/google_ads_client.rb, line 96 def load_environment_config # Generic variables @config.refresh_token = ENV.fetch("GOOGLE_ADS_REFRESH_TOKEN", @config.refresh_token) @config.client_id = ENV.fetch("GOOGLE_ADS_CLIENT_ID", @config.client_id) @config.client_secret = ENV.fetch("GOOGLE_ADS_CLIENT_SECRET", @config.client_secret) @config.keyfile = ENV.fetch("GOOGLE_ADS_JSON_KEY_FILE_PATH", @config.keyfile) @config.impersonate = ENV.fetch("GOOGLE_ADS_IMPERSONATED_EMAIL", @config.impersonate) @config.developer_token = ENV.fetch("GOOGLE_ADS_DEVELOPER_TOKEN", @config.developer_token) @config.login_customer_id = ENV.fetch("GOOGLE_ADS_LOGIN_CUSTOMER_ID", @config.login_customer_id) @config.linked_customer_id = ENV.fetch("GOOGLE_ADS_LINKED_CUSTOMER_ID", @config.linked_customer_id) @config.api_endpoint = ENV.fetch("GOOGLE_ADS_ENDPOINT", @config.api_endpoint) # Client library-specific variables @config.log_level = ENV.fetch("GOOGLE_ADS_RUBY_LOG_LEVEL", @config.log_level) @config.http_proxy = ENV.fetch("GOOGLE_ADS_RUBY_HTTP_PROXY", @config.http_proxy) end
Set the logger to use. This will only take effect on services fetched after setting this value.
# File lib/google/ads/google_ads/google_ads_client.rb, line 189 def logger=(logger) @logger = logger end
# File lib/google/ads/google_ads/google_ads_client.rb, line 137 def make_channel channel_args = { MAX_MESSAGE_LENGTH => 64*1024*1024, MAX_METADATA_SIZE => 16*1024*1024, } if !@config.use_insecure_channel call_creds = GRPC::Core::CallCredentials.new(get_credentials) chan_creds = GRPC::Core::ChannelCredentials.new.compose(call_creds) GRPC::Core::Channel.new(target, channel_args, chan_creds) else GRPC::Core::Channel.new(target, channel_args, :this_channel_is_insecure) end end
# File lib/google/ads/google_ads/google_ads_client.rb, line 164 def operation Factories.version_alternate_for(:operations) end
# File lib/google/ads/google_ads/google_ads_client.rb, line 152 def patch_lro_headers(class_to_return, headers) PatchLROHeaders.new(class_to_return, headers).call end
Returns a reference to the PathLookupUtil to generate resource names.
# File lib/google/ads/google_ads/google_ads_client.rb, line 183 def path(version = default_api_version) lookup_util.path(version) end
Return a resource or common entity for the provided entity type. For example, passing :Campaign will return an instantiated Campaign.
Raises ArgumentError if no entity can be found for the provided type.
# File lib/google/ads/google_ads/google_ads_client.rb, line 160 def resource Factories.version_alternate_for(:resources) end
Return a service for the provided entity type. For example, passing :Campaign will return an instantiated CampaignServiceClient.
Raises ArgumentError if no service can be found for the provided type.
# File lib/google/ads/google_ads/google_ads_client.rb, line 117 def service ServiceLookup.new( lookup_util, @logger, @config, make_channel, endpoint, deprecator, ).call end
# File lib/google/ads/google_ads/google_ads_client.rb, line 132 def target default_target = "googleads.googleapis.com:443" target = @config.api_endpoint || default_target end
Returns a reference to the WrapperUtil
class for ease of access.
# File lib/google/ads/google_ads/google_ads_client.rb, line 178 def wrapper() Google::Ads::GoogleAds::WrapperUtil end
Private Instance Methods
Create the default logger, useful if the user hasn't defined one.
# File lib/google/ads/google_ads/google_ads_client.rb, line 241 def create_default_logger() if @config.logger.nil? logger = Logger.new(@config.log_target) logger.level = Logger.const_get(@config.log_level) logger else @config.logger end end
# File lib/google/ads/google_ads/google_ads_client.rb, line 255 def default_api_version Google::Ads::GoogleAds.default_api_version end
# File lib/google/ads/google_ads/google_ads_client.rb, line 259 def deprecate(deprecation) deprecator.deprecate(deprecation) end
# File lib/google/ads/google_ads/google_ads_client.rb, line 263 def deprecator Google::Ads::GoogleAds::Deprecation.new( @config.treat_deprecation_warnings_as_errors, @config.warn_on_all_deprecations, ) end
# File lib/google/ads/google_ads/google_ads_client.rb, line 202 def get_credentials if @config.authentication @config.authentication elsif @config.keyfile get_service_account_credentials else get_updater_proc end end
Provides a Google::Auth::Credentials initialized with a keyfile specified in the config.
# File lib/google/ads/google_ads/google_ads_client.rb, line 226 def get_service_account_credentials raise 'config.impersonate required if keyfile specified' unless @config.impersonate keyfile = File.read(@config.keyfile) keyfile = JSON.parse(keyfile) Signet::OAuth2::Client.new( token_credential_uri: "https://accounts.google.com/o/oauth2/token", audience: "https://accounts.google.com/o/oauth2/token", issuer: keyfile.fetch("client_email"), signing_key: OpenSSL::PKey::RSA.new(keyfile.fetch("private_key")), person: @config.impersonate, scope: [SCOPE], ).updater_proc end
Provides the service a method by which to obtain an access token to authenticate API requests.
# File lib/google/ads/google_ads/google_ads_client.rb, line 214 def get_updater_proc Signet::OAuth2::Client.new( token_credential_uri: "https://www.googleapis.com/oauth2/v3/token", client_id: @config.client_id, client_secret: @config.client_secret, refresh_token: @config.refresh_token, scope: [SCOPE] ).updater_proc end
# File lib/google/ads/google_ads/google_ads_client.rb, line 251 def lookup_util @lookup_util ||= Google::Ads::GoogleAds::LookupUtil.new end