<commit_before># typed: false

class BaseApiClient < BaseService
  extend T::Sig
  include Oauth2::Errors

  private

  # Make a GET request.
  #
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  #
  # returns  the response
  def get(path, options = {}, authorization = nil, headers = {})
    request(:get, path, options, authorization, headers)
  end

  # Make a POST request.
  #
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  #
  # returns - [Response] the response
  def post(path, options = {}, authorization = nil, headers = {})
    request(:post, path, options, authorization, headers)
  end

  # Make a PUT request.
  #
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  #
  # returns [Response] the response
  def put(path, options = {})
    request(:put, path, form: options)
  end

  # Make a PATCH request.
  #
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  #
  # returns [Response] the response
  def patch(path, options = {})
    request(:patch, path, form: options)
  end

  # Make a DELETE request.
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  # returns [Response] the response
  def delete(path, options = {})
    request(:delete, path, options)
  end

  def api_base_uri
    raise "specify me"
  end

  def perform_offline?
    false
  end

  def request(method, path, payload = {}, authorization = nil, headers = {})
    # Mock out the request
    return Struct.new(:body).new("{}") if perform_offline?

    connection.send(method) do |req|
      req.url [api_base_uri, path].join("")
      req.headers["Authorization"] = authorization || (defined?(api_authorization_header) && api_authorization_header)
      req.headers["Content-Type"] = "application/json"
      req.headers.merge!(headers)

      req.body = payload.to_json if method.to_sym.eql?(:post) || method.to_sym.eql?(:delete)
      req.params = payload if method.to_sym.eql?(:get)
    end
  end

  def connection(raise_error: true)
    @connection ||= begin
      require "faraday"
      Faraday.new(url: api_base_uri) do |faraday|
        faraday.proxy = proxy_url if proxy_url.present?
        faraday.request :retry, max: retry_count, interval: 0.05, interval_randomness: 0.5, backoff_factor: 2

        # Log level info: Brief summaries
        # Log level debug: Detailed bodies and headers
        faraday.response(:logger, Rails.logger, bodies: true, headers: true)

        if raise_error
          faraday.use(Faraday::Response::RaiseError)
        end

        faraday.adapter Faraday.default_adapter
      end
    end
  end

  ## Some convenience methods used for Sorbet typed responses in various clients<commit_msg>fix: Proxies are being phased out as most services are now off of heroku (#3838)<commit_after># typed: false

class BaseApiClient < BaseService
  extend T::Sig
  include Oauth2::Errors

  private

  # Make a GET request.
  #
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  #
  # returns  the response
  def get(path, options = {}, authorization = nil, headers = {})
    request(:get, path, options, authorization, headers)
  end

  # Make a POST request.
  #
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  #
  # returns - [Response] the response
  def post(path, options = {}, authorization = nil, headers = {})
    request(:post, path, options, authorization, headers)
  end

  # Make a PUT request.
  #
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  #
  # returns [Response] the response
  def put(path, options = {})
    request(:put, path, form: options)
  end

  # Make a PATCH request.
  #
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  #
  # returns [Response] the response
  def patch(path, options = {})
    request(:patch, path, form: options)
  end

  # Make a DELETE request.
  # path    - [String] the path relative to the endpoint
  # options - [Hash] the parameters to supply
  # returns [Response] the response
  def delete(path, options = {})
    request(:delete, path, options)
  end

  def api_base_uri
    raise "specify me"
  end

  def perform_offline?
    false
  end

  def request(method, path, payload = {}, authorization = nil, headers = {})
    # Mock out the request
    return Struct.new(:body).new("{}") if perform_offline?

    connection.send(method) do |req|
      req.url [api_base_uri, path].join("")
      req.headers["Authorization"] = authorization || (defined?(api_authorization_header) && api_authorization_header)
      req.headers["Content-Type"] = "application/json"
      req.headers.merge!(headers)

      req.body = payload.to_json if method.to_sym.eql?(:post) || method.to_sym.eql?(:delete)
      req.params = payload if method.to_sym.eql?(:get)
    end
  end

  def connection(raise_error: true)
    @connection ||= begin
      require "faraday"
      Faraday.new(url: api_base_uri) do |faraday|
        # yolo
        # faraday.proxy = proxy_url if proxy_url.present?
        faraday.request :retry, max: retry_count, interval: 0.05, interval_randomness: 0.5, backoff_factor: 2

        # Log level info: Brief summaries
        # Log level debug: Detailed bodies and headers
        faraday.response(:logger, Rails.logger, bodies: true, headers: true)

        if raise_error
          faraday.use(Faraday::Response::RaiseError)
        end

        faraday.adapter Faraday.default_adapter
      end
    end
  end

  ## Some convenience methods used for Sorbet typed responses in various clients
{'fraction_non_alphanumeric': 0.10630323679727427, 'fraction_numerical': 0.0030664395229982964, 'mean_word_length': 3.0489655172413794, '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': '4645361', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1747, 'n_words': 572}