<commit_before># encoding: utf-8

module FIR
  module Http

    def get url, params = {}, timeout = 300
      begin
        res = ::RestClient::Request.execute(
          method:  :get,
          url:     url,
          timeout: timeout,
          headers: default_headers.merge(params: params)
        )
      rescue => e
        logger.error "#{e.class}\n#{e.message}"
        exit 1
      end

      JSON.parse(res.body.force_encoding("UTF-8"), symbolize_names: true)
    end

    def post url, query, timeout = 300
      begin
        res = ::RestClient::Request.execute(
          method:  :post,
          url:     url,
          payload: query,
          timeout: timeout,
          headers: default_headers
        )
      rescue => e
        logger.error "#{e.class}\n#{e.message}"
        exit 1
      end

      JSON.parse(res.body.force_encoding("UTF-8"), symbolize_names: true)
    end

    def patch url, query, timeout = 300
      begin
        res = ::RestClient::Request.execute(
          method:  :patch,
          url:     url,
          payload: query,
          timeout: timeout,
          headers: default_headers
        )
      rescue => e
        logger.error "#{e.class}\n#{e.message}"
        exit 1
      end

      JSON.parse(res.body.force_encoding("UTF-8"), symbolize_names: true)
    end

    def put url, query, timeout = 300
      begin
        res = ::RestClient::Request.execute(
          method:  :put,
          url:     url,
          payload: query,
          timeout: timeout,
          headers: default_headers
        )
      rescue => e
        logger.error "#{e.class}\n#{e.message}"
        exit 1
      end

      JSON.parse(res.body.force_encoding("UTF-8"), symbolize_names: true)
    end

    private

      def default_headers
        { content_type: :json, source: 'fir-cli', cli_version: FIR::VERSION }
      end
  end
end<commit_msg>remove similar code<commit_after># encoding: utf-8

module FIR
  module Http

    DEFAULT_TIMEOUT = 300

    def get url, params = {}
      begin
        res = ::RestClient::Request.execute(
          method:  :get,
          url:     url,
          timeout: DEFAULT_TIMEOUT,
          headers: default_headers.merge(params: params)
        )
      rescue => e
        logger.error e.class.to_s + "\n" + e.message.to_s
        exit 1
      end

      JSON.parse(res.body.force_encoding("UTF-8"), symbolize_names: true)
    end

    %w(post patch put).each do |method|
      define_method method do |url, query|
        begin
          res = ::RestClient::Request.execute(
            method:  method.to_sym,
            url:     url,
            payload: query,
            timeout: DEFAULT_TIMEOUT,
            headers: default_headers
          )
        rescue => e
          logger.error e.class.to_s + "\n" + e.message.to_s
          exit 1
        end

        JSON.parse(res.body.force_encoding("UTF-8"), symbolize_names: true)
      end
    end

    private

      def default_headers
        { content_type: :json, source: 'fir-cli', cli_version: FIR::VERSION }
      end
  end
end
{'fraction_non_alphanumeric': 0.10753736192332683, 'fraction_numerical': 0.009421702404158544, 'mean_word_length': 1.7056239015817223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5407678', 'n_tokens_mistral': 944, 'n_tokens_neox': 914, 'n_words': 226}