Sha256: 76c5dfeb1eafb9fa3e9b04d3e34f482cedb88ff01f0ba1a59bd6944be7b4149d
Contents?: true
Size: 1.23 KB
Versions: 2
Compression:
Stored size: 1.23 KB
Contents
# frozen_string_literal: true require 'creditsafe/authenticate' require 'creditsafe/version' require 'creditsafe/errors' module Creditsafe class FaradayAuth < Faraday::Middleware def initialize(app, username, password, options = {}) super(app) @username = username @password = password @token = nil @options = options end def call(env) retry_count = 0 request_body = env[:body] authenticate! unless @token begin env[:request_headers]['Authorization'] = @token env[:request_headers]['Content-Type'] = 'application/json' env[:request_headers]['User-Agent'] = "finpoint/#{Creditsafe::VERSION}" # after failure env[:body] is set to the response body env[:body] = request_body @app.call(env).tap do |resp| if resp.status == 401 raise Creditsafe::UnauthorizedError, resp.body end end rescue Creditsafe::UnauthorizedError => e retry_count += 1 if retry_count <= 1 authenticate! retry else raise e end end end private def authenticate! @token = Authenticate.new.call(@username, @password) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
creditsafe_connect-0.1.2 | lib/creditsafe/faraday_auth.rb |
creditsafe_connect-0.1.1 | lib/creditsafe/faraday_auth.rb |