Module: SBF::APIConnector

Defined in:
lib/stbaldricks/api_connector.rb

Defined Under Namespace

Classes: Configuration

Instance Method Summary collapse

Instance Method Details

#with_auto_connectObject

Executes API request with attempts to auto login the configured user on the API.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stbaldricks/api_connector.rb', line 4

def with_auto_connect
  # Check if we still need to attempt the initial login
  if SBF::Client::Configuration.user_token.nil?
     = SBF::Client::User.login!(SBF::APIConnector::Configuration.username, SBF::APIConnector::Configuration.password)
    error .http_code, .error.to_json if .error?
  end

  # Yield to the block to execute the request
  response = yield

  if response.error? && response.http_code == 401
    # If the user token is expired, try to login and refresh the token and try again
    SBF::Client::LOG.debug { 'User session has expired attempting re-login.' }
     = SBF::Client::User.login!(SBF::APIConnector::Configuration.username, SBF::APIConnector::Configuration.password)
    error .http_code, .error.to_json if .error?

    # Try request again
    response = yield
  end

  response
end