<commit_before>require 'faraday'
require 'reactio/api_endpoint'
require 'reactio/faraday_engine'

module Reactio
  class APIClient

    def self.build(api_key, organization)
      raise ArgumentError, 'api_key is required' unless api_key
      raise ArgumentError, 'organization is required' unless organization
      new(api_key, APIEndpoint.new(organization))
    end

    def initialize(api_key, api_endpoint)
      @http = build_http_client(api_key, api_endpoint)
    end

    def request(method, path, env = {})
      @http
        .send(method, path) {|r| r.body = env[:body] if env.key?(:body) }
        .body
    end

    private

      def build_http_client(api_key, api_endpoint)
        Faraday.new(url: api_endpoint.base_url) do |faraday|
          faraday.use :reactio, api_key
          faraday.request :url_encoded
          faraday.adapter Faraday.default_adapter
        end
      end
  end
end<commit_msg>build http client in factory method<commit_after>require 'faraday'
require 'reactio/api_endpoint'
require 'reactio/faraday_engine'

module Reactio
  class APIClient

    class << self

      def build(api_key, organization)
        raise ArgumentError, 'api_key is required' unless api_key
        raise ArgumentError, 'organization is required' unless organization
        http_client = build_http_client(
          api_key,
          APIEndpoint.new(organization)
        )
        new(http_client)
      end

      def build_http_client(api_key, api_endpoint)
        Faraday.new(url: api_endpoint.base_url) do |faraday|
          faraday.use :reactio, api_key
          faraday.request :url_encoded
          faraday.adapter Faraday.default_adapter
        end
      end
    end

    def initialize(http_client)
      @http = http_client
    end

    def request(method, path, env = {})
      @http
        .send(method, path) {|r| r.body = env[:body] if env.key?(:body) }
        .body
    end
  end
end
{'fraction_non_alphanumeric': 0.08182288969445883, 'fraction_numerical': 0.0, 'mean_word_length': 2.708253358925144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '484246', 'n_tokens_mistral': 609, 'n_tokens_neox': 584, 'n_words': 147}