Module: SBF::APIConnector
- Defined in:
- lib/stbaldricks/api_connector.rb
Defined Under Namespace
Classes: Configuration
Instance Method Summary collapse
-
#with_auto_connect ⇒ Object
Executes API request with attempts to auto login the configured user on the API.
Instance Method Details
#with_auto_connect ⇒ Object
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? login_response = SBF::Client::User.login!(SBF::APIConnector::Configuration.username, SBF::APIConnector::Configuration.password) error login_response.http_code, login_response.error.to_json if login_response.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.' } login_response = SBF::Client::User.login!(SBF::APIConnector::Configuration.username, SBF::APIConnector::Configuration.password) error login_response.http_code, login_response.error.to_json if login_response.error? # Try request again response = yield end response end |