class AdwordsApi::CredentialHandler

Attributes

partial_failure[RW]

Whether we're making partial failure requests.

validate_only[RW]

Whether we're making validate-only requests.

Public Class Methods

new(config) click to toggle source
Calls superclass method
# File lib/adwords_api/credential_handler.rb, line 30
def initialize(config)
  super(config)
  @validate_only = false
  @partial_failure = false
end

Public Instance Methods

credentials(credentials_override = nil) click to toggle source

Create the list of credentials to be used by the auth handler for header generation.

Calls superclass method
# File lib/adwords_api/credential_handler.rb, line 38
def credentials(credentials_override = nil)
  result = super(credentials_override)
  validate_headers_for_server(result)

  extra_headers = {
    'userAgent' => generate_user_agent(),
    'developerToken' => result[:developer_token]
  }
  extra_headers['clientCustomerId'] = result[:client_customer_id] if
      result[:client_customer_id]
  extra_headers['validateOnly'] = 'true' if @validate_only
  extra_headers['partialFailure'] = 'true' if @partial_failure
  result[:extra_headers] = extra_headers
  return result
end
generate_user_agent(extra_ids = []) click to toggle source

Generates string to use as user agent in headers.

Calls superclass method
# File lib/adwords_api/credential_handler.rb, line 55
def generate_user_agent(extra_ids = [])
  agent_app = @config.read('authentication.user_agent')
  if !agent_app.nil? && !agent_app.ascii_only?
    raise AdwordsApi::Errors::InvalidUserAgentError.new(
        'User agent contains non-ASCII characters.', agent_app)
  end
  extra_ids << ['AwApi-Ruby/%s' % AdwordsApi::ApiConfig::CLIENT_LIB_VERSION]
  super(extra_ids, agent_app)
end
identifier() click to toggle source

Returns the client customer ID specified in the current credentials.

# File lib/adwords_api/credential_handler.rb, line 66
def identifier()
  return credentials[:extra_headers]['clientCustomerId']
end

Private Instance Methods

validate_headers_for_server(credentials) click to toggle source

Validates that the right credentials are being used for the chosen environment.

Raises:

  • AdwordsApi::Errors:BadCredentialsError if supplied credentials are not

valid.

# File lib/adwords_api/credential_handler.rb, line 79
def validate_headers_for_server(credentials)
  client_customer_id = credentials[:client_customer_id]

  if client_customer_id and (!(client_customer_id.is_a?(Integer) or
      (client_customer_id =~ /^\d+(-\d+-\d+)?$/)))
    raise AdwordsApi::Errors::BadCredentialsError,
        'Invalid client customer ID: %s' % client_customer_id.to_s
  end

  token = credentials[:developer_token]
  if token.nil? || token.empty?
    raise AdwordsApi::Errors::BadCredentialsError,
        'Developer token is missing, check credentials.'
  end
  return nil
end