# frozen_string_literal: true require 'creditsafe/client' require 'creditsafe/errors' require 'creditsafe/version' module Creditsafe class Authenticate def call(username, password) response = post('authenticate', username:, password:) if response.success? response.body[:token] elsif response.status == 401 raise UnauthorizedError, response.body else raise GenericError, response.body end end private def post(path, params = {}) connection.post(path, MultiJson.dump(params)) end def connection api_url = Client.new.api_url @connection ||= Faraday.new(url: api_url) do |conn| conn.headers.merge!( content_type: 'application/json', user_agent: "finpoint/#{Creditsafe::VERSION}" ) conn.response :json, parser_options: { symbolize_names: true } conn.response :logger if Creditsafe.configuration.debug conn.adapter Faraday.default_adapter end end end end