<commit_before>require 'rest_client'
require 'oauth'
require 'json'
require 'thread'

module Runcible
  class Base
    def initialize(config = {})
      @mutex = Mutex.new
      @config = config
    end

    def lazy_config=(a_block)
      @mutex.synchronize { @lazy_config = a_block }
    end

    def config
      @mutex.synchronize do
        @config = @lazy_config.call if defined?(@lazy_config)
        fail Runcible::ConfigurationUndefinedError, Runcible::ConfigurationUndefinedError.message unless @config
        @config
      end
    end

    def path(*args)
      self.class.path(*args)
    end

    def call(method, path, options = {})
      clone_config = self.config.clone
      #on occation path will already have prefix (sync cancel)
      path = clone_config[:api_path] + path unless path.start_with?(clone_config[:api_path])

      RestClient.log = []
      headers = clone_config[:headers].clone

      get_params = options[:params] if options[:params]
      path = combine_get_params(path, get_params) if get_params

      client_options = {}
      client_options[:timeout] = clone_config[:timeout] if clone_config[:timeout]
      client_options[:open_timeout] = clone_config[:open_timeout] if clone_config[:open_timeout]
      client_options[:verify_ssl] = clone_config[:verify_ssl] unless clone_config[:verify_ssl].nil?

      if clone_config[:oauth]
        headers = add_oauth_header(method, path, headers)
        headers['pulp-user'] = clone_config[:user]
      elsif clone_config[:cert_auth]
        if !clone_config[:cert_auth][:ssl_client_cert] || !clone_config[:cert_auth][:ssl_client_key]
          fail Runcible::ConfigurationUndefinedError, "Missing SSL certificate or key configuration."
        end
        client_options[:ssl_client_cert] = clone_config[:cert_auth][:ssl_client_cert]
        client_options[:ssl_client_key] = clone_config[:cert_auth][:ssl_client_key]
      else
        client_options[:user] = clone_config[:user]
        client_options[:password] = config[:http_auth][:password]
      end

      client_options[:ca_file] = config[:ca_cert_file] unless config[:ca_cert_file].nil?
      client = RestClient::Resource.new(clone_config[:url], client_options)

      args = [method]
      args << generate_payload(options) if [:post, :put].include?(method)
      args << headers

      response = get_response(client, path, *args)
      process_response(response)

    rescue RestClient::ResourceNotFound => e
      log_info
      raise e
    rescue => e
      log_exception
      raise e
    end

    def get_response(client, path, *args)
      client[path].send(*args) do |response, request, result, &_block|
        resp = response.return!(request, result)
        log_debug
        return resp
      end
    end
<commit_msg>Fixes #17404: Configure CA cert properly<commit_after>require 'rest_client'
require 'oauth'
require 'json'
require 'thread'

module Runcible
  class Base
    def initialize(config = {})
      @mutex = Mutex.new
      @config = config
    end

    def lazy_config=(a_block)
      @mutex.synchronize { @lazy_config = a_block }
    end

    def config
      @mutex.synchronize do
        @config = @lazy_config.call if defined?(@lazy_config)
        fail Runcible::ConfigurationUndefinedError, Runcible::ConfigurationUndefinedError.message unless @config
        @config
      end
    end

    def path(*args)
      self.class.path(*args)
    end

    def call(method, path, options = {})
      clone_config = self.config.clone
      #on occation path will already have prefix (sync cancel)
      path = clone_config[:api_path] + path unless path.start_with?(clone_config[:api_path])

      RestClient.log = []
      headers = clone_config[:headers].clone

      get_params = options[:params] if options[:params]
      path = combine_get_params(path, get_params) if get_params

      client_options = {}
      client_options[:timeout] = clone_config[:timeout] if clone_config[:timeout]
      client_options[:open_timeout] = clone_config[:open_timeout] if clone_config[:open_timeout]
      client_options[:verify_ssl] = clone_config[:verify_ssl] unless clone_config[:verify_ssl].nil?

      if clone_config[:oauth]
        headers = add_oauth_header(method, path, headers)
        headers['pulp-user'] = clone_config[:user]
      elsif clone_config[:cert_auth]
        if !clone_config[:cert_auth][:ssl_client_cert] || !clone_config[:cert_auth][:ssl_client_key]
          fail Runcible::ConfigurationUndefinedError, "Missing SSL certificate or key configuration."
        end
        client_options[:ssl_client_cert] = clone_config[:cert_auth][:ssl_client_cert]
        client_options[:ssl_client_key] = clone_config[:cert_auth][:ssl_client_key]
      else
        client_options[:user] = clone_config[:user]
        client_options[:password] = config[:http_auth][:password]
      end

      client_options[:ssl_ca_file] = config[:ca_cert_file] unless config[:ca_cert_file].nil?
      client = RestClient::Resource.new(clone_config[:url], client_options)

      args = [method]
      args << generate_payload(options) if [:post, :put].include?(method)
      args << headers

      response = get_response(client, path, *args)
      process_response(response)

    rescue RestClient::ResourceNotFound => e
      log_info
      raise e
    rescue => e
      log_exception
      raise e
    end

    def get_response(client, path, *args)
      client[path].send(*args) do |response, request, result, &_block|
        resp = response.return!(request, result)
        log_debug
        return resp
      end
    end

{'fraction_non_alphanumeric': 0.10496149023822318, 'fraction_numerical': 0.0008955758552749418, 'mean_word_length': 3.410742496050553, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '4078978', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1650, 'n_words': 351}