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.



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
92
93
94
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 59

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

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#lookup_util=(value) ⇒ Object

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



57
58
59
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 57

def lookup_util=(value)
  @lookup_util = value
end

Instance Method Details

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

Yields:

  • (@config)


96
97
98
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 96

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.



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

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

#endpointObject



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

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

#enumObject



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

def enum
  Factories.version_alternate_for(:enums)
end

#field_maskObject

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



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

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

#load_environment_configObject



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

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. = 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)
end

#make_channelObject



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

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

#operationObject



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

def operation
  Factories.version_alternate_for(:operations)
end

#patch_lro_headers(class_to_return, headers) ⇒ Object



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

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.



187
188
189
# File 'lib/google/ads/google_ads/google_ads_client.rb', line 187

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

#resourceObject

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.



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

def resource
  Factories.version_alternate_for(:resources)
end

#serviceObject

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.



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

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

#targetObject



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

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

#wrapperObject

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



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

def wrapper()
  Google::Ads::GoogleAds::WrapperUtil
end