Class: AdsCommon::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/ads_common/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provided_config = nil) ⇒ Api

Constructor for API.



44
45
46
47
48
# File 'lib/ads_common/api.rb', line 44

def initialize(provided_config = nil)
  @wrappers = {}
  load_config(provided_config)
  check_version(@logger)
end

Instance Attribute Details

#configObject (readonly)

Methods that return the client library configuration. Needs to be redefined in subclasses.



35
36
37
# File 'lib/ads_common/api.rb', line 35

def config
  @config
end

#credential_handlerObject (readonly)

Credential handler objects used for authentication.



38
39
40
# File 'lib/ads_common/api.rb', line 38

def credential_handler
  @credential_handler
end

#loggerObject

The logger for this API object.



41
42
43
# File 'lib/ads_common/api.rb', line 41

def logger
  @logger
end

Instance Method Details

#api_configObject

Getter for the API service configurations.



57
58
59
# File 'lib/ads_common/api.rb', line 57

def api_config()
  return AdsCommon::ApiConfig
end

#authorize(parameters = {}, &block) ⇒ Object

Authorize with specified authentication method.

Args:

- parameters - hash of credentials to add to configuration
- block - code block to handle auth login url

Returns:

- Auth token for the method

Throws:

- AdsCommon::Errors::AuthError or derived if authetication error has
  occured


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ads_common/api.rb', line 100

def authorize(parameters = {}, &block)
  parameters.each_pair do |key, value|
    @credential_handler.set_credential(key, value)
  end

  auth_handler = get_auth_handler()
  token = auth_handler.get_token()

  # If token is invalid ask for a new one.
  if token.nil?
    begin
      credentials = @credential_handler.credentials
      token = auth_handler.get_token(credentials)
    rescue AdsCommon::Errors::OAuth2VerificationRequired => e
      verification_code = (block_given?) ? yield(e.oauth_url) : nil
      # Retry with verification code if one provided.
      if verification_code
        @credential_handler.set_credential(
            :oauth2_verification_code, verification_code)
        retry
      else
        raise e
      end
    end
  end
  return token
end

#get_auth_handlerObject

Auxiliary method to get an authentication handler. Creates a new one if the handler has not been initialized yet.

Returns:

  • auth handler



134
135
136
137
138
139
140
# File 'lib/ads_common/api.rb', line 134

def get_auth_handler()
  if @auth_handler.nil?
    @auth_handler = create_auth_handler()
    @credential_handler.set_auth_handler(@auth_handler)
  end
  return @auth_handler
end

#save_oauth2_token(token) ⇒ Object

Updates default configuration file to include OAuth2 token information.



143
144
145
146
147
# File 'lib/ads_common/api.rb', line 143

def save_oauth2_token(token)
  raise AdsCommon::Errors::Error, "Can't save nil token" if token.nil?
  AdsCommon::Utils.save_oauth2_token(
      File.join(ENV['HOME'], api_config.default_config_filename), token)
end

#service(name, version = nil) ⇒ Object

Obtain an API service, given a version and its name.

Args:

  • name: name for the intended service

  • version: intended API version.

Returns:

  • the service wrapper for the intended service.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ads_common/api.rb', line 70

def service(name, version = nil)
  name = name.to_sym
  version = (version.nil?) ? api_config.default_version : version.to_sym

  # Check if the combination is available.
  validate_service_request(version, name)

  # Try to re-use the service for this version if it was requested before.
  wrapper = if @wrappers.include?(version) && @wrappers[version][name]
    @wrappers[version][name]
  else
    @wrappers[version] ||= {}
    @wrappers[version][name] = prepare_wrapper(version, name)
  end
  return wrapper
end