<commit_before>require 'faraday'
require 'faraday_middleware'
require 'faraday_middleware/parse_oj'

Dir[File.expand_path('../resources/*.rb', __FILE__)].each { |f| require f }
require File.expand_path('../response/base.rb', __FILE__)
require File.expand_path('../recurring_frequencies.rb', __FILE__)
Dir[File.expand_path('../response/*.rb', __FILE__)].each { |f| require f }

module RockRMS
  class Client
    include RecurringFrequencies
    include RockRMS::Client::Attribute
    include RockRMS::Client::AttributeValue
    include RockRMS::Client::Batch
    include RockRMS::Client::Block
    include RockRMS::Client::BlockType
    include RockRMS::Client::Fund
    include RockRMS::Client::Campus
    include RockRMS::Client::DefinedType
    include RockRMS::Client::DefinedValue
    include RockRMS::Client::Gateway
    include RockRMS::Client::Group
    include RockRMS::Client::GroupMember
    include RockRMS::Client::History
    include RockRMS::Client::Page
    include RockRMS::Client::PaymentDetail
    include RockRMS::Client::Person
    include RockRMS::Client::PhoneNumber
    include RockRMS::Client::RecurringDonation
    include RockRMS::Client::RecurringDonationDetail
    include RockRMS::Client::Refund
    include RockRMS::Client::RefundReason
    include RockRMS::Client::SavedPaymentMethod
    include RockRMS::Client::ServiceJob
    include RockRMS::Client::Transaction
    include RockRMS::Client::TransactionDetail
    include RockRMS::Client::UserLogin
    include RockRMS::Client::Utility
    include RockRMS::Client::WorkflowActionType
    include RockRMS::Client::WorkflowActivityType
    include RockRMS::Client::WorkflowType

    attr_reader :url, :username, :password, :logger, :cookie, :connection, :adapter

    def initialize(url:, username:, password:, logger: true, adapter: Faraday.default_adapter)
      @url      = "#{url}/api/"
      @username = username
      @password = password
      @logger   = logger
      @adapter  = adapter
      @cookie   = auth['set-cookie']
    end

    def delete(path, options = {})
      connection.delete(path, options).body
    end

    def get(path, options = {})
      connection.get(path, options).body
    end

    def patch(path, options = {})
      connection.patch(path, options).body
    end

    def post(path, options = {})
      connection.post(path, options).body
    end

    def put(path, options = {})
      connection.put(path, options).body
    end

    private

    def auth
      connection.post(
        'Auth/Login',
        'Username'  => username,
        'Password'  => password,
        'Persisted' => true
      )
    end

    def connection
      headers = {<commit_msg>Handle 301 on auth/login issues<commit_after>require 'faraday'
require 'faraday_middleware'
require 'faraday_middleware/parse_oj'

Dir[File.expand_path('../resources/*.rb', __FILE__)].each { |f| require f }
require File.expand_path('../response/base.rb', __FILE__)
require File.expand_path('../recurring_frequencies.rb', __FILE__)
Dir[File.expand_path('../response/*.rb', __FILE__)].each { |f| require f }

module RockRMS
  class Client
    include RecurringFrequencies
    include RockRMS::Client::Attribute
    include RockRMS::Client::AttributeValue
    include RockRMS::Client::Batch
    include RockRMS::Client::Block
    include RockRMS::Client::BlockType
    include RockRMS::Client::Fund
    include RockRMS::Client::Campus
    include RockRMS::Client::DefinedType
    include RockRMS::Client::DefinedValue
    include RockRMS::Client::Gateway
    include RockRMS::Client::Group
    include RockRMS::Client::GroupMember
    include RockRMS::Client::History
    include RockRMS::Client::Page
    include RockRMS::Client::PaymentDetail
    include RockRMS::Client::Person
    include RockRMS::Client::PhoneNumber
    include RockRMS::Client::RecurringDonation
    include RockRMS::Client::RecurringDonationDetail
    include RockRMS::Client::Refund
    include RockRMS::Client::RefundReason
    include RockRMS::Client::SavedPaymentMethod
    include RockRMS::Client::ServiceJob
    include RockRMS::Client::Transaction
    include RockRMS::Client::TransactionDetail
    include RockRMS::Client::UserLogin
    include RockRMS::Client::Utility
    include RockRMS::Client::WorkflowActionType
    include RockRMS::Client::WorkflowActivityType
    include RockRMS::Client::WorkflowType

    attr_reader :url, :username, :password, :logger, :cookie, :connection, :adapter

    def initialize(url:, username:, password:, logger: true, adapter: Faraday.default_adapter)
      @url      = "#{url}/api/"
      @username = username
      @password = password
      @logger   = logger
      @adapter  = adapter
      @cookie   = auth['set-cookie']
    end

    def delete(path, options = {})
      connection.delete(path, options).body
    end

    def get(path, options = {})
      connection.get(path, options).body
    end

    def patch(path, options = {})
      connection.patch(path, options).body
    end

    def post(path, options = {})
      connection.post(path, options).body
    end

    def put(path, options = {})
      connection.put(path, options).body
    end

    private

    def auth
      begin
        auth_request('Auth/Login')
      rescue Faraday::Error::ParsingError => e
        if e.message.include?('Document Moved')
          auth_request('auth/login')
        else
          raise e
        end
      end
    end

    def auth_request(path)
      connection.post(
        path,
        'Username'  => username,
        'Password'  => password,
        'Persisted' => true
      )
    end

    def connection
      headers = {
{'fraction_non_alphanumeric': 0.11638237384506041, 'fraction_numerical': 0.0005330490405117271, 'mean_word_length': 3.5689935064935066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4855294', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1635, 'n_words': 345}