<commit_before>require "logging"
require "rest-client"
require "gds_api/govuk_headers"

module LegacyClient
  class Client
    # Sub-paths almost certainly shouldn't start with leading slashes,
    # since this will make the request relative to the server root
    SAFE_ABSOLUTE_PATHS = ["/_bulk", "/_status", "/_aliases", "/_search/scroll"].freeze

    def initialize(base_uri, args = {})
      @base_uri = base_uri
      @error_log_level = :error
      @timeout = args[:timeout]
      @open_timeout = args[:open_timeout]
    end

    def get(path, headers = {})
      request(:get, path, nil, headers)
    end

    def post(path, payload, headers = {})
      request(:post, path, payload, headers)
    end

    def patch(path, payload, headers = {})
      request(:patch, path, payload, headers)
    end

    def put(path, payload, headers = {})
      request(:put, path, payload, headers)
    end

    def delete(path, headers = {})
      request(:delete, path, nil, headers)
    end

    def head(path, headers = {})
      request(:head, path, nil, headers)
    end

    def options(path, headers = {})
      request(:options, path, nil, headers)
    end

    # RestClient doesn't natively support sending payloads with these request
    # methods, but elasticsearch requires them for certain operations
    def get_with_payload(path, payload, headers = {})
      request(:get, path, payload, headers)
    end

    def delete_with_payload(path, payload, headers = {})
      request(:delete, path, payload, headers)
    end

  private

    def logger
      Logging.logger[self]
    end

    def url_for(sub_path)
      if sub_path.is_a? URI
        sub_path = sub_path.to_s
      end
      if sub_path.start_with? "/"
        path_without_query = sub_path.split("?")[0]
        unless SAFE_ABSOLUTE_PATHS.include? path_without_query
          logger.error "Request sub-path '#{sub_path}' has a leading slash"
          raise ArgumentError, "Only whitelisted absolute paths are allowed"
        end
      end

      # Addition on URLs does relative resolution
      (@base_uri + sub_path).to_s
    end

    def logging_exception_body(&_block)
      yield
    rescue RestClient::BadRequest => error
      logger.send(
        @error_log_level,
        "BadRequest error from elasticsearch. " +
        "Response: #{error.http_body}"
      )
      raise
    rescue RestClient::InternalServerError => error
      logger.send(
        @error_log_level,
        "Internal server error in elasticsearch. " +
        "Response: #{error.http_body}"
      )
      raise
    end

    def request(method, path, payload = nil, headers = {})
      if headers == {}
        headers[:content_type] = "application/json"
      end

      headers = headers.merge(GdsApi::GovukHeaders.headers)

      logging_exception_body do
        args = {
          method: method,
          url: url_for(path),
        }
        args[:payload] = payload if payload
        args[:headers] = headers if headers
        args[:timeout] = @timeout if @timeout
        args[:open_timeout] = @open_timeout if @open_timeout
        logger.debug(args.reject { |k| k == :payload })
        RestClient::Request.execute(args)
      end
    end
  end
end<commit_msg>Remove unnecessary conditional<commit_after>require "logging"
require "rest-client"
require "gds_api/govuk_headers"

module LegacyClient
  class Client
    # Sub-paths almost certainly shouldn't start with leading slashes,
    # since this will make the request relative to the server root
    SAFE_ABSOLUTE_PATHS = ["/_bulk", "/_status", "/_aliases", "/_search/scroll"].freeze

    def initialize(base_uri, args = {})
      @base_uri = base_uri
      @error_log_level = :error
      @timeout = args[:timeout]
      @open_timeout = args[:open_timeout]
    end

    def get(path, headers = {})
      request(:get, path, nil, headers)
    end

    def post(path, payload, headers = {})
      request(:post, path, payload, headers)
    end

    def patch(path, payload, headers = {})
      request(:patch, path, payload, headers)
    end

    def put(path, payload, headers = {})
      request(:put, path, payload, headers)
    end

    def delete(path, headers = {})
      request(:delete, path, nil, headers)
    end

    def head(path, headers = {})
      request(:head, path, nil, headers)
    end

    def options(path, headers = {})
      request(:options, path, nil, headers)
    end

    # RestClient doesn't natively support sending payloads with these request
    # methods, but elasticsearch requires them for certain operations
    def get_with_payload(path, payload, headers = {})
      request(:get, path, payload, headers)
    end

    def delete_with_payload(path, payload, headers = {})
      request(:delete, path, payload, headers)
    end

  private

    def logger
      Logging.logger[self]
    end

    def url_for(sub_path)
      if sub_path.is_a? URI
        sub_path = sub_path.to_s
      end
      if sub_path.start_with? "/"
        path_without_query = sub_path.split("?")[0]
        unless SAFE_ABSOLUTE_PATHS.include? path_without_query
          logger.error "Request sub-path '#{sub_path}' has a leading slash"
          raise ArgumentError, "Only whitelisted absolute paths are allowed"
        end
      end

      # Addition on URLs does relative resolution
      (@base_uri + sub_path).to_s
    end

    def logging_exception_body(&_block)
      yield
    rescue RestClient::BadRequest => error
      logger.send(
        @error_log_level,
        "BadRequest error from elasticsearch. " +
        "Response: #{error.http_body}"
      )
      raise
    rescue RestClient::InternalServerError => error
      logger.send(
        @error_log_level,
        "Internal server error in elasticsearch. " +
        "Response: #{error.http_body}"
      )
      raise
    end

    def request(method, path, payload = nil, headers = {})
      if headers == {}
        headers[:content_type] = "application/json"
      end

      headers = headers.merge(GdsApi::GovukHeaders.headers)

      logging_exception_body do
        args = {
          method: method,
          url: url_for(path),
        }
        args[:payload] = payload if payload
        args[:headers] = headers
        args[:timeout] = @timeout if @timeout
        args[:open_timeout] = @open_timeout if @open_timeout
        logger.debug(args.reject { |k| k == :payload })
        RestClient::Request.execute(args)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10285006195786865, 'fraction_numerical': 0.0003097893432465923, 'mean_word_length': 2.5892162312395777, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3072781', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1872, 'n_words': 521}