<commit_before># frozen_string_literal: true

require_relative './configuration.rb'
require_relative './connection.rb'
require_relative '../utils/loggeable'
require_relative '../utils/validatable'

using ObjectExtensions
using StringExtensions

module FinAppsCore
  module REST
    # base client functionality
    class BaseClient
      include ::FinAppsCore::Utils::Loggeable
      include ::FinAppsCore::Utils::Validatable
      include ::FinAppsCore::REST::Connection

      attr_reader :config

      def initialize(options, logger = nil)
        @config = ::FinAppsCore::REST::Configuration.new options
        @logger = logger
      end

      # Returns an initialized Faraday connection object.
      #
      # @return Faraday::Connection.
      def connection
        @connection ||= faraday(config, logger)
      end

      # Performs HTTP GET, POST, UPDATE and DELETE requests.
      # You shouldn't need to use this method directly, but it can be useful for debugging.
      # Returns a hash obtained from parsing the JSON object in the response body.
      #
      # @param [String] path
      # @param [String] method
      # @param [Hash] params
      # @return [Hash,Array<String>]
      def send_request(path, method, params = {})
        not_blank(path, :path)
        not_blank(method, :method)

        response, error_messages = execute_request(path, method, params)
        result = if empty?(response)
                   nil
                 else
                   block_given? ? yield(response) : response.body
                 end

        [result, error_messages]
      end

      # Defines methods to perform HTTP GET, POST, PUT and DELETE requests.
      # Returns a hash obtained from parsing the JSON object in the response body.
      #
      def method_missing(method_id, *arguments, &block)
        if %i[get post put delete].include? method_id
          send_to_connection method_id, arguments
        else
          super
        end
      end

      def respond_to_missing?(method_sym, include_private = false)
        %i[get post put delete].include?(method_sym) ? true : super
      end

      private

      def send_to_connection(method_id, arguments)
        connection.send(method_id) do |req|
          req.url arguments.first
          req.body = arguments[1] unless method_id == :get
        end
      end

      def empty?(response)
        !response || empty_body?(response)
      end

      def empty_body?(response)
        !response.respond_to?(:body) || !response.body || (response.body.respond_to?(:empty?) && response.body.empty?)
      end

      def execute_request(path, method, params)
        errors = []

        begin
          response = execute_method path, method, params
        rescue FinAppsCore::InvalidArgumentsError => e
          handle_error e
        rescue FinAppsCore::MissingArgumentsError => e
          handle_error e
        rescue Faraday::Error::ConnectionFailed => e
          handle_error e
        rescue Faraday::Error::ClientError => e
          errors = handle_client_error(e)
        end
<commit_msg>Get empty responses as they are sent<commit_after># frozen_string_literal: true

require_relative './configuration.rb'
require_relative './connection.rb'
require_relative '../utils/loggeable'
require_relative '../utils/validatable'

using ObjectExtensions
using StringExtensions

module FinAppsCore
  module REST
    # base client functionality
    class BaseClient
      include ::FinAppsCore::Utils::Loggeable
      include ::FinAppsCore::Utils::Validatable
      include ::FinAppsCore::REST::Connection

      attr_reader :config

      def initialize(options, logger = nil)
        @config = ::FinAppsCore::REST::Configuration.new options
        @logger = logger
      end

      # Returns an initialized Faraday connection object.
      #
      # @return Faraday::Connection.
      def connection
        @connection ||= faraday(config, logger)
      end

      # Performs HTTP GET, POST, UPDATE and DELETE requests.
      # You shouldn't need to use this method directly, but it can be useful for debugging.
      # Returns a hash obtained from parsing the JSON object in the response body.
      #
      # @param [String] path
      # @param [String] method
      # @param [Hash] params
      # @return [Hash,Array<String>]
      def send_request(path, method, params = {})
        not_blank(path, :path)
        not_blank(method, :method)

        response, error_messages = execute_request(path, method, params)
        result = block_given? ? yield(response) : response_body(response)

        [result, error_messages]
      end

      # Defines methods to perform HTTP GET, POST, PUT and DELETE requests.
      # Returns a hash obtained from parsing the JSON object in the response body.
      #
      def method_missing(method_id, *arguments, &block)
        if %i[get post put delete].include? method_id
          send_to_connection method_id, arguments
        else
          super
        end
      end

      def respond_to_missing?(method_sym, include_private = false)
        %i[get post put delete].include?(method_sym) ? true : super
      end

      private

      def response_body(response)
        response.body if response.respond_to?(:body)
      end

      def send_to_connection(method_id, arguments)
        connection.send(method_id) do |req|
          req.url arguments.first
          req.body = arguments[1] unless method_id == :get
        end
      end

      def execute_request(path, method, params)
        response = nil
        errors = []

        begin
          response = execute_method path, method, params
        rescue FinAppsCore::InvalidArgumentsError,
               FinAppsCore::MissingArgumentsError,
               Faraday::Error::ConnectionFailed => e
          handle_error e
        rescue Faraday::Error::ClientError => e
          errors = handle_client_error(e)
        end

{'fraction_non_alphanumeric': 0.08291117865585951, 'fraction_numerical': 0.00033772374197906115, 'mean_word_length': 2.4820693709582597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '231799', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1551, 'n_words': 469}