def authenticate
      $logger.debug { 'ApiClient#authenticate' }
      StorageFile.open(token_file) do |fp|
        token = fp.load
        if should_read_token_from_file?(token)
          $logger.debug { "Read token from file" }
          update_current_token(token)
          return
        end
        $logger.debug { "AuthApi#create_token" }
        auth_api = TriglavClient::AuthApi.new(@api_client)
        credential = TriglavClient::Credential.new(
          username: username, password: password, authenticator: authenticator
        )
        handle_auth_error do
          $logger.debug { 'TriglavClient::AuthApi' }
          result = auth_api.create_token(credential)
          token = {access_token: result.access_token}
          update_current_token(token)
          fp.dump(token)
        end
      end
    end
def authenticate
      $logger.debug { 'ApiClient#authenticate' }
      StorageFile.open(token_file) do |fp|
        token = fp.load
        if should_read_token_from_file?(token)
          $logger.debug { "Read token from file" }
          update_current_token(token)
          return
        end
        $logger.debug { "AuthApi#create_token" }
        auth_api = TriglavClient::AuthApi.new(@api_client)
        credential = TriglavClient::Credential.new(
          username: username, password: password, authenticator: authenticator
        )
        handle_auth_error do
          $logger.debug { 'TriglavClient::AuthApi' }
          result = auth_api.create_token(credential)
          token = {access_token: result.access_token}
          update_current_token(token)
          fp.dump(token)
        end
      end
    end
Authenticate

 1. Another process saved a newer token onto the token_file => read it
 2. The token saved on the token_file is same with current token => re-authenticate
 3. The token saved on the token_file is older than the current token
   => unknown situation, re-authenticate and save into token_file to refresh anyway
 4. No token is saved on the token_file => authenticate