Class: Google::Ads::GoogleAds::GoogleAdsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/google/ads/google_ads/google_ads_client.rb

Constant Summary collapse

DEFAULT_CONFIG_FILENAME =
"google_ads_config.rb".freeze
SCOPE =
"https://www.googleapis.com/auth/adwords".freeze
MAX_MESSAGE_LENGTH =
"grpc.max_receive_message_length".freeze
MAX_METADATA_SIZE =
"grpc.max_metadata_size".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path = nil, &block) ⇒ GoogleAdsClient

Returns a new instance of GoogleAdsClient.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 56

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

Instance Attribute Details

#logger ⇒ Object

Returns the value of attribute logger.



52
53
54
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 52

def logger
  @logger
end

#lookup_util=(value) ⇒ Object

Allow setting the lookup_util manually for users who use it before creating the client.



54
55
56
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 54

def lookup_util=(value)
  @lookup_util = value
end

Instance Method Details

#configure {|@config| ... } ⇒ Object

Yields:

  • (@config)


93
94
95
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 93

def configure(&block)
  yield @config
end

#decode_partial_failure_error(pfe) ⇒ Object

Decode a partial failure error from a response. See Google::Ads::GoogleAds::PartialFailureErrorDecoder for full documentation.



194
195
196
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 194

def decode_partial_failure_error(pfe)
  StatusDecoder.decode(pfe)
end

#decode_warning(warning) ⇒ Object

Identical to decoding a partial failure error as above, but duplicated so the client code is easier to follow.



200
201
202
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 200

def decode_warning(warning)
  StatusDecoder.decode(warning)
end

#endpoint ⇒ Object



131
132
133
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 131

def endpoint
  target.split(":443").first
end

#enum ⇒ Object



171
172
173
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 171

def enum
  Factories.version_alternate_for(:enums)
end

#field_mask ⇒ Object

Returns a reference to the FieldMaskUtil class for ease of access.



176
177
178
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 176

def field_mask()
  Google::Ads::GoogleAds::FieldMaskUtil
end

#load_environment_config ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 97

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.use_application_default_credentials = ENV.fetch("GOOGLE_ADS_USE_APPLICATION_DEFAULT_CREDENTIALS", @config.use_application_default_credentials)
  @config.developer_token = ENV.fetch("GOOGLE_ADS_DEVELOPER_TOKEN", @config.developer_token)
  @config. = ENV.fetch("GOOGLE_ADS_LOGIN_CUSTOMER_ID", @config.)
  @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)
  @config.ads_assistant = ENV.fetch("GOOGLE_ADS_ASSISTANT", @config.ads_assistant)
end

#make_channel ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 140

def make_channel
  channel_args = {
    MAX_MESSAGE_LENGTH => 64*1024*1024,
     => 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

#operation ⇒ Object



167
168
169
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 167

def operation
  Factories.version_alternate_for(:operations)
end

#patch_lro_headers(class_to_return, headers) ⇒ Object



155
156
157
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 155

def patch_lro_headers(class_to_return, headers)
  PatchLROHeaders.new(class_to_return, headers).call
end

#path(version = default_api_version) ⇒ Object

Returns a reference to the PathLookupUtil to generate resource names.



181
182
183
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 181

def path(version = default_api_version)
  lookup_util.path(version)
end

#resource ⇒ Object

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.



163
164
165
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 163

def resource
  Factories.version_alternate_for(:resources)
end

#service ⇒ Object

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.



120
121
122
123
124
125
126
127
128
129
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 120

def service
  ServiceLookup.new(
    lookup_util,
    @logger,
    @config,
    make_channel,
    endpoint,
    deprecator,
  ).call
end

#target ⇒ Object



135
136
137
138
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 135

def target
  default_target = "googleads.googleapis.com:443"
  target = @config.api_endpoint || default_target
end