lib/soaspec/o_auth2.rb in soaspec-0.1.11 vs lib/soaspec/o_auth2.rb in soaspec-0.1.12

- old
+ new

@@ -1,18 +1,29 @@ require 'erb' module Soaspec # Handles working with OAuth2 class OAuth2 + # How often to refresh access token + @refresh_token = :always + # List of access tokens. They are mapped according to the OAuth parameters used + @access_tokens = {} class << self # Default token url used across entire suite attr_accessor :token_url + # @attr [Symbol] refresh_token How often to refresh access token + # Values are: + # * :always - (Default) Request token from token url every time it is needed + # * :once - Request token once for the entire execution of the suite + attr_accessor :refresh_token + # @attr [Hash] access_tokens List of access tokens. They are mapped according to the OAuth parameters used + attr_accessor :access_tokens end - # [Hash] OAuth parameters + # @attr [Hash] OAuth parameters attr_accessor :params - # Count of tries to obtain access token + # @attr [Integer] Count of tries to obtain access token attr_accessor :retry_count # @param [Hash] params_sent Parameters to make OAuth request # @param [String] api_username Username to use which can be set by Soaspec::ExchangeHandler def initialize(params_sent, api_username = nil) @@ -26,15 +37,25 @@ params[:token_url] = ERB.new(params[:token_url]).result(binding) if params[:token_url] params[:password] = ERB.new(params[:password]).result(binding) if params[:password] Soaspec::SpecLogger.info request_message end + # @return [String] Existing or new access token, dependent on refresh_token attribute + def access_token + case Soaspec::OAuth2.refresh_token + when :once + Soaspec::OAuth2.access_tokens[params] ||= response['access_token'] + else # Default is :always + response['access_token'] + end + end + # @return [Hash] Hash containing access token parameters def response + Soaspec::SpecLogger.info "using oauth_params: #{params}" if Soaspec.debug_oauth? response = RestClient.post(params[:token_url], payload, cache_control: 'no_cache', verify_ssl: false) rescue RestClient::Exception => error Soaspec::SpecLogger.info(["oauth_error: #{error.message}", "oauth_response: #{error.response}"]) - Soaspec::SpecLogger.info "oauth_params_used: #{params}" if Soaspec.debug_oauth? self.retry_count += 1 sleep 0.1 # Wait if a bit before retying obtaining access token retry if retry_count < 3 raise error else \ No newline at end of file