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.
44 45 46 47 |
# File 'lib/ads_common/api.rb', line 44 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.
35 36 37 |
# File 'lib/ads_common/api.rb', line 35 def config @config end |
#credential_handler ⇒ Object (readonly)
Credential handler objects used for authentication.
38 39 40 |
# File 'lib/ads_common/api.rb', line 38 def credential_handler @credential_handler end |
#logger ⇒ Object
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_config ⇒ Object
Getter for the API service configurations.
56 57 58 |
# File 'lib/ads_common/api.rb', line 56 def api_config() return AdsCommon::ApiConfig end |
#authorize(parameters = {}, &block) ⇒ Object
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 (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
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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ads_common/api.rb', line 69 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 |