Class: AdsCommon::Api
- Inherits:
-
Object
- Object
- AdsCommon::Api
- Defined in:
- lib/ads_common/api.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Methods that return the client library configuration.
-
#credential_handler ⇒ Object
readonly
Credential handler objects used for authentication.
-
#logger ⇒ Object
The logger for this API object.
Instance Method Summary collapse
-
#api_config ⇒ Object
Getter for the API service configurations.
-
#authorize(parameters = {}, &block) ⇒ Object
Authorize with specified authentication method.
-
#get_auth_handler ⇒ Object
Auxiliary method to get an authentication handler.
-
#initialize(provided_config = nil) ⇒ Api
constructor
Constructor for API.
-
#save_oauth2_token(token) ⇒ Object
Updates default configuration file to include OAuth2 token information.
-
#service(name, version = nil) ⇒ Object
Obtain an API service, given a version and its name.
Constructor Details
#initialize(provided_config = nil) ⇒ Api
Constructor for API.
42 43 44 45 |
# File 'lib/ads_common/api.rb', line 42 def initialize(provided_config = nil) @wrappers = {} load_config(provided_config) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Methods that return the client library configuration. Needs to be redefined in subclasses.
33 34 35 |
# File 'lib/ads_common/api.rb', line 33 def config @config end |
#credential_handler ⇒ Object (readonly)
Credential handler objects used for authentication.
36 37 38 |
# File 'lib/ads_common/api.rb', line 36 def credential_handler @credential_handler end |
#logger ⇒ Object
The logger for this API object.
39 40 41 |
# File 'lib/ads_common/api.rb', line 39 def logger @logger end |
Instance Method Details
#api_config ⇒ Object
Getter for the API service configurations.
54 55 56 |
# File 'lib/ads_common/api.rb', line 54 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
98 99 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 |
# File 'lib/ads_common/api.rb', line 98 def (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_handler ⇒ Object
Auxiliary method to get an authentication handler. Creates a new one if the handler has not been initialized yet.
Returns:
-
auth handler
132 133 134 135 136 137 138 |
# File 'lib/ads_common/api.rb', line 132 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.
141 142 143 144 145 |
# File 'lib/ads_common/api.rb', line 141 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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ads_common/api.rb', line 67 def service(name, version = nil) name = name.to_sym version = (version.nil?) ? api_config.default_version : version.to_sym environment = @config.read('service.environment') # Check if the combination is available. validate_service_request(environment, 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 |